Skip to content

Commit 9a82679

Browse files
Merge pull request #34 from stefannikolei/nikoleis/nrt
NRT
2 parents bf398a9 + 28bfbea commit 9a82679

File tree

4 files changed

+17
-39
lines changed

4 files changed

+17
-39
lines changed

src/SharedInfrastructure/DebugGuard.cs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Six Labors Split License.
33

44
using System.Diagnostics;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Runtime.CompilerServices;
67

78
namespace SixLabors;
@@ -22,33 +23,25 @@ internal static partial class DebugGuard
2223
/// <typeparam name="TValue">The type of the value.</typeparam>
2324
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
2425
[Conditional("DEBUG")]
25-
public static void NotNull<TValue>(TValue value, string parameterName)
26-
where TValue : class
27-
{
28-
if (value is null)
29-
{
30-
ThrowArgumentNullException(parameterName);
31-
}
32-
}
26+
public static void NotNull<TValue>([NotNull] TValue? value, [CallerArgumentExpression("value")] string? parameterName = null)
27+
where TValue : class =>
28+
ArgumentNullException.ThrowIfNull(value, parameterName);
3329

3430
/// <summary>
3531
/// Ensures that the target value is not null, empty, or whitespace.
3632
/// </summary>
3733
/// <param name="value">The target string, which should be checked against being null or empty.</param>
38-
/// <param name="parameterName">Name of the parameter.</param>
34+
/// <param name="paramName">Name of the parameter.</param>
3935
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
4036
/// <exception cref="ArgumentException"><paramref name="value"/> is empty or contains only blanks.</exception>
4137
[Conditional("DEBUG")]
42-
public static void NotNullOrWhiteSpace(string value, string parameterName)
38+
public static void NotNullOrWhiteSpace([NotNull] string? value, [CallerArgumentExpression("value")] string? paramName = null)
4339
{
44-
if (value is null)
45-
{
46-
ThrowArgumentNullException(parameterName);
47-
}
40+
ArgumentNullException.ThrowIfNull(value);
4841

4942
if (string.IsNullOrWhiteSpace(value))
5043
{
51-
ThrowArgumentException("Must not be empty or whitespace.", parameterName);
44+
ThrowArgumentException("Must not be empty or whitespace.", paramName!);
5245
}
5346
}
5447

@@ -155,7 +148,9 @@ public static void MustBeBetweenOrEqualTo<TValue>(TValue value, TValue min, TVal
155148
{
156149
if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0)
157150
{
158-
ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than or equal to {min} and less than or equal to {max}.");
151+
ThrowArgumentOutOfRangeException(
152+
parameterName,
153+
$"Value {value} must be greater than or equal to {min} and less than or equal to {max}.");
159154
}
160155
}
161156

@@ -282,8 +277,4 @@ private static void ThrowArgumentException(string message, string parameterName)
282277
[MethodImpl(MethodImplOptions.NoInlining)]
283278
private static void ThrowArgumentOutOfRangeException(string parameterName, string message) =>
284279
throw new ArgumentOutOfRangeException(parameterName, message);
285-
286-
[MethodImpl(MethodImplOptions.NoInlining)]
287-
private static void ThrowArgumentNullException(string parameterName) =>
288-
throw new ArgumentNullException(parameterName);
289280
}

src/SharedInfrastructure/Guard.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Six Labors Split License.
33

44
using System.Diagnostics;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Runtime.CompilerServices;
67

78
namespace SixLabors;
@@ -20,16 +21,9 @@ internal static partial class Guard
2021
/// <typeparam name="TValue">The type of the value.</typeparam>
2122
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
2223
[MethodImpl(MethodImplOptions.AggressiveInlining)]
23-
public static void NotNull<TValue>(TValue value, string parameterName)
24-
where TValue : class
25-
{
26-
if (value is not null)
27-
{
28-
return;
29-
}
30-
31-
ThrowHelper.ThrowArgumentNullExceptionForNotNull(parameterName);
32-
}
24+
public static void NotNull<TValue>([NotNull]TValue? value, [CallerArgumentExpression("value")] string? parameterName = null)
25+
where TValue : class =>
26+
ArgumentNullException.ThrowIfNull(value, parameterName);
3327

3428
/// <summary>
3529
/// Ensures that the target value is not null, empty, or whitespace.

src/SharedInfrastructure/ThrowHelper.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,13 @@ namespace SixLabors;
1313
internal static partial class ThrowHelper
1414
#pragma warning restore RCS1043 // Remove 'partial' modifier from type with a single part.
1515
{
16-
/// <summary>
17-
/// Throws an <see cref="ArgumentNullException"/> when <see cref="Guard.NotNull{TValue}"/> fails.
18-
/// </summary>
19-
/// <param name="name">The argument name.</param>
20-
[DoesNotReturn]
21-
public static void ThrowArgumentNullExceptionForNotNull(string name)
22-
=> ThrowArgumentNullException(name, $"Parameter \"{name}\" must be not null.");
23-
2416
/// <summary>
2517
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.NotNullOrWhiteSpace"/> fails.
2618
/// </summary>
2719
/// <param name="value">The value.</param>
2820
/// <param name="name">The argument name.</param>
2921
[DoesNotReturn]
30-
public static void ThrowArgumentExceptionForNotNullOrWhitespace(string value, string name)
22+
public static void ThrowArgumentExceptionForNotNullOrWhitespace(string? value, string name)
3123
{
3224
if (value is null)
3325
{

tests/SharedInfrastructure.Tests/SharedInfrastructure.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFrameworks>net6.0;</TargetFrameworks>
55
<AssemblyName>SharedInfrastructure.Tests</AssemblyName>
66
<RootNamespace>SharedInfrastructure.Tests</RootNamespace>
7+
<Nullable>enable</Nullable>
78
</PropertyGroup>
89

910
<ItemGroup>

0 commit comments

Comments
 (0)