Skip to content

Commit b11bda2

Browse files
authored
Add ThrowIfNull overload for pointers (#61633)
1 parent 6ff57f1 commit b11bda2

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

src/libraries/System.Private.CoreLib/src/System/ArgumentNullException.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpres
6464
}
6565
}
6666

67+
/// <summary>Throws an <see cref="ArgumentNullException"/> if <paramref name="argument"/> is null.</summary>
68+
/// <param name="argument">The pointer argument to validate as non-null.</param>
69+
/// <param name="paramName">The name of the parameter with which <paramref name="argument"/> corresponds.</param>
70+
[CLSCompliant(false)]
71+
public static unsafe void ThrowIfNull([NotNull] void* argument, [CallerArgumentExpression("argument")] string? paramName = null)
72+
{
73+
if (argument is null)
74+
{
75+
Throw(paramName);
76+
}
77+
}
78+
6779
[DoesNotReturn]
6880
private static void Throw(string? paramName) =>
6981
throw new ArgumentNullException(paramName);

src/libraries/System.Runtime/ref/System.Runtime.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ public ArgumentNullException(string? paramName) { }
285285
public ArgumentNullException(string? message, System.Exception? innerException) { }
286286
public ArgumentNullException(string? paramName, string? message) { }
287287
public static void ThrowIfNull([System.Diagnostics.CodeAnalysis.NotNullAttribute] object? argument, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute("argument")] string? paramName = null) { throw null; }
288+
[System.CLSCompliant(false)]
289+
public static unsafe void ThrowIfNull([System.Diagnostics.CodeAnalysis.NotNullAttribute] void* argument, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute("argument")] string? paramName = null) { throw null; }
288290
}
289291
public partial class ArgumentOutOfRangeException : System.ArgumentException
290292
{

src/libraries/System.Runtime/tests/System/ArgumentNullExceptionTests.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,37 @@ public static void Ctor_String_String()
4949
}
5050

5151
[Fact]
52-
public static void ThrowIfNull_NonNull_DoesntThrow()
52+
public static unsafe void ThrowIfNull_NonNull_DoesntThrow()
5353
{
5454
foreach (object o in new[] { new object(), "", "argument" })
5555
{
5656
ArgumentNullException.ThrowIfNull(o);
5757
ArgumentNullException.ThrowIfNull(o, "paramName");
5858
}
59+
60+
int i = 0;
61+
ArgumentNullException.ThrowIfNull(&i);
62+
ArgumentNullException.ThrowIfNull(&i, "paramName");
5963
}
6064

6165
[Theory]
6266
[InlineData(null)]
6367
[InlineData("")]
6468
[InlineData("name")]
65-
public static void ThrowIfNull_Null_ThrowsArgumentNullException(string paramName)
69+
public static unsafe void ThrowIfNull_Null_ThrowsArgumentNullException(string paramName)
6670
{
67-
AssertExtensions.Throws<ArgumentNullException>(paramName, () => ArgumentNullException.ThrowIfNull(null, paramName));
71+
AssertExtensions.Throws<ArgumentNullException>(paramName, () => ArgumentNullException.ThrowIfNull((object)null, paramName));
72+
AssertExtensions.Throws<ArgumentNullException>(paramName, () => ArgumentNullException.ThrowIfNull((void*)null, paramName));
6873
}
6974

7075
[Fact]
71-
public static void ThrowIfNull_UsesArgumentExpression()
76+
public static unsafe void ThrowIfNull_UsesArgumentExpression()
7277
{
73-
object something = null;
74-
AssertExtensions.Throws<ArgumentNullException>(nameof(something), () => ArgumentNullException.ThrowIfNull(something));
78+
object someObject = null;
79+
AssertExtensions.Throws<ArgumentNullException>(nameof(someObject), () => ArgumentNullException.ThrowIfNull(someObject));
80+
81+
byte* somePointer = null;
82+
AssertExtensions.Throws<ArgumentNullException>(nameof(somePointer), () => ArgumentNullException.ThrowIfNull(somePointer));
7583
}
7684
}
7785
}

0 commit comments

Comments
 (0)