Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ThrowIfNull overload for pointers #61633

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpres
}
}

/// <summary>Throws an <see cref="ArgumentNullException"/> if <paramref name="argument"/> is null.</summary>
/// <param name="argument">The pointer argument to validate as non-null.</param>
/// <param name="paramName">The name of the parameter with which <paramref name="argument"/> corresponds.</param>
[CLSCompliant(false)]
public static unsafe void ThrowIfNull([NotNull] void* argument, [CallerArgumentExpression("argument")] string? paramName = null)
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
{
if (argument is null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just calling it out, because I always forget, x is null does work for pointers today.

{
Throw(paramName);
}
}

[DoesNotReturn]
private static void Throw(string? paramName) =>
throw new ArgumentNullException(paramName);
Expand Down
2 changes: 2 additions & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ public ArgumentNullException(string? paramName) { }
public ArgumentNullException(string? message, System.Exception? innerException) { }
public ArgumentNullException(string? paramName, string? message) { }
public static void ThrowIfNull([System.Diagnostics.CodeAnalysis.NotNullAttribute] object? argument, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute("argument")] string? paramName = null) { throw null; }
[System.CLSCompliant(false)]
public static unsafe void ThrowIfNull([System.Diagnostics.CodeAnalysis.NotNullAttribute] void* argument, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute("argument")] string? paramName = null) { throw null; }
}
public partial class ArgumentOutOfRangeException : System.ArgumentException
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,37 @@ public static void Ctor_String_String()
}

[Fact]
public static void ThrowIfNull_NonNull_DoesntThrow()
public static unsafe void ThrowIfNull_NonNull_DoesntThrow()
{
foreach (object o in new[] { new object(), "", "argument" })
{
ArgumentNullException.ThrowIfNull(o);
ArgumentNullException.ThrowIfNull(o, "paramName");
}

int i = 0;
ArgumentNullException.ThrowIfNull(&i);
ArgumentNullException.ThrowIfNull(&i, "paramName");
}

[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("name")]
public static void ThrowIfNull_Null_ThrowsArgumentNullException(string paramName)
public static unsafe void ThrowIfNull_Null_ThrowsArgumentNullException(string paramName)
{
AssertExtensions.Throws<ArgumentNullException>(paramName, () => ArgumentNullException.ThrowIfNull(null, paramName));
AssertExtensions.Throws<ArgumentNullException>(paramName, () => ArgumentNullException.ThrowIfNull((object)null, paramName));
AssertExtensions.Throws<ArgumentNullException>(paramName, () => ArgumentNullException.ThrowIfNull((void*)null, paramName));
}

[Fact]
public static void ThrowIfNull_UsesArgumentExpression()
public static unsafe void ThrowIfNull_UsesArgumentExpression()
{
object something = null;
AssertExtensions.Throws<ArgumentNullException>(nameof(something), () => ArgumentNullException.ThrowIfNull(something));
object someObject = null;
AssertExtensions.Throws<ArgumentNullException>(nameof(someObject), () => ArgumentNullException.ThrowIfNull(someObject));

byte* somePointer = null;
AssertExtensions.Throws<ArgumentNullException>(nameof(somePointer), () => ArgumentNullException.ThrowIfNull(somePointer));
}
}
}