Skip to content

Commit

Permalink
Use ArgumentNullException.ThrowIfNull in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed Jul 25, 2024
1 parent 54a9efd commit e6176ec
Show file tree
Hide file tree
Showing 21 changed files with 68 additions and 236 deletions.
5 changes: 1 addition & 4 deletions src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,7 @@ public static int GetGeneration(WeakReference wo)
object? obj = GCHandle.InternalGet(wo.WeakHandle);
KeepAlive(wo);

if (obj is null)
{
throw new ArgumentNullException(nameof(wo));
}
ArgumentNullException.ThrowIfNull(obj, nameof(wo));

return GetGeneration(obj);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,7 @@ private void AddOneArgTypeHelper(Type clsArgument, Type[]? requiredCustomModifie
{
Type t = optionalCustomModifiers[i];

if (t == null)
throw new ArgumentNullException(nameof(optionalCustomModifiers));
ArgumentNullException.ThrowIfNull(t, nameof(optionalCustomModifiers));

if (t.HasElementType)
throw new ArgumentException(SR.Argument_ArraysInvalid, nameof(optionalCustomModifiers));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1890,7 +1890,7 @@ private static object CreateCustomAttributeInstance(RuntimeModule module, Runtim
{
if (module is null)
{
throw new ArgumentNullException(SR.Arg_InvalidHandle);
throw new ArgumentNullException(null, SR.Arg_InvalidHandle);
}

object? result = null;
Expand Down Expand Up @@ -1920,7 +1920,7 @@ private static void GetPropertyOrFieldData(
{
if (module is null)
{
throw new ArgumentNullException(SR.Arg_InvalidHandle);
throw new ArgumentNullException(null, SR.Arg_InvalidHandle);
}

string? nameLocal = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public TValue this[TKey key]
{
get
{
if (key == null)
throw new ArgumentNullException(nameof(key));
ArgumentNullException.ThrowIfNull(key);

Entry entry = Find(key);
if (entry == null)
Expand All @@ -59,8 +58,7 @@ public TValue this[TKey key]
}
set
{
if (key == null)
throw new ArgumentNullException(nameof(key));
ArgumentNullException.ThrowIfNull(key);

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

public bool TryGetValue(TKey key, out TValue? value)
{
ArgumentNullException.ThrowIfNull(key);

value = default(TValue);
if (key == null)
throw new ArgumentNullException(nameof(key));
Entry entry = Find(key);
if (entry != null)
{
Expand All @@ -87,8 +85,8 @@ public bool TryGetValue(TKey key, out TValue? value)

public void Add(TKey key, TValue value)
{
if (key == null)
throw new ArgumentNullException(nameof(key));
ArgumentNullException.ThrowIfNull(key);

Entry entry = Find(key);
if (entry != null)
throw new ArgumentException(SR.Format(SR.Argument_AddingDuplicate, key));
Expand All @@ -105,8 +103,8 @@ public void Clear(int capacity = DefaultSize)

public bool Remove(TKey key)
{
if (key == null)
throw new ArgumentNullException(nameof(key));
ArgumentNullException.ThrowIfNull(key);

int bucket = GetBucket(key);
Entry? prev = null;
Entry? entry = _buckets[bucket];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,7 @@ public static int GetGeneration(WeakReference wo)
object? obj = RuntimeImports.RhHandleGet(wo.WeakHandle);
KeepAlive(wo);

if (obj == null)
{
throw new ArgumentNullException(nameof(wo));
}
ArgumentNullException.ThrowIfNull(obj, nameof(wo));

return RuntimeImports.RhGetGeneration(obj);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,7 @@ public static unsafe void StringBuilderToAnsiString(System.Text.StringBuilder st

public static unsafe void AnsiStringToStringBuilder(byte* newBuffer, System.Text.StringBuilder stringBuilder)
{
if (newBuffer == null)
throw new ArgumentNullException(nameof(newBuffer));
ArgumentNullException.ThrowIfNull(newBuffer);

int lenAnsi;
int lenUnicode;
Expand Down Expand Up @@ -435,8 +434,7 @@ public static unsafe void WideCharArrayToAnsiCharArray(char[] managedArray, byte
return;

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

int lenUnicode = managedArray.Length;
fixed (char* pManaged = managedArray)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@
<data name="ConcurrentDictionary_KeyAlreadyExisted" xml:space="preserve">
<value>The key already existed in the dictionary.</value>
</data>
<data name="ConcurrentDictionary_ItemKeyIsNull" xml:space="preserve">
<value>TKey is a reference type and item.Key is null.</value>
</data>
<data name="ConcurrentDictionary_TypeOfKeyIncorrect" xml:space="preserve">
<value>The key was of an incorrect type for this dictionary.</value>
</data>
Expand Down
Loading

0 comments on commit e6176ec

Please sign in to comment.