Skip to content

Commit e6176ec

Browse files
committed
Use ArgumentNullException.ThrowIfNull in more places
1 parent 54a9efd commit e6176ec

File tree

21 files changed

+68
-236
lines changed

21 files changed

+68
-236
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,7 @@ public static int GetGeneration(WeakReference wo)
291291
object? obj = GCHandle.InternalGet(wo.WeakHandle);
292292
KeepAlive(wo);
293293

294-
if (obj is null)
295-
{
296-
throw new ArgumentNullException(nameof(wo));
297-
}
294+
ArgumentNullException.ThrowIfNull(obj, nameof(wo));
298295

299296
return GetGeneration(obj);
300297
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,7 @@ private void AddOneArgTypeHelper(Type clsArgument, Type[]? requiredCustomModifie
278278
{
279279
Type t = optionalCustomModifiers[i];
280280

281-
if (t == null)
282-
throw new ArgumentNullException(nameof(optionalCustomModifiers));
281+
ArgumentNullException.ThrowIfNull(t, nameof(optionalCustomModifiers));
283282

284283
if (t.HasElementType)
285284
throw new ArgumentException(SR.Argument_ArraysInvalid, nameof(optionalCustomModifiers));

src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,7 +1890,7 @@ private static object CreateCustomAttributeInstance(RuntimeModule module, Runtim
18901890
{
18911891
if (module is null)
18921892
{
1893-
throw new ArgumentNullException(SR.Arg_InvalidHandle);
1893+
throw new ArgumentNullException(null, SR.Arg_InvalidHandle);
18941894
}
18951895

18961896
object? result = null;
@@ -1920,7 +1920,7 @@ private static void GetPropertyOrFieldData(
19201920
{
19211921
if (module is null)
19221922
{
1923-
throw new ArgumentNullException(SR.Arg_InvalidHandle);
1923+
throw new ArgumentNullException(null, SR.Arg_InvalidHandle);
19241924
}
19251925

19261926
string? nameLocal = null;

src/coreclr/nativeaot/Common/src/System/Collections/Generic/LowLevelDictionary.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ public TValue this[TKey key]
4949
{
5050
get
5151
{
52-
if (key == null)
53-
throw new ArgumentNullException(nameof(key));
52+
ArgumentNullException.ThrowIfNull(key);
5453

5554
Entry entry = Find(key);
5655
if (entry == null)
@@ -59,8 +58,7 @@ public TValue this[TKey key]
5958
}
6059
set
6160
{
62-
if (key == null)
63-
throw new ArgumentNullException(nameof(key));
61+
ArgumentNullException.ThrowIfNull(key);
6462

6563
_version++;
6664
Entry entry = Find(key);
@@ -73,9 +71,9 @@ public TValue this[TKey key]
7371

7472
public bool TryGetValue(TKey key, out TValue? value)
7573
{
74+
ArgumentNullException.ThrowIfNull(key);
75+
7676
value = default(TValue);
77-
if (key == null)
78-
throw new ArgumentNullException(nameof(key));
7977
Entry entry = Find(key);
8078
if (entry != null)
8179
{
@@ -87,8 +85,8 @@ public bool TryGetValue(TKey key, out TValue? value)
8785

8886
public void Add(TKey key, TValue value)
8987
{
90-
if (key == null)
91-
throw new ArgumentNullException(nameof(key));
88+
ArgumentNullException.ThrowIfNull(key);
89+
9290
Entry entry = Find(key);
9391
if (entry != null)
9492
throw new ArgumentException(SR.Format(SR.Argument_AddingDuplicate, key));
@@ -105,8 +103,8 @@ public void Clear(int capacity = DefaultSize)
105103

106104
public bool Remove(TKey key)
107105
{
108-
if (key == null)
109-
throw new ArgumentNullException(nameof(key));
106+
ArgumentNullException.ThrowIfNull(key);
107+
110108
int bucket = GetBucket(key);
111109
Entry? prev = null;
112110
Entry? entry = _buckets[bucket];

src/coreclr/nativeaot/System.Private.CoreLib/src/System/GC.NativeAot.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,7 @@ public static int GetGeneration(WeakReference wo)
111111
object? obj = RuntimeImports.RhHandleGet(wo.WeakHandle);
112112
KeepAlive(wo);
113113

114-
if (obj == null)
115-
{
116-
throw new ArgumentNullException(nameof(wo));
117-
}
114+
ArgumentNullException.ThrowIfNull(obj, nameof(wo));
118115

119116
return RuntimeImports.RhGetGeneration(obj);
120117
}

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/PInvokeMarshal.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,7 @@ public static unsafe void StringBuilderToAnsiString(System.Text.StringBuilder st
348348

349349
public static unsafe void AnsiStringToStringBuilder(byte* newBuffer, System.Text.StringBuilder stringBuilder)
350350
{
351-
if (newBuffer == null)
352-
throw new ArgumentNullException(nameof(newBuffer));
351+
ArgumentNullException.ThrowIfNull(newBuffer);
353352

354353
int lenAnsi;
355354
int lenUnicode;
@@ -435,8 +434,7 @@ public static unsafe void WideCharArrayToAnsiCharArray(char[] managedArray, byte
435434
return;
436435

437436
// Desktop CLR crash (AV at runtime) - we can do better in .NET Native
438-
if (pNative == null)
439-
throw new ArgumentNullException(nameof(pNative));
437+
ArgumentNullException.ThrowIfNull(pNative);
440438

441439
int lenUnicode = managedArray.Length;
442440
fixed (char* pManaged = managedArray)

src/libraries/System.Collections.Concurrent/src/Resources/Strings.resx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@
126126
<data name="ConcurrentDictionary_KeyAlreadyExisted" xml:space="preserve">
127127
<value>The key already existed in the dictionary.</value>
128128
</data>
129-
<data name="ConcurrentDictionary_ItemKeyIsNull" xml:space="preserve">
130-
<value>TKey is a reference type and item.Key is null.</value>
131-
</data>
132129
<data name="ConcurrentDictionary_TypeOfKeyIncorrect" xml:space="preserve">
133130
<value>The key was of an incorrect type for this dictionary.</value>
134131
</data>

0 commit comments

Comments
 (0)