diff --git a/.editorconfig b/.editorconfig
index bb9c61f6623de0..28e3eaba64e83b 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 cf97025667a2bd..408bc1b422fe31 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 2b6c9b7ebad22d..ffecc9b0d493b0 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 @@ public static object GetClassFactoryForType(ComActivationContext cxt)
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 @@ public static void ClassRegistrationScenarioForType(ComActivationContext cxt, bo
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 @@ public static unsafe int RegisterClassForTypeInternal(ComActivationContextIntern
if (cxtInt.InterfaceId != Guid.Empty
|| cxtInt.ClassFactoryDest != IntPtr.Zero)
{
- throw new ArgumentException();
+ throw new ArgumentException(null, nameof(pCxtInt));
}
try
@@ -328,7 +328,7 @@ public static unsafe int UnregisterClassForTypeInternal(ComActivationContextInte
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 d32d53ae753b6e..8c7039e557b457 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 @@ public TypedReference GetNextArg(RuntimeTypeHandle rth)
// 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 fc8a9650f63b7e..e3e1b54373fa5e 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 @@ public static void AddMemoryPressure(long bytesAllocated)
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 8c377012666b56..6bdd48d7290a90 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 @@ public IntPtr GetOrCreateComInterfaceForObject(object instance, CreateComInterfa
{
IntPtr ptr;
if (!TryGetOrCreateComInterfaceForObjectInternal(this, instance, flags, out ptr))
- throw new ArgumentException();
+ throw new ArgumentException(null, nameof(instance));
return ptr;
}
@@ -215,7 +215,7 @@ public object GetOrCreateObjectForComInstance(IntPtr externalComObject, CreateOb
{
object? obj;
if (!TryGetOrCreateObjectForComInstanceInternal(this, externalComObject, flags, null, out obj))
- throw new ArgumentNullException();
+ throw new ArgumentNullException(nameof(externalComObject));
return obj!;
}
@@ -271,7 +271,7 @@ public object GetOrRegisterObjectForComInstance(IntPtr externalComObject, Create
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 93cfd86bc167b1..bde5f089c75c8a 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
@@ -2752,7 +2752,7 @@ public override InterfaceMapping GetInterfaceMap(Type ifaceType)
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);
@@ -2788,7 +2788,7 @@ public override InterfaceMapping GetInterfaceMap(Type ifaceType)
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);
@@ -2851,7 +2851,7 @@ public override InterfaceMapping GetInterfaceMap(Type ifaceType)
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;
@@ -2885,7 +2885,7 @@ public override InterfaceMapping GetInterfaceMap(Type ifaceType)
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;
@@ -2913,7 +2913,7 @@ public override InterfaceMapping GetInterfaceMap(Type ifaceType)
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 c2d72e3915cae7..9752c3cbde3667 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 @@ private static extern long PendingUnmanagedWorkItemCount
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 71b23e1f7cd3f7..a7b047119d5d5f 100644
--- a/src/libraries/Common/src/System/Buffers/ArrayBufferWriter.cs
+++ b/src/libraries/Common/src/System/Buffers/ArrayBufferWriter.cs
@@ -42,7 +42,7 @@ public ArrayBufferWriter()
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 @@ public void Clear()
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 ecd9f27ecb8dd8..96b41501f3407d 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 @@ public static void End(IAsyncResult asyncResult)
return;
}
- throw new ArgumentNullException();
+ throw new ArgumentNullException(nameof(asyncResult));
}
/// Processes an IAsyncResult returned by Begin.
@@ -55,7 +55,7 @@ public static TResult End(IAsyncResult asyncResult)
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 d1688c9c860b08..45f0d593336675 100644
--- a/src/libraries/Common/tests/TestUtilities/System/AssertExtensions.cs
+++ b/src/libraries/Common/tests/TestUtilities/System/AssertExtensions.cs
@@ -27,7 +27,7 @@ public static void ThrowsContains(Action action, string expectedMessageConten
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 @@ public static void Throws(string netCoreParamName, string netFxParamName, Act
if (netFxParamName == null && IsNetFramework)
{
// Param name varies between .NET Framework versions -- skip checking it
- return;
+ return exception;
}
string expectedParamName =
@@ -43,6 +43,7 @@ public static void Throws(string netCoreParamName, string netFxParamName, Act
netFxParamName : netCoreParamName;
Assert.Equal(expectedParamName, exception.ParamName);
+ return exception;
}
public static void Throws(string netCoreParamName, string netFxParamName, Func