Skip to content

Commit 9e4fb47

Browse files
committed
Use extension everything to polyfill ArgumentNullException.ThrowIfNull
1 parent d0e6ce8 commit 9e4fb47

File tree

559 files changed

+2191
-4953
lines changed

Some content is hidden

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

559 files changed

+2191
-4953
lines changed

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/MethodInvoker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ internal MethodInvoker(RuntimeConstructorInfo constructor)
2929

3030
public static MethodInvoker Create(MethodBase method)
3131
{
32-
ArgumentNullException.ThrowIfNull(method, nameof(method));
32+
ArgumentNullException.ThrowIfNull(method);
3333

3434
if (method is RuntimeMethodInfo rmi)
3535
{

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.NativeAot.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ internal static unsafe void PtrToStructureImpl(IntPtr ptr, object structure)
9090
public static unsafe void DestroyStructure(IntPtr ptr, Type structuretype)
9191
{
9292
ArgumentNullException.ThrowIfNull(ptr);
93-
ArgumentNullException.ThrowIfNull(structuretype, nameof(structuretype));
93+
ArgumentNullException.ThrowIfNull(structuretype);
9494

9595
RuntimeTypeHandle structureTypeHandle = structuretype.TypeHandle;
9696

src/libraries/Common/src/Extensions/NonCapturingTimer/NonCapturingTimer.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ internal static class NonCapturingTimer
1313
{
1414
public static Timer Create(TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period)
1515
{
16-
if (callback is null)
17-
{
18-
throw new ArgumentNullException(nameof(callback));
19-
}
16+
ArgumentNullException.ThrowIfNull(callback);
2017

2118
// Don't capture the current ExecutionContext and its AsyncLocals onto the timer
2219
bool restoreFlow = false;

src/libraries/Common/src/System/CodeDom/CodeTypeReference.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,7 @@ public CodeTypeReference()
4242

4343
public CodeTypeReference(Type type)
4444
{
45-
#if NET
4645
ArgumentNullException.ThrowIfNull(type);
47-
#else
48-
if (type is null)
49-
{
50-
throw new ArgumentNullException(nameof(type));
51-
}
52-
#endif
5346

5447
if (type.IsArray)
5548
{

src/libraries/Common/src/System/CodeDom/CodeTypeReferenceCollection.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,7 @@ public CodeTypeReference this[int index]
4141

4242
public void AddRange(CodeTypeReference[] value)
4343
{
44-
#if NET
4544
ArgumentNullException.ThrowIfNull(value);
46-
#else
47-
if (value is null)
48-
{
49-
throw new ArgumentNullException(nameof(value));
50-
}
51-
#endif
5245

5346
for (int i = 0; i < value.Length; i++)
5447
{
@@ -58,14 +51,7 @@ public void AddRange(CodeTypeReference[] value)
5851

5952
public void AddRange(CodeTypeReferenceCollection value)
6053
{
61-
#if NET
6254
ArgumentNullException.ThrowIfNull(value);
63-
#else
64-
if (value is null)
65-
{
66-
throw new ArgumentNullException(nameof(value));
67-
}
68-
#endif
6955

7056
int currentCount = value.Count;
7157
for (int i = 0; i < currentCount; i++)

src/libraries/Common/src/System/Composition/Diagnostics/CompositionTrace.cs

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,8 @@ public static void Registration_TypeExportConventionOverridden(Type type)
3535

3636
public static void Registration_MemberExportConventionOverridden(Type type, MemberInfo member)
3737
{
38-
if (type is null)
39-
{
40-
throw new ArgumentNullException(nameof(type));
41-
}
42-
if (member is null)
43-
{
44-
throw new ArgumentNullException(nameof(member));
45-
}
38+
ArgumentNullException.ThrowIfNull(type);
39+
ArgumentNullException.ThrowIfNull(member);
4640

4741
if (CompositionTraceSource.CanWriteWarning)
4842
{
@@ -54,14 +48,8 @@ public static void Registration_MemberExportConventionOverridden(Type type, Memb
5448

5549
public static void Registration_MemberImportConventionOverridden(Type type, MemberInfo member)
5650
{
57-
if (type is null)
58-
{
59-
throw new ArgumentNullException(nameof(type));
60-
}
61-
if (member is null)
62-
{
63-
throw new ArgumentNullException(nameof(member));
64-
}
51+
ArgumentNullException.ThrowIfNull(type);
52+
ArgumentNullException.ThrowIfNull(member);
6553

6654
if (CompositionTraceSource.CanWriteWarning)
6755
{
@@ -73,14 +61,8 @@ public static void Registration_MemberImportConventionOverridden(Type type, Memb
7361

7462
public static void Registration_OnSatisfiedImportNotificationOverridden(Type type, MemberInfo member)
7563
{
76-
if (type is null)
77-
{
78-
throw new ArgumentNullException(nameof(type));
79-
}
80-
if (member is null)
81-
{
82-
throw new ArgumentNullException(nameof(member));
83-
}
64+
ArgumentNullException.ThrowIfNull(type);
65+
ArgumentNullException.ThrowIfNull(member);
8466

8567
if (CompositionTraceSource.CanWriteWarning)
8668
{
@@ -92,10 +74,7 @@ public static void Registration_OnSatisfiedImportNotificationOverridden(Type typ
9274

9375
public static void Registration_PartCreationConventionOverridden(Type type)
9476
{
95-
if (type is null)
96-
{
97-
throw new ArgumentNullException(nameof(type));
98-
}
77+
ArgumentNullException.ThrowIfNull(type);
9978

10079
if (CompositionTraceSource.CanWriteWarning)
10180
{
@@ -107,14 +86,8 @@ public static void Registration_PartCreationConventionOverridden(Type type)
10786

10887
public static void Registration_MemberImportConventionMatchedTwice(Type type, MemberInfo member)
10988
{
110-
if (type is null)
111-
{
112-
throw new ArgumentNullException(nameof(type));
113-
}
114-
if (member is null)
115-
{
116-
throw new ArgumentNullException(nameof(member));
117-
}
89+
ArgumentNullException.ThrowIfNull(type);
90+
ArgumentNullException.ThrowIfNull(member);
11891

11992
if (CompositionTraceSource.CanWriteWarning)
12093
{
@@ -126,10 +99,7 @@ public static void Registration_MemberImportConventionMatchedTwice(Type type, Me
12699

127100
public static void Registration_PartMetadataConventionOverridden(Type type)
128101
{
129-
if (type is null)
130-
{
131-
throw new ArgumentNullException(nameof(type));
132-
}
102+
ArgumentNullException.ThrowIfNull(type);
133103

134104
if (CompositionTraceSource.CanWriteWarning)
135105
{
@@ -141,14 +111,8 @@ public static void Registration_PartMetadataConventionOverridden(Type type)
141111

142112
public static void Registration_ParameterImportConventionOverridden(ParameterInfo parameter, ConstructorInfo constructor)
143113
{
144-
if (parameter is null)
145-
{
146-
throw new ArgumentNullException(nameof(parameter));
147-
}
148-
if (constructor is null)
149-
{
150-
throw new ArgumentNullException(nameof(constructor));
151-
}
114+
ArgumentNullException.ThrowIfNull(parameter);
115+
ArgumentNullException.ThrowIfNull(constructor);
152116

153117
if (CompositionTraceSource.CanWriteWarning)
154118
{
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Diagnostics.CodeAnalysis;
5+
using System.Runtime.CompilerServices;
6+
7+
namespace System
8+
{
9+
/// <summary>Provides downlevel polyfills for static methods on Exception-derived types.</summary>
10+
internal static class ExceptionPolyfills
11+
{
12+
extension(ArgumentNullException)
13+
{
14+
public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
15+
{
16+
if (argument is null)
17+
{
18+
ThrowArgumentNullException(paramName);
19+
}
20+
}
21+
}
22+
23+
[DoesNotReturn]
24+
private static void ThrowArgumentNullException(string? paramName) =>
25+
throw new ArgumentNullException(paramName);
26+
}
27+
}

src/libraries/Common/src/System/IO/ReadOnlyMemoryStream.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,7 @@ protected override void Dispose(bool disposing)
198198
#if NETFRAMEWORK || NETSTANDARD2_0
199199
private static void ValidateBufferArguments(byte[] buffer, int offset, int count)
200200
{
201-
if (buffer is null)
202-
{
203-
throw new ArgumentNullException(nameof(buffer));
204-
}
201+
ArgumentNullException.ThrowIfNull(buffer);
205202

206203
if (offset < 0)
207204
{

src/libraries/Common/src/System/IO/StreamHelpers.CopyValidation.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ internal static partial class StreamHelpers
99
/// <summary>Validate the arguments to CopyTo, as would Stream.CopyTo.</summary>
1010
public static void ValidateCopyToArgs(Stream source, Stream destination, int bufferSize)
1111
{
12-
if (destination is null)
13-
{
14-
throw new ArgumentNullException(nameof(destination));
15-
}
12+
ArgumentNullException.ThrowIfNull(destination);
1613

1714
if (bufferSize <= 0)
1815
{

src/libraries/Common/src/System/Resources/ResourceWriter.cs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,7 @@ public sealed partial class
4848
ResourceWriter(string fileName)
4949
#endif
5050
{
51-
if (fileName is null)
52-
{
53-
throw new ArgumentNullException(nameof(fileName));
54-
}
51+
ArgumentNullException.ThrowIfNull(fileName);
5552

5653
_output = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
5754
_resourceList = new SortedDictionary<string, object?>(FastResourceComparer.Default);
@@ -65,10 +62,7 @@ public sealed partial class
6562
ResourceWriter(Stream stream)
6663
#endif
6764
{
68-
if (stream is null)
69-
{
70-
throw new ArgumentNullException(nameof(stream));
71-
}
65+
ArgumentNullException.ThrowIfNull(stream);
7266

7367
if (!stream.CanWrite)
7468
{
@@ -85,10 +79,7 @@ public sealed partial class
8579
//
8680
public void AddResource(string name, string? value)
8781
{
88-
if (name is null)
89-
{
90-
throw new ArgumentNullException(nameof(name));
91-
}
82+
ArgumentNullException.ThrowIfNull(name);
9283

9384
if (_resourceList == null)
9485
{
@@ -105,10 +96,7 @@ public void AddResource(string name, string? value)
10596
//
10697
public void AddResource(string name, object? value)
10798
{
108-
if (name is null)
109-
{
110-
throw new ArgumentNullException(nameof(name));
111-
}
99+
ArgumentNullException.ThrowIfNull(name);
112100

113101
if (_resourceList == null)
114102
{
@@ -134,10 +122,7 @@ public void AddResource(string name, object? value)
134122
//
135123
public void AddResource(string name, Stream? value, bool closeAfterWrite = false)
136124
{
137-
if (name is null)
138-
{
139-
throw new ArgumentNullException(nameof(name));
140-
}
125+
ArgumentNullException.ThrowIfNull(name);
141126

142127
if (_resourceList == null)
143128
{
@@ -174,10 +159,7 @@ private void AddResourceInternal(string name, Stream? value, bool closeAfterWrit
174159
//
175160
public void AddResource(string name, byte[]? value)
176161
{
177-
if (name is null)
178-
{
179-
throw new ArgumentNullException(nameof(name));
180-
}
162+
ArgumentNullException.ThrowIfNull(name);
181163

182164
if (_resourceList == null)
183165
{

0 commit comments

Comments
 (0)