Skip to content

Commit

Permalink
Remove usage of !! from dotnet/runtime (#68178)
Browse files Browse the repository at this point in the history
* Remove usage of !! from dotnet/runtime

- Use ArgumentNullException.ThrowIfNull instead where possible.  It's only usable for projects that only target .NET 6+, and it can't be used in places like this(...) or base(...).
- In other cases, if the project already has a ThrowHelper, augment it for null as needed and use that.
- For most of the extensions projects, add a ThrowHelper.ThrowIfNull that replicates ArgumentNullException.ThrowIfNull.
- For everything else, just use "throw new".

* Address PR feedback

* Address PR feedback

* Remove false positives from searches

* Address PR feedback
  • Loading branch information
stephentoub authored Apr 21, 2022
1 parent ed04fef commit 215b39a
Show file tree
Hide file tree
Showing 1,398 changed files with 18,934 additions and 6,507 deletions.
3 changes: 0 additions & 3 deletions eng/CodeAnalysis.src.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1557,9 +1557,6 @@ dotnet_diagnostic.IDE0160.severity = silent
# IDE0161: Convert to file-scoped namespace
dotnet_diagnostic.IDE0161.severity = silent

# IDE0190: Null check can be simplified
dotnet_diagnostic.IDE0190.severity = suggestion

# IDE1005: Delegate invocation can be simplified.
dotnet_diagnostic.IDE1005.severity = suggestion

Expand Down
3 changes: 0 additions & 3 deletions eng/CodeAnalysis.test.globalconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1551,9 +1551,6 @@ dotnet_diagnostic.IDE0160.severity = silent
# IDE0161: Convert to file-scoped namespace
dotnet_diagnostic.IDE0161.severity = silent

# IDE0190: Null check can be simplified
dotnet_diagnostic.IDE0190.severity = silent

# IDE1005: Delegate invocation can be simplified.
dotnet_diagnostic.IDE1005.severity = silent

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ internal static class OAVariantLib
* Variant and the types that CLR supports explicitly in the
* CLR Variant class.
*/
internal static Variant ChangeType(Variant source, Type targetClass!!, short options, CultureInfo culture!!)
internal static Variant ChangeType(Variant source, Type targetClass, short options, CultureInfo culture)
{
ArgumentNullException.ThrowIfNull(targetClass);
ArgumentNullException.ThrowIfNull(culture);

Variant result = default;
ChangeTypeEx(ref result, ref source,
culture.LCID,
Expand Down
56 changes: 44 additions & 12 deletions src/coreclr/System.Private.CoreLib/src/System/Attribute.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,11 @@ public static Attribute[] GetCustomAttributes(MemberInfo element, Type attribute
return GetCustomAttributes(element, attributeType, true);
}

public static Attribute[] GetCustomAttributes(MemberInfo element!!, Type attributeType!!, bool inherit)
public static Attribute[] GetCustomAttributes(MemberInfo element, Type attributeType, bool inherit)
{
ArgumentNullException.ThrowIfNull(element);
ArgumentNullException.ThrowIfNull(attributeType);

if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);

Expand All @@ -466,8 +469,10 @@ public static Attribute[] GetCustomAttributes(MemberInfo element)
return GetCustomAttributes(element, true);
}

public static Attribute[] GetCustomAttributes(MemberInfo element!!, bool inherit)
public static Attribute[] GetCustomAttributes(MemberInfo element, bool inherit)
{
ArgumentNullException.ThrowIfNull(element);

return element.MemberType switch
{
MemberTypes.Property => InternalGetCustomAttributes((PropertyInfo)element, typeof(Attribute), inherit),
Expand All @@ -481,8 +486,11 @@ public static bool IsDefined(MemberInfo element, Type attributeType)
return IsDefined(element, attributeType, true);
}

public static bool IsDefined(MemberInfo element!!, Type attributeType!!, bool inherit)
public static bool IsDefined(MemberInfo element, Type attributeType, bool inherit)
{
ArgumentNullException.ThrowIfNull(element);
ArgumentNullException.ThrowIfNull(attributeType);

// Returns true if a custom attribute subclass of attributeType class/interface with inheritance walk
if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);
Expand Down Expand Up @@ -526,8 +534,11 @@ public static Attribute[] GetCustomAttributes(ParameterInfo element, Type attrib
return GetCustomAttributes(element, attributeType, true);
}

public static Attribute[] GetCustomAttributes(ParameterInfo element!!, Type attributeType!!, bool inherit)
public static Attribute[] GetCustomAttributes(ParameterInfo element, Type attributeType, bool inherit)
{
ArgumentNullException.ThrowIfNull(element);
ArgumentNullException.ThrowIfNull(attributeType);

if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);

Expand All @@ -541,8 +552,10 @@ public static Attribute[] GetCustomAttributes(ParameterInfo element!!, Type attr
return (Attribute[])element.GetCustomAttributes(attributeType, inherit);
}

public static Attribute[] GetCustomAttributes(ParameterInfo element!!, bool inherit)
public static Attribute[] GetCustomAttributes(ParameterInfo element, bool inherit)
{
ArgumentNullException.ThrowIfNull(element);

if (element.Member == null)
throw new ArgumentException(SR.Argument_InvalidParameterInfo, nameof(element));

Expand All @@ -558,8 +571,11 @@ public static bool IsDefined(ParameterInfo element, Type attributeType)
return IsDefined(element, attributeType, true);
}

public static bool IsDefined(ParameterInfo element!!, Type attributeType!!, bool inherit)
public static bool IsDefined(ParameterInfo element, Type attributeType, bool inherit)
{
ArgumentNullException.ThrowIfNull(element);
ArgumentNullException.ThrowIfNull(attributeType);

// Returns true is a custom attribute subclass of attributeType class/interface with inheritance walk

if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
Expand Down Expand Up @@ -620,13 +636,18 @@ public static Attribute[] GetCustomAttributes(Module element)
return GetCustomAttributes(element, true);
}

public static Attribute[] GetCustomAttributes(Module element!!, bool inherit)
public static Attribute[] GetCustomAttributes(Module element, bool inherit)
{
ArgumentNullException.ThrowIfNull(element);

return (Attribute[])element.GetCustomAttributes(typeof(Attribute), inherit);
}

public static Attribute[] GetCustomAttributes(Module element!!, Type attributeType!!, bool inherit)
public static Attribute[] GetCustomAttributes(Module element, Type attributeType, bool inherit)
{
ArgumentNullException.ThrowIfNull(element);
ArgumentNullException.ThrowIfNull(attributeType);

if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);

Expand All @@ -638,8 +659,11 @@ public static bool IsDefined(Module element, Type attributeType)
return IsDefined(element, attributeType, false);
}

public static bool IsDefined(Module element!!, Type attributeType!!, bool inherit)
public static bool IsDefined(Module element, Type attributeType, bool inherit)
{
ArgumentNullException.ThrowIfNull(element);
ArgumentNullException.ThrowIfNull(attributeType);

// Returns true is a custom attribute subclass of attributeType class/interface with no inheritance walk

if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
Expand Down Expand Up @@ -676,8 +700,11 @@ public static Attribute[] GetCustomAttributes(Assembly element, Type attributeTy
return GetCustomAttributes(element, attributeType, true);
}

public static Attribute[] GetCustomAttributes(Assembly element!!, Type attributeType!!, bool inherit)
public static Attribute[] GetCustomAttributes(Assembly element, Type attributeType, bool inherit)
{
ArgumentNullException.ThrowIfNull(element);
ArgumentNullException.ThrowIfNull(attributeType);

if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);

Expand All @@ -689,8 +716,10 @@ public static Attribute[] GetCustomAttributes(Assembly element)
return GetCustomAttributes(element, true);
}

public static Attribute[] GetCustomAttributes(Assembly element!!, bool inherit)
public static Attribute[] GetCustomAttributes(Assembly element, bool inherit)
{
ArgumentNullException.ThrowIfNull(element);

return (Attribute[])element.GetCustomAttributes(typeof(Attribute), inherit);
}

Expand All @@ -699,8 +728,11 @@ public static bool IsDefined(Assembly element, Type attributeType)
return IsDefined(element, attributeType, true);
}

public static bool IsDefined(Assembly element!!, Type attributeType!!, bool inherit)
public static bool IsDefined(Assembly element, Type attributeType, bool inherit)
{
ArgumentNullException.ThrowIfNull(element);
ArgumentNullException.ThrowIfNull(attributeType);

// Returns true is a custom attribute subclass of attributeType class/interface with no inheritance walk

if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ IEnumerator IEnumerable.GetEnumerator()

// ICollection members

public void CopyTo(Array array!!, int index)
public void CopyTo(Array array, int index)
{
ArgumentNullException.ThrowIfNull(array);

if (array.Rank != 1)
throw new ArgumentException(SR.Arg_RankMultiDimNotSupported);

Expand All @@ -55,14 +57,18 @@ public void CopyTo(Array array!!, int index)

// IDictionary members

public object? this[object key!!]
public object? this[object key]
{
get
{
ArgumentNullException.ThrowIfNull(key);

return null;
}
set
{
ArgumentNullException.ThrowIfNull(key);

if (!key.GetType().IsSerializable)
throw new ArgumentException(SR.Argument_NotSerializable, nameof(key));

Expand All @@ -82,8 +88,10 @@ public bool Contains(object key)
return false;
}

public void Add(object key!!, object? value)
public void Add(object key, object? value)
{
ArgumentNullException.ThrowIfNull(key);

if (!key.GetType().IsSerializable)
throw new ArgumentException(SR.Argument_NotSerializable, nameof(key));

Expand Down
38 changes: 25 additions & 13 deletions src/coreclr/System.Private.CoreLib/src/System/Delegate.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ public abstract partial class Delegate : ICloneable, ISerializable
// This constructor is called from the class generated by the
// compiler generated code
[RequiresUnreferencedCode("The target method might be removed")]
protected Delegate(object target!!, string method!!)
protected Delegate(object target, string method)
{
ArgumentNullException.ThrowIfNull(target);
ArgumentNullException.ThrowIfNull(method);

// This API existed in v1/v1.1 and only expected to create closed
// instance delegates. Constrain the call to BindToMethodName to
// such and don't allow relaxed signature matching (which could make
Expand All @@ -51,8 +54,11 @@ protected Delegate(object target!!, string method!!)
// This constructor is called from a class to generate a
// delegate based upon a static method name and the Type object
// for the class defining the method.
protected Delegate([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type target!!, string method!!)
protected Delegate([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type target, string method)
{
ArgumentNullException.ThrowIfNull(target);
ArgumentNullException.ThrowIfNull(method);

if (target.ContainsGenericParameters)
throw new ArgumentException(SR.Arg_UnboundGenParam, nameof(target));
if (!(target is RuntimeType rtTarget))
Expand Down Expand Up @@ -207,8 +213,12 @@ protected virtual MethodInfo GetMethodImpl()

// V1 API.
[RequiresUnreferencedCode("The target method might be removed")]
public static Delegate? CreateDelegate(Type type!!, object target!!, string method!!, bool ignoreCase, bool throwOnBindFailure)
public static Delegate? CreateDelegate(Type type, object target, string method, bool ignoreCase, bool throwOnBindFailure)
{
ArgumentNullException.ThrowIfNull(type);
ArgumentNullException.ThrowIfNull(target);
ArgumentNullException.ThrowIfNull(method);

if (!(type is RuntimeType rtType))
throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(type));
if (!rtType.IsDelegate())
Expand Down Expand Up @@ -238,8 +248,12 @@ protected virtual MethodInfo GetMethodImpl()
}

// V1 API.
public static Delegate? CreateDelegate(Type type!!, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type target!!, string method!!, bool ignoreCase, bool throwOnBindFailure)
public static Delegate? CreateDelegate(Type type, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type target, string method, bool ignoreCase, bool throwOnBindFailure)
{
ArgumentNullException.ThrowIfNull(type);
ArgumentNullException.ThrowIfNull(target);
ArgumentNullException.ThrowIfNull(method);

if (target.ContainsGenericParameters)
throw new ArgumentException(SR.Arg_UnboundGenParam, nameof(target));
if (!(type is RuntimeType rtType))
Expand Down Expand Up @@ -270,9 +284,10 @@ protected virtual MethodInfo GetMethodImpl()
}

// V1 API.
public static Delegate? CreateDelegate(Type type!!, MethodInfo method!!, bool throwOnBindFailure)
public static Delegate? CreateDelegate(Type type, MethodInfo method, bool throwOnBindFailure)
{
// Validate the parameters.
ArgumentNullException.ThrowIfNull(type);
ArgumentNullException.ThrowIfNull(method);

if (!(type is RuntimeType rtType))
throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(type));
Expand Down Expand Up @@ -306,11 +321,8 @@ protected virtual MethodInfo GetMethodImpl()
// V2 API.
public static Delegate? CreateDelegate(Type type, object? firstArgument, MethodInfo method, bool throwOnBindFailure)
{
// Validate the parameters.
if (type == null)
throw new ArgumentNullException(nameof(type));
if (method == null)
throw new ArgumentNullException(nameof(method));
ArgumentNullException.ThrowIfNull(type);
ArgumentNullException.ThrowIfNull(method);

if (!(type is RuntimeType rtType))
throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(type));
Expand Down Expand Up @@ -343,9 +355,9 @@ protected virtual MethodInfo GetMethodImpl()
//

// V2 internal API.
internal static Delegate CreateDelegateNoSecurityCheck(Type type!!, object? target, RuntimeMethodHandle method)
internal static Delegate CreateDelegateNoSecurityCheck(Type type, object? target, RuntimeMethodHandle method)
{
// Validate the parameters.
ArgumentNullException.ThrowIfNull(type);

if (method.IsNullHandle())
throw new ArgumentNullException(nameof(method));
Expand Down
12 changes: 9 additions & 3 deletions src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,10 @@ public static void WaitForPendingFinalizers()
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void _SuppressFinalize(object o);

public static void SuppressFinalize(object obj!!)
public static void SuppressFinalize(object obj)
{
ArgumentNullException.ThrowIfNull(obj);

_SuppressFinalize(obj);
}

Expand All @@ -314,8 +316,10 @@ public static void SuppressFinalize(object obj!!)
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void _ReRegisterForFinalize(object o);

public static void ReRegisterForFinalize(object obj!!)
public static void ReRegisterForFinalize(object obj)
{
ArgumentNullException.ThrowIfNull(obj);

_ReRegisterForFinalize(obj);
}

Expand Down Expand Up @@ -617,8 +621,10 @@ internal static void RegisterMemoryLoadChangeNotification(float lowMemoryPercent
}
}

internal static void UnregisterMemoryLoadChangeNotification(Action notification!!)
internal static void UnregisterMemoryLoadChangeNotification(Action notification)
{
ArgumentNullException.ThrowIfNull(notification);

lock (s_notifications)
{
for (int i = 0; i < s_notifications.Count; ++i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ public static Assembly Load(string assemblyString)

[Obsolete("Assembly.LoadWithPartialName has been deprecated. Use Assembly.Load() instead.")]
[System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod
public static Assembly? LoadWithPartialName(string partialName!!)
public static Assembly? LoadWithPartialName(string partialName)
{
ArgumentNullException.ThrowIfNull(partialName);

if ((partialName.Length == 0) || (partialName[0] == '\0'))
throw new ArgumentException(SR.Format_StringZeroLength, nameof(partialName));

Expand All @@ -42,8 +44,10 @@ public static Assembly Load(string assemblyString)
// Locate an assembly by its name. The name can be strong or
// weak. The assembly is loaded into the domain of the caller.
[System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod
public static Assembly Load(AssemblyName assemblyRef!!)
public static Assembly Load(AssemblyName assemblyRef)
{
ArgumentNullException.ThrowIfNull(assemblyRef);

StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return RuntimeAssembly.InternalLoad(assemblyRef, ref stackMark, AssemblyLoadContext.CurrentContextualReflectionContext);
}
Expand Down
Loading

0 comments on commit 215b39a

Please sign in to comment.