diff --git a/.editorconfig b/.editorconfig
index bb9c61f6623..28e3eaba64e 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -155,6 +155,7 @@ csharp_space_between_square_brackets = false
# Analyzers
dotnet_code_quality.ca1802.api_surface = private, internal
+dotnet_code_quality.CA2208.api_surface = public
# C++ Files
[*.{cpp,h,in}]
diff --git a/eng/CodeAnalysis.ruleset b/eng/CodeAnalysis.ruleset
index bc11bd3b6d5..66a6af943a1 100644
--- a/eng/CodeAnalysis.ruleset
+++ b/eng/CodeAnalysis.ruleset
@@ -106,7 +106,7 @@
-
+
diff --git a/src/coreclr/src/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComActivator.cs b/src/coreclr/src/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComActivator.cs
index 2b6c9b7ebad..ffecc9b0d49 100644
--- a/src/coreclr/src/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComActivator.cs
+++ b/src/coreclr/src/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComActivator.cs
@@ -124,7 +124,7 @@ namespace Internal.Runtime.InteropServices
if (!Path.IsPathRooted(cxt.AssemblyPath))
{
- throw new ArgumentException();
+ throw new ArgumentException(null, nameof(cxt));
}
Type classType = FindClassType(cxt.ClassId, cxt.AssemblyPath, cxt.AssemblyName, cxt.TypeName);
@@ -156,7 +156,7 @@ namespace Internal.Runtime.InteropServices
if (!Path.IsPathRooted(cxt.AssemblyPath))
{
- throw new ArgumentException();
+ throw new ArgumentException(null, nameof(cxt));
}
Type classType = FindClassType(cxt.ClassId, cxt.AssemblyPath, cxt.AssemblyName, cxt.TypeName);
@@ -288,7 +288,7 @@ $@"{nameof(RegisterClassForTypeInternal)} arguments:
if (cxtInt.InterfaceId != Guid.Empty
|| cxtInt.ClassFactoryDest != IntPtr.Zero)
{
- throw new ArgumentException();
+ throw new ArgumentException(null, nameof(pCxtInt));
}
try
@@ -328,7 +328,7 @@ $@"{nameof(UnregisterClassForTypeInternal)} arguments:
if (cxtInt.InterfaceId != Guid.Empty
|| cxtInt.ClassFactoryDest != IntPtr.Zero)
{
- throw new ArgumentException();
+ throw new ArgumentException(null, nameof(pCxtInt));
}
try
diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/ArgIterator.cs b/src/coreclr/src/System.Private.CoreLib/src/System/ArgIterator.cs
index d32d53ae753..8c7039e557b 100644
--- a/src/coreclr/src/System.Private.CoreLib/src/System/ArgIterator.cs
+++ b/src/coreclr/src/System.Private.CoreLib/src/System/ArgIterator.cs
@@ -85,7 +85,9 @@ namespace System
// malicious caller to increment the pointer to an arbitrary
// location in memory and read the contents.
if (ArgPtr == IntPtr.Zero)
+#pragma warning disable CA2208 // Instantiate argument exceptions correctly, the argument not applicable
throw new ArgumentNullException();
+#pragma warning restore CA2208
TypedReference result = default;
// reference to TypedReference is banned, so have to pass result as pointer
diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/GC.cs b/src/coreclr/src/System.Private.CoreLib/src/System/GC.cs
index fc8a9650f63..e3e1b54373f 100644
--- a/src/coreclr/src/System.Private.CoreLib/src/System/GC.cs
+++ b/src/coreclr/src/System.Private.CoreLib/src/System/GC.cs
@@ -135,7 +135,7 @@ namespace System
if ((4 == IntPtr.Size) && (bytesAllocated > int.MaxValue))
{
- throw new ArgumentOutOfRangeException("pressure",
+ throw new ArgumentOutOfRangeException(nameof(bytesAllocated),
SR.ArgumentOutOfRange_MustBeNonNegInt32);
}
diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs
index 8c377012666..6bdd48d7290 100644
--- a/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs
+++ b/src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.cs
@@ -136,7 +136,7 @@ namespace System.Runtime.InteropServices
{
IntPtr ptr;
if (!TryGetOrCreateComInterfaceForObjectInternal(this, instance, flags, out ptr))
- throw new ArgumentException();
+ throw new ArgumentException(null, nameof(instance));
return ptr;
}
@@ -215,7 +215,7 @@ namespace System.Runtime.InteropServices
{
object? obj;
if (!TryGetOrCreateObjectForComInstanceInternal(this, externalComObject, flags, null, out obj))
- throw new ArgumentNullException();
+ throw new ArgumentNullException(nameof(externalComObject));
return obj!;
}
@@ -271,7 +271,7 @@ namespace System.Runtime.InteropServices
object? obj;
if (!TryGetOrCreateObjectForComInstanceInternal(this, externalComObject, flags, wrapper, out obj))
- throw new ArgumentNullException();
+ throw new ArgumentNullException(nameof(externalComObject));
return obj!;
}
diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs b/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs
index bfbdbea7de4..38511276255 100644
--- a/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs
+++ b/src/coreclr/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs
@@ -2755,7 +2755,7 @@ namespace System
protected override PropertyInfo? GetPropertyImpl(
string name, BindingFlags bindingAttr, Binder? binder, Type? returnType, Type[]? types, ParameterModifier[]? modifiers)
{
- if (name == null) throw new ArgumentNullException();
+ if (name == null) throw new ArgumentNullException(nameof(name));
ListBuilder candidates = GetPropertyCandidates(name, bindingAttr, types, false);
@@ -2791,7 +2791,7 @@ namespace System
public override EventInfo? GetEvent(string name, BindingFlags bindingAttr)
{
- if (name is null) throw new ArgumentNullException();
+ if (name is null) throw new ArgumentNullException(nameof(name));
FilterHelper(bindingAttr, ref name, out _, out MemberListType listType);
@@ -2854,7 +2854,7 @@ namespace System
public override Type? GetInterface(string fullname, bool ignoreCase)
{
- if (fullname is null) throw new ArgumentNullException();
+ if (fullname is null) throw new ArgumentNullException(nameof(fullname));
BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.NonPublic;
@@ -2888,7 +2888,7 @@ namespace System
public override Type? GetNestedType(string fullname, BindingFlags bindingAttr)
{
- if (fullname is null) throw new ArgumentNullException();
+ if (fullname is null) throw new ArgumentNullException(nameof(fullname));
bindingAttr &= ~BindingFlags.Static;
string name, ns;
@@ -2916,7 +2916,7 @@ namespace System
public override MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr)
{
- if (name is null) throw new ArgumentNullException();
+ if (name is null) throw new ArgumentNullException(nameof(name));
ListBuilder methods = default;
ListBuilder constructors = default;
diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Threading/ThreadPool.CoreCLR.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Threading/ThreadPool.CoreCLR.cs
index c2d72e3915c..9752c3cbde3 100644
--- a/src/coreclr/src/System.Private.CoreLib/src/System/Threading/ThreadPool.CoreCLR.cs
+++ b/src/coreclr/src/System.Private.CoreLib/src/System/Threading/ThreadPool.CoreCLR.cs
@@ -260,7 +260,7 @@ namespace System.Threading
get;
}
- private static RegisteredWaitHandle RegisterWaitForSingleObject( // throws RegisterWaitException
+ private static RegisteredWaitHandle RegisterWaitForSingleObject(
WaitHandle waitObject,
WaitOrTimerCallback callBack,
object? state,
diff --git a/src/libraries/Common/src/System/Buffers/ArrayBufferWriter.cs b/src/libraries/Common/src/System/Buffers/ArrayBufferWriter.cs
index 71b23e1f7cd..a7b047119d5 100644
--- a/src/libraries/Common/src/System/Buffers/ArrayBufferWriter.cs
+++ b/src/libraries/Common/src/System/Buffers/ArrayBufferWriter.cs
@@ -42,7 +42,7 @@ namespace System.Buffers
public ArrayBufferWriter(int initialCapacity)
{
if (initialCapacity <= 0)
- throw new ArgumentException(nameof(initialCapacity));
+ throw new ArgumentException(null, nameof(initialCapacity));
_buffer = new T[initialCapacity];
_index = 0;
@@ -101,7 +101,7 @@ namespace System.Buffers
public void Advance(int count)
{
if (count < 0)
- throw new ArgumentException(nameof(count));
+ throw new ArgumentException(null, nameof(count));
if (_index > _buffer.Length - count)
ThrowInvalidOperationException_AdvancedTooFar(_buffer.Length);
diff --git a/src/libraries/Common/src/System/Threading/Tasks/TaskToApm.cs b/src/libraries/Common/src/System/Threading/Tasks/TaskToApm.cs
index ecd9f27ecb8..96b41501f34 100644
--- a/src/libraries/Common/src/System/Threading/Tasks/TaskToApm.cs
+++ b/src/libraries/Common/src/System/Threading/Tasks/TaskToApm.cs
@@ -43,7 +43,7 @@ namespace System.Threading.Tasks
return;
}
- throw new ArgumentNullException();
+ throw new ArgumentNullException(nameof(asyncResult));
}
/// Processes an IAsyncResult returned by Begin.
@@ -55,7 +55,7 @@ namespace System.Threading.Tasks
return task.GetAwaiter().GetResult();
}
- throw new ArgumentNullException();
+ throw new ArgumentNullException(nameof(asyncResult));
}
/// Provides a simple IAsyncResult that wraps a Task.
diff --git a/src/libraries/Common/tests/TestUtilities/System/AssertExtensions.cs b/src/libraries/Common/tests/TestUtilities/System/AssertExtensions.cs
index d1688c9c860..45f0d593336 100644
--- a/src/libraries/Common/tests/TestUtilities/System/AssertExtensions.cs
+++ b/src/libraries/Common/tests/TestUtilities/System/AssertExtensions.cs
@@ -27,7 +27,7 @@ namespace System
Assert.Contains(expectedMessageContent, Assert.Throws(action).Message);
}
- public static void Throws(string netCoreParamName, string netFxParamName, Action action)
+ public static T Throws(string netCoreParamName, string netFxParamName, Action action)
where T : ArgumentException
{
T exception = Assert.Throws(action);
@@ -35,7 +35,7 @@ namespace System
if (netFxParamName == null && IsNetFramework)
{
// Param name varies between .NET Framework versions -- skip checking it
- return;
+ return exception;
}
string expectedParamName =
@@ -43,6 +43,7 @@ namespace System
netFxParamName : netCoreParamName;
Assert.Equal(expectedParamName, exception.ParamName);
+ return exception;
}
public static void Throws(string netCoreParamName, string netFxParamName, Func