Skip to content
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
Add ThrowIfNull overload for pointers
  • Loading branch information
stephentoub committed Nov 15, 2021
commit 64463fe9319c0f578736622c9dda7f80410d1226
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)
{
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));
}
}
}