Skip to content

Commit 215b39a

Browse files
authored
Remove usage of !! from dotnet/runtime (#68178)
* 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
1 parent ed04fef commit 215b39a

File tree

1,398 files changed

+18934
-6507
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,398 files changed

+18934
-6507
lines changed

eng/CodeAnalysis.src.globalconfig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,9 +1557,6 @@ dotnet_diagnostic.IDE0160.severity = silent
15571557
# IDE0161: Convert to file-scoped namespace
15581558
dotnet_diagnostic.IDE0161.severity = silent
15591559

1560-
# IDE0190: Null check can be simplified
1561-
dotnet_diagnostic.IDE0190.severity = suggestion
1562-
15631560
# IDE1005: Delegate invocation can be simplified.
15641561
dotnet_diagnostic.IDE1005.severity = suggestion
15651562

eng/CodeAnalysis.test.globalconfig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,9 +1551,6 @@ dotnet_diagnostic.IDE0160.severity = silent
15511551
# IDE0161: Convert to file-scoped namespace
15521552
dotnet_diagnostic.IDE0161.severity = silent
15531553

1554-
# IDE0190: Null check can be simplified
1555-
dotnet_diagnostic.IDE0190.severity = silent
1556-
15571554
# IDE1005: Delegate invocation can be simplified.
15581555
dotnet_diagnostic.IDE1005.severity = silent
15591556

src/coreclr/System.Private.CoreLib/src/Microsoft/Win32/OAVariantLib.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,11 @@ internal static class OAVariantLib
7272
* Variant and the types that CLR supports explicitly in the
7373
* CLR Variant class.
7474
*/
75-
internal static Variant ChangeType(Variant source, Type targetClass!!, short options, CultureInfo culture!!)
75+
internal static Variant ChangeType(Variant source, Type targetClass, short options, CultureInfo culture)
7676
{
77+
ArgumentNullException.ThrowIfNull(targetClass);
78+
ArgumentNullException.ThrowIfNull(culture);
79+
7780
Variant result = default;
7881
ChangeTypeEx(ref result, ref source,
7982
culture.LCID,

src/coreclr/System.Private.CoreLib/src/System/Attribute.CoreCLR.cs

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,11 @@ public static Attribute[] GetCustomAttributes(MemberInfo element, Type attribute
448448
return GetCustomAttributes(element, attributeType, true);
449449
}
450450

451-
public static Attribute[] GetCustomAttributes(MemberInfo element!!, Type attributeType!!, bool inherit)
451+
public static Attribute[] GetCustomAttributes(MemberInfo element, Type attributeType, bool inherit)
452452
{
453+
ArgumentNullException.ThrowIfNull(element);
454+
ArgumentNullException.ThrowIfNull(attributeType);
455+
453456
if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
454457
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);
455458

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

469-
public static Attribute[] GetCustomAttributes(MemberInfo element!!, bool inherit)
472+
public static Attribute[] GetCustomAttributes(MemberInfo element, bool inherit)
470473
{
474+
ArgumentNullException.ThrowIfNull(element);
475+
471476
return element.MemberType switch
472477
{
473478
MemberTypes.Property => InternalGetCustomAttributes((PropertyInfo)element, typeof(Attribute), inherit),
@@ -481,8 +486,11 @@ public static bool IsDefined(MemberInfo element, Type attributeType)
481486
return IsDefined(element, attributeType, true);
482487
}
483488

484-
public static bool IsDefined(MemberInfo element!!, Type attributeType!!, bool inherit)
489+
public static bool IsDefined(MemberInfo element, Type attributeType, bool inherit)
485490
{
491+
ArgumentNullException.ThrowIfNull(element);
492+
ArgumentNullException.ThrowIfNull(attributeType);
493+
486494
// Returns true if a custom attribute subclass of attributeType class/interface with inheritance walk
487495
if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
488496
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);
@@ -526,8 +534,11 @@ public static Attribute[] GetCustomAttributes(ParameterInfo element, Type attrib
526534
return GetCustomAttributes(element, attributeType, true);
527535
}
528536

529-
public static Attribute[] GetCustomAttributes(ParameterInfo element!!, Type attributeType!!, bool inherit)
537+
public static Attribute[] GetCustomAttributes(ParameterInfo element, Type attributeType, bool inherit)
530538
{
539+
ArgumentNullException.ThrowIfNull(element);
540+
ArgumentNullException.ThrowIfNull(attributeType);
541+
531542
if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
532543
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);
533544

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

544-
public static Attribute[] GetCustomAttributes(ParameterInfo element!!, bool inherit)
555+
public static Attribute[] GetCustomAttributes(ParameterInfo element, bool inherit)
545556
{
557+
ArgumentNullException.ThrowIfNull(element);
558+
546559
if (element.Member == null)
547560
throw new ArgumentException(SR.Argument_InvalidParameterInfo, nameof(element));
548561

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

561-
public static bool IsDefined(ParameterInfo element!!, Type attributeType!!, bool inherit)
574+
public static bool IsDefined(ParameterInfo element, Type attributeType, bool inherit)
562575
{
576+
ArgumentNullException.ThrowIfNull(element);
577+
ArgumentNullException.ThrowIfNull(attributeType);
578+
563579
// Returns true is a custom attribute subclass of attributeType class/interface with inheritance walk
564580

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

623-
public static Attribute[] GetCustomAttributes(Module element!!, bool inherit)
639+
public static Attribute[] GetCustomAttributes(Module element, bool inherit)
624640
{
641+
ArgumentNullException.ThrowIfNull(element);
642+
625643
return (Attribute[])element.GetCustomAttributes(typeof(Attribute), inherit);
626644
}
627645

628-
public static Attribute[] GetCustomAttributes(Module element!!, Type attributeType!!, bool inherit)
646+
public static Attribute[] GetCustomAttributes(Module element, Type attributeType, bool inherit)
629647
{
648+
ArgumentNullException.ThrowIfNull(element);
649+
ArgumentNullException.ThrowIfNull(attributeType);
650+
630651
if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
631652
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);
632653

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

641-
public static bool IsDefined(Module element!!, Type attributeType!!, bool inherit)
662+
public static bool IsDefined(Module element, Type attributeType, bool inherit)
642663
{
664+
ArgumentNullException.ThrowIfNull(element);
665+
ArgumentNullException.ThrowIfNull(attributeType);
666+
643667
// Returns true is a custom attribute subclass of attributeType class/interface with no inheritance walk
644668

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

679-
public static Attribute[] GetCustomAttributes(Assembly element!!, Type attributeType!!, bool inherit)
703+
public static Attribute[] GetCustomAttributes(Assembly element, Type attributeType, bool inherit)
680704
{
705+
ArgumentNullException.ThrowIfNull(element);
706+
ArgumentNullException.ThrowIfNull(attributeType);
707+
681708
if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))
682709
throw new ArgumentException(SR.Argument_MustHaveAttributeBaseClass);
683710

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

692-
public static Attribute[] GetCustomAttributes(Assembly element!!, bool inherit)
719+
public static Attribute[] GetCustomAttributes(Assembly element, bool inherit)
693720
{
721+
ArgumentNullException.ThrowIfNull(element);
722+
694723
return (Attribute[])element.GetCustomAttributes(typeof(Attribute), inherit);
695724
}
696725

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

702-
public static bool IsDefined(Assembly element!!, Type attributeType!!, bool inherit)
731+
public static bool IsDefined(Assembly element, Type attributeType, bool inherit)
703732
{
733+
ArgumentNullException.ThrowIfNull(element);
734+
ArgumentNullException.ThrowIfNull(attributeType);
735+
704736
// Returns true is a custom attribute subclass of attributeType class/interface with no inheritance walk
705737

706738
if (!attributeType.IsSubclassOf(typeof(Attribute)) && attributeType != typeof(Attribute))

src/coreclr/System.Private.CoreLib/src/System/Collections/EmptyReadOnlyDictionaryInternal.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ IEnumerator IEnumerable.GetEnumerator()
3333

3434
// ICollection members
3535

36-
public void CopyTo(Array array!!, int index)
36+
public void CopyTo(Array array, int index)
3737
{
38+
ArgumentNullException.ThrowIfNull(array);
39+
3840
if (array.Rank != 1)
3941
throw new ArgumentException(SR.Arg_RankMultiDimNotSupported);
4042

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

5658
// IDictionary members
5759

58-
public object? this[object key!!]
60+
public object? this[object key]
5961
{
6062
get
6163
{
64+
ArgumentNullException.ThrowIfNull(key);
65+
6266
return null;
6367
}
6468
set
6569
{
70+
ArgumentNullException.ThrowIfNull(key);
71+
6672
if (!key.GetType().IsSerializable)
6773
throw new ArgumentException(SR.Argument_NotSerializable, nameof(key));
6874

@@ -82,8 +88,10 @@ public bool Contains(object key)
8288
return false;
8389
}
8490

85-
public void Add(object key!!, object? value)
91+
public void Add(object key, object? value)
8692
{
93+
ArgumentNullException.ThrowIfNull(key);
94+
8795
if (!key.GetType().IsSerializable)
8896
throw new ArgumentException(SR.Argument_NotSerializable, nameof(key));
8997

src/coreclr/System.Private.CoreLib/src/System/Delegate.CoreCLR.cs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ public abstract partial class Delegate : ICloneable, ISerializable
3434
// This constructor is called from the class generated by the
3535
// compiler generated code
3636
[RequiresUnreferencedCode("The target method might be removed")]
37-
protected Delegate(object target!!, string method!!)
37+
protected Delegate(object target, string method)
3838
{
39+
ArgumentNullException.ThrowIfNull(target);
40+
ArgumentNullException.ThrowIfNull(method);
41+
3942
// This API existed in v1/v1.1 and only expected to create closed
4043
// instance delegates. Constrain the call to BindToMethodName to
4144
// such and don't allow relaxed signature matching (which could make
@@ -51,8 +54,11 @@ protected Delegate(object target!!, string method!!)
5154
// This constructor is called from a class to generate a
5255
// delegate based upon a static method name and the Type object
5356
// for the class defining the method.
54-
protected Delegate([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type target!!, string method!!)
57+
protected Delegate([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type target, string method)
5558
{
59+
ArgumentNullException.ThrowIfNull(target);
60+
ArgumentNullException.ThrowIfNull(method);
61+
5662
if (target.ContainsGenericParameters)
5763
throw new ArgumentException(SR.Arg_UnboundGenParam, nameof(target));
5864
if (!(target is RuntimeType rtTarget))
@@ -207,8 +213,12 @@ protected virtual MethodInfo GetMethodImpl()
207213

208214
// V1 API.
209215
[RequiresUnreferencedCode("The target method might be removed")]
210-
public static Delegate? CreateDelegate(Type type!!, object target!!, string method!!, bool ignoreCase, bool throwOnBindFailure)
216+
public static Delegate? CreateDelegate(Type type, object target, string method, bool ignoreCase, bool throwOnBindFailure)
211217
{
218+
ArgumentNullException.ThrowIfNull(type);
219+
ArgumentNullException.ThrowIfNull(target);
220+
ArgumentNullException.ThrowIfNull(method);
221+
212222
if (!(type is RuntimeType rtType))
213223
throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(type));
214224
if (!rtType.IsDelegate())
@@ -238,8 +248,12 @@ protected virtual MethodInfo GetMethodImpl()
238248
}
239249

240250
// V1 API.
241-
public static Delegate? CreateDelegate(Type type!!, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type target!!, string method!!, bool ignoreCase, bool throwOnBindFailure)
251+
public static Delegate? CreateDelegate(Type type, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type target, string method, bool ignoreCase, bool throwOnBindFailure)
242252
{
253+
ArgumentNullException.ThrowIfNull(type);
254+
ArgumentNullException.ThrowIfNull(target);
255+
ArgumentNullException.ThrowIfNull(method);
256+
243257
if (target.ContainsGenericParameters)
244258
throw new ArgumentException(SR.Arg_UnboundGenParam, nameof(target));
245259
if (!(type is RuntimeType rtType))
@@ -270,9 +284,10 @@ protected virtual MethodInfo GetMethodImpl()
270284
}
271285

272286
// V1 API.
273-
public static Delegate? CreateDelegate(Type type!!, MethodInfo method!!, bool throwOnBindFailure)
287+
public static Delegate? CreateDelegate(Type type, MethodInfo method, bool throwOnBindFailure)
274288
{
275-
// Validate the parameters.
289+
ArgumentNullException.ThrowIfNull(type);
290+
ArgumentNullException.ThrowIfNull(method);
276291

277292
if (!(type is RuntimeType rtType))
278293
throw new ArgumentException(SR.Argument_MustBeRuntimeType, nameof(type));
@@ -306,11 +321,8 @@ protected virtual MethodInfo GetMethodImpl()
306321
// V2 API.
307322
public static Delegate? CreateDelegate(Type type, object? firstArgument, MethodInfo method, bool throwOnBindFailure)
308323
{
309-
// Validate the parameters.
310-
if (type == null)
311-
throw new ArgumentNullException(nameof(type));
312-
if (method == null)
313-
throw new ArgumentNullException(nameof(method));
324+
ArgumentNullException.ThrowIfNull(type);
325+
ArgumentNullException.ThrowIfNull(method);
314326

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

345357
// V2 internal API.
346-
internal static Delegate CreateDelegateNoSecurityCheck(Type type!!, object? target, RuntimeMethodHandle method)
358+
internal static Delegate CreateDelegateNoSecurityCheck(Type type, object? target, RuntimeMethodHandle method)
347359
{
348-
// Validate the parameters.
360+
ArgumentNullException.ThrowIfNull(type);
349361

350362
if (method.IsNullHandle())
351363
throw new ArgumentNullException(nameof(method));

src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,10 @@ public static void WaitForPendingFinalizers()
302302
[MethodImpl(MethodImplOptions.InternalCall)]
303303
private static extern void _SuppressFinalize(object o);
304304

305-
public static void SuppressFinalize(object obj!!)
305+
public static void SuppressFinalize(object obj)
306306
{
307+
ArgumentNullException.ThrowIfNull(obj);
308+
307309
_SuppressFinalize(obj);
308310
}
309311

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

317-
public static void ReRegisterForFinalize(object obj!!)
319+
public static void ReRegisterForFinalize(object obj)
318320
{
321+
ArgumentNullException.ThrowIfNull(obj);
322+
319323
_ReRegisterForFinalize(obj);
320324
}
321325

@@ -617,8 +621,10 @@ internal static void RegisterMemoryLoadChangeNotification(float lowMemoryPercent
617621
}
618622
}
619623

620-
internal static void UnregisterMemoryLoadChangeNotification(Action notification!!)
624+
internal static void UnregisterMemoryLoadChangeNotification(Action notification)
621625
{
626+
ArgumentNullException.ThrowIfNull(notification);
627+
622628
lock (s_notifications)
623629
{
624630
for (int i = 0; i < s_notifications.Count; ++i)

src/coreclr/System.Private.CoreLib/src/System/Reflection/Assembly.CoreCLR.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ public static Assembly Load(string assemblyString)
2323

2424
[Obsolete("Assembly.LoadWithPartialName has been deprecated. Use Assembly.Load() instead.")]
2525
[System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod
26-
public static Assembly? LoadWithPartialName(string partialName!!)
26+
public static Assembly? LoadWithPartialName(string partialName)
2727
{
28+
ArgumentNullException.ThrowIfNull(partialName);
29+
2830
if ((partialName.Length == 0) || (partialName[0] == '\0'))
2931
throw new ArgumentException(SR.Format_StringZeroLength, nameof(partialName));
3032

@@ -42,8 +44,10 @@ public static Assembly Load(string assemblyString)
4244
// Locate an assembly by its name. The name can be strong or
4345
// weak. The assembly is loaded into the domain of the caller.
4446
[System.Security.DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod
45-
public static Assembly Load(AssemblyName assemblyRef!!)
47+
public static Assembly Load(AssemblyName assemblyRef)
4648
{
49+
ArgumentNullException.ThrowIfNull(assemblyRef);
50+
4751
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
4852
return RuntimeAssembly.InternalLoad(assemblyRef, ref stackMark, AssemblyLoadContext.CurrentContextualReflectionContext);
4953
}

0 commit comments

Comments
 (0)