Skip to content

Commit 003e43b

Browse files
committed
Add/use ArgumentException.ThrowIfNullOrEmpty
Adds ArgumentException.ThrowIfNullOrEmpty and then uses it in a bunch of places around the tree. Most of the replaced places used a resource string for the message, and I used it in places where it didn't feel like we were losing any meaningful information by centralizing on the single shared string, but I didn't use it in places where the resource string message used in the empty case conveyed something that seemed useful, e.g. a recommendation on what to use instead of an empty string. There are a few places where I allowed for order of exception throws to change in the case where there were multiple user errors and the validation for a single argument was split, e.g. I changed a few cases of: ```C# if (arg1 is null) throw new ArgumentNullException(...); if (arg2 is null) throw new ArgumentNullException(...); if (arg1.Length == 0) throw new ArgumentException(...); if (arg2.Length == 0) throw new ArgumentException(...); ``` to: ```C# ArgumentException.ThrowIfNullOrEmpty(arg1); ArgumentException.ThrowIfNullOrEmpty(arg2); ``` even though it'll produce a different exception for `M("", null)` than it would previously. Technically a breaking change, but given this is a case of multiple usage errors and all the exceptions are ArgumentException-based, I think it's acceptable. I wanted to get this in before we do the massive !! conversion, as I expect !! auto-fixes will make it a bit harder to roll this out. I explicitly did not do anything to roll out use of ArgumentNullException.ThrowIfNull, except in cases where I was modifying a method to use ArgumentException.ThrowIfNullOrEmpty, in which case for consistency I changed anything else in the function that could have been using ThrowIfNull to do so.
1 parent 1092617 commit 003e43b

File tree

136 files changed

+553
-1462
lines changed

Some content is hidden

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

136 files changed

+553
-1462
lines changed

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,7 @@ public ModuleBuilder DefineDynamicModule(string name)
314314

315315
private ModuleBuilder DefineDynamicModuleInternalNoLock(string name)
316316
{
317-
if (name == null)
318-
{
319-
throw new ArgumentNullException(nameof(name));
320-
}
321-
if (name.Length == 0)
322-
{
323-
throw new ArgumentException(SR.Argument_EmptyName, nameof(name));
324-
}
317+
ArgumentException.ThrowIfNullOrEmpty(name);
325318
if (name[0] == '\0')
326319
{
327320
throw new ArgumentException(SR.Argument_InvalidName, nameof(name));
@@ -467,14 +460,7 @@ public override Assembly GetSatelliteAssembly(CultureInfo culture, Version? vers
467460
/// <param name="name">The name of module for the look up.</param>
468461
private ModuleBuilder? GetDynamicModuleNoLock(string name)
469462
{
470-
if (name == null)
471-
{
472-
throw new ArgumentNullException(nameof(name));
473-
}
474-
if (name.Length == 0)
475-
{
476-
throw new ArgumentException(SR.Argument_EmptyName, nameof(name));
477-
}
463+
ArgumentException.ThrowIfNullOrEmpty(name);
478464

479465
for (int i = 0; i < _assemblyData._moduleBuilderList.Count; i++)
480466
{

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/FieldBuilder.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,12 @@ public sealed class FieldBuilder : FieldInfo
2020
internal FieldBuilder(TypeBuilder typeBuilder, string fieldName, Type type,
2121
Type[]? requiredCustomModifiers, Type[]? optionalCustomModifiers, FieldAttributes attributes)
2222
{
23-
if (fieldName == null)
24-
throw new ArgumentNullException(nameof(fieldName));
25-
26-
if (fieldName.Length == 0)
27-
throw new ArgumentException(SR.Argument_EmptyName, nameof(fieldName));
23+
ArgumentException.ThrowIfNullOrEmpty(fieldName);
2824

2925
if (fieldName[0] == '\0')
3026
throw new ArgumentException(SR.Argument_IllegalName, nameof(fieldName));
3127

32-
if (type == null)
33-
throw new ArgumentNullException(nameof(type));
28+
ArgumentNullException.ThrowIfNull(type);
3429

3530
if (type == typeof(void))
3631
throw new ArgumentException(SR.Argument_BadFieldType);

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ILGenerator.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,11 +1273,7 @@ public virtual void UsingNamespace(string usingNamespace)
12731273
// Specifying the namespace to be used in evaluating locals and watches
12741274
// for the current active lexical scope.
12751275

1276-
if (usingNamespace == null)
1277-
throw new ArgumentNullException(nameof(usingNamespace));
1278-
1279-
if (usingNamespace.Length == 0)
1280-
throw new ArgumentException(SR.Argument_EmptyName, nameof(usingNamespace));
1276+
ArgumentException.ThrowIfNullOrEmpty(usingNamespace);
12811277

12821278
MethodBuilder? methodBuilder = m_methodBuilder as MethodBuilder;
12831279
if (methodBuilder == null)

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilder.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,18 @@ internal MethodBuilder(string name, MethodAttributes attributes, CallingConventi
6161
Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers,
6262
ModuleBuilder mod, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TypeBuilder type)
6363
{
64-
if (name == null)
65-
throw new ArgumentNullException(nameof(name));
66-
67-
if (name.Length == 0)
68-
throw new ArgumentException(SR.Argument_EmptyName, nameof(name));
64+
ArgumentException.ThrowIfNullOrEmpty(name);
6965

7066
if (name[0] == '\0')
7167
throw new ArgumentException(SR.Argument_IllegalName, nameof(name));
7268

73-
if (mod == null)
74-
throw new ArgumentNullException(nameof(mod));
69+
ArgumentNullException.ThrowIfNull(mod);
7570

7671
if (parameterTypes != null)
7772
{
7873
foreach (Type t in parameterTypes)
7974
{
80-
if (t == null)
81-
throw new ArgumentNullException(nameof(parameterTypes));
75+
ArgumentNullException.ThrowIfNull(t, nameof(parameterTypes));
8276
}
8377
}
8478

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/ModuleBuilder.cs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -948,14 +948,7 @@ private MethodBuilder DefineGlobalMethodNoLock(string name, MethodAttributes att
948948
{
949949
throw new InvalidOperationException(SR.InvalidOperation_GlobalsHaveBeenCreated);
950950
}
951-
if (name == null)
952-
{
953-
throw new ArgumentNullException(nameof(name));
954-
}
955-
if (name.Length == 0)
956-
{
957-
throw new ArgumentException(SR.Argument_EmptyName, nameof(name));
958-
}
951+
ArgumentException.ThrowIfNullOrEmpty(name);
959952
if ((attributes & MethodAttributes.Static) == 0)
960953
{
961954
throw new ArgumentException(SR.Argument_GlobalFunctionHasToBeStatic);
@@ -1352,18 +1345,8 @@ internal int GetArrayMethodToken(Type arrayClass, string methodName, CallingConv
13521345
private int GetArrayMethodTokenNoLock(Type arrayClass, string methodName, CallingConventions callingConvention,
13531346
Type? returnType, Type[]? parameterTypes)
13541347
{
1355-
if (arrayClass == null)
1356-
{
1357-
throw new ArgumentNullException(nameof(arrayClass));
1358-
}
1359-
if (methodName == null)
1360-
{
1361-
throw new ArgumentNullException(nameof(methodName));
1362-
}
1363-
if (methodName.Length == 0)
1364-
{
1365-
throw new ArgumentException(SR.Argument_EmptyName, nameof(methodName));
1366-
}
1348+
ArgumentNullException.ThrowIfNull(arrayClass);
1349+
ArgumentException.ThrowIfNullOrEmpty(methodName);
13671350
if (!arrayClass.IsArray)
13681351
{
13691352
throw new ArgumentException(SR.Argument_HasToBeArrayClass);

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/PropertyBuilder.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ internal PropertyBuilder(
3434
int prToken, // the metadata token for this property
3535
TypeBuilder containingType) // the containing type
3636
{
37-
if (name == null)
38-
throw new ArgumentNullException(nameof(name));
39-
if (name.Length == 0)
40-
throw new ArgumentException(SR.Argument_EmptyName, nameof(name));
37+
ArgumentException.ThrowIfNullOrEmpty(name);
4138
if (name[0] == '\0')
4239
throw new ArgumentException(SR.Argument_IllegalName, nameof(name));
4340

src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -480,11 +480,7 @@ internal TypeBuilder(
480480
string fullname, TypeAttributes attr, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type? parent, Type[]? interfaces, ModuleBuilder module,
481481
PackingSize iPackingSize, int iTypeSize, TypeBuilder? enclosingType)
482482
{
483-
if (fullname == null)
484-
throw new ArgumentNullException(nameof(fullname));
485-
486-
if (fullname.Length == 0)
487-
throw new ArgumentException(SR.Argument_EmptyName, nameof(fullname));
483+
ArgumentException.ThrowIfNullOrEmpty(fullname);
488484

489485
if (fullname[0] == '\0')
490486
throw new ArgumentException(SR.Argument_IllegalName, nameof(fullname));
@@ -581,11 +577,7 @@ private FieldBuilder DefineDataHelper(string name, byte[]? data, int size, Field
581577
FieldBuilder fdBuilder;
582578
TypeAttributes typeAttributes;
583579

584-
if (name == null)
585-
throw new ArgumentNullException(nameof(name));
586-
587-
if (name.Length == 0)
588-
throw new ArgumentException(SR.Argument_EmptyName, nameof(name));
580+
ArgumentException.ThrowIfNullOrEmpty(name);
589581

590582
if (size <= 0 || size >= 0x003f0000)
591583
throw new ArgumentException(SR.Argument_BadSizeForData);
@@ -1282,11 +1274,7 @@ private MethodBuilder DefineMethodNoLock(string name, MethodAttributes attribute
12821274
Type? returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers,
12831275
Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers)
12841276
{
1285-
if (name == null)
1286-
throw new ArgumentNullException(nameof(name));
1287-
1288-
if (name.Length == 0)
1289-
throw new ArgumentException(SR.Argument_EmptyName, nameof(name));
1277+
ArgumentException.ThrowIfNullOrEmpty(name);
12901278

12911279
AssemblyBuilder.CheckContext(returnType);
12921280
AssemblyBuilder.CheckContext(returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes);
@@ -1374,23 +1362,9 @@ private MethodBuilder DefinePInvokeMethodHelper(
13741362

13751363
lock (SyncRoot)
13761364
{
1377-
if (name == null)
1378-
throw new ArgumentNullException(nameof(name));
1379-
1380-
if (name.Length == 0)
1381-
throw new ArgumentException(SR.Argument_EmptyName, nameof(name));
1382-
1383-
if (dllName == null)
1384-
throw new ArgumentNullException(nameof(dllName));
1385-
1386-
if (dllName.Length == 0)
1387-
throw new ArgumentException(SR.Argument_EmptyName, nameof(dllName));
1388-
1389-
if (importName == null)
1390-
throw new ArgumentNullException(nameof(importName));
1391-
1392-
if (importName.Length == 0)
1393-
throw new ArgumentException(SR.Argument_EmptyName, nameof(importName));
1365+
ArgumentException.ThrowIfNullOrEmpty(name);
1366+
ArgumentException.ThrowIfNullOrEmpty(dllName);
1367+
ArgumentException.ThrowIfNullOrEmpty(importName);
13941368

13951369
if ((attributes & MethodAttributes.Abstract) != 0)
13961370
throw new ArgumentException(SR.Argument_BadPInvokeMethod);
@@ -1792,10 +1766,7 @@ private PropertyBuilder DefinePropertyNoLock(string name, PropertyAttributes att
17921766
Type returnType, Type[]? returnTypeRequiredCustomModifiers, Type[]? returnTypeOptionalCustomModifiers,
17931767
Type[]? parameterTypes, Type[][]? parameterTypeRequiredCustomModifiers, Type[][]? parameterTypeOptionalCustomModifiers)
17941768
{
1795-
if (name == null)
1796-
throw new ArgumentNullException(nameof(name));
1797-
if (name.Length == 0)
1798-
throw new ArgumentException(SR.Argument_EmptyName, nameof(name));
1769+
ArgumentException.ThrowIfNullOrEmpty(name);
17991770

18001771
AssemblyBuilder.CheckContext(returnType);
18011772
AssemblyBuilder.CheckContext(returnTypeRequiredCustomModifiers, returnTypeOptionalCustomModifiers, parameterTypes);
@@ -1847,10 +1818,7 @@ public EventBuilder DefineEvent(string name, EventAttributes attributes, Type ev
18471818

18481819
private EventBuilder DefineEventNoLock(string name, EventAttributes attributes, Type eventtype)
18491820
{
1850-
if (name == null)
1851-
throw new ArgumentNullException(nameof(name));
1852-
if (name.Length == 0)
1853-
throw new ArgumentException(SR.Argument_EmptyName, nameof(name));
1821+
ArgumentException.ThrowIfNullOrEmpty(name);
18541822
if (name[0] == '\0')
18551823
throw new ArgumentException(SR.Argument_IllegalName, nameof(name));
18561824

src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,7 @@ private static partial void GetTypeByName(string name, bool throwOnError, bool i
558558

559559
internal static RuntimeType GetTypeByNameUsingCARules(string name, RuntimeModule scope)
560560
{
561-
if (string.IsNullOrEmpty(name))
562-
throw new ArgumentException(null, nameof(name));
561+
ArgumentException.ThrowIfNullOrEmpty(name);
563562

564563
RuntimeType? type = null;
565564
GetTypeByNameUsingCARules(name, new QCallModule(ref scope), ObjectHandleOnStack.Create(ref type));

src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanAndroid.Derive.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ public override byte[] DeriveKeyFromHash(
2323
byte[]? secretPrepend,
2424
byte[]? secretAppend)
2525
{
26-
if (otherPartyPublicKey == null)
27-
throw new ArgumentNullException(nameof(otherPartyPublicKey));
28-
if (string.IsNullOrEmpty(hashAlgorithm.Name))
29-
throw new ArgumentException(SR.Cryptography_HashAlgorithmNameNullOrEmpty, nameof(hashAlgorithm));
26+
ArgumentNullException.ThrowIfNull(otherPartyPublicKey);
27+
ArgumentException.ThrowIfNullOrEmpty(hashAlgorithm.Name, nameof(hashAlgorithm));
3028

3129
ThrowIfDisposed();
3230

@@ -45,10 +43,8 @@ public override byte[] DeriveKeyFromHmac(
4543
byte[]? secretPrepend,
4644
byte[]? secretAppend)
4745
{
48-
if (otherPartyPublicKey == null)
49-
throw new ArgumentNullException(nameof(otherPartyPublicKey));
50-
if (string.IsNullOrEmpty(hashAlgorithm.Name))
51-
throw new ArgumentException(SR.Cryptography_HashAlgorithmNameNullOrEmpty, nameof(hashAlgorithm));
46+
ArgumentNullException.ThrowIfNull(otherPartyPublicKey);
47+
ArgumentException.ThrowIfNullOrEmpty(hashAlgorithm.Name, nameof(hashAlgorithm));
5248

5349
ThrowIfDisposed();
5450

src/libraries/Common/src/System/Security/Cryptography/ECDiffieHellmanCng.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,8 @@ public override byte[] DeriveKeyFromHash(
7676
byte[]? secretPrepend,
7777
byte[]? secretAppend)
7878
{
79-
if (otherPartyPublicKey == null)
80-
throw new ArgumentNullException(nameof(otherPartyPublicKey));
81-
if (string.IsNullOrEmpty(hashAlgorithm.Name))
82-
throw new ArgumentException(SR.Cryptography_HashAlgorithmNameNullOrEmpty, nameof(hashAlgorithm));
79+
ArgumentNullException.ThrowIfNull(otherPartyPublicKey);
80+
ArgumentException.ThrowIfNullOrEmpty(hashAlgorithm.Name, nameof(hashAlgorithm));
8381

8482
using (SafeNCryptSecretHandle secretAgreement = DeriveSecretAgreementHandle(otherPartyPublicKey))
8583
{
@@ -99,10 +97,8 @@ public override byte[] DeriveKeyFromHmac(
9997
byte[]? secretPrepend,
10098
byte[]? secretAppend)
10199
{
102-
if (otherPartyPublicKey == null)
103-
throw new ArgumentNullException(nameof(otherPartyPublicKey));
104-
if (string.IsNullOrEmpty(hashAlgorithm.Name))
105-
throw new ArgumentException(SR.Cryptography_HashAlgorithmNameNullOrEmpty, nameof(hashAlgorithm));
100+
ArgumentNullException.ThrowIfNull(otherPartyPublicKey);
101+
ArgumentException.ThrowIfNullOrEmpty(hashAlgorithm.Name, nameof(hashAlgorithm));
106102

107103
using (SafeNCryptSecretHandle secretAgreement = DeriveSecretAgreementHandle(otherPartyPublicKey))
108104
{

0 commit comments

Comments
 (0)