diff --git a/src/SharedInfrastructure/DebugGuard.cs b/src/SharedInfrastructure/DebugGuard.cs
index e79cf8f..34b11c8 100644
--- a/src/SharedInfrastructure/DebugGuard.cs
+++ b/src/SharedInfrastructure/DebugGuard.cs
@@ -2,6 +2,7 @@
// Licensed under the Six Labors Split License.
using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace SixLabors;
@@ -22,33 +23,25 @@ internal static partial class DebugGuard
/// The type of the value.
/// is null.
[Conditional("DEBUG")]
- public static void NotNull(TValue value, string parameterName)
- where TValue : class
- {
- if (value is null)
- {
- ThrowArgumentNullException(parameterName);
- }
- }
+ public static void NotNull([NotNull] TValue? value, [CallerArgumentExpression("value")] string? parameterName = null)
+ where TValue : class =>
+ ArgumentNullException.ThrowIfNull(value, parameterName);
///
/// Ensures that the target value is not null, empty, or whitespace.
///
/// The target string, which should be checked against being null or empty.
- /// Name of the parameter.
+ /// Name of the parameter.
/// is null.
/// is empty or contains only blanks.
[Conditional("DEBUG")]
- public static void NotNullOrWhiteSpace(string value, string parameterName)
+ public static void NotNullOrWhiteSpace([NotNull] string? value, [CallerArgumentExpression("value")] string? paramName = null)
{
- if (value is null)
- {
- ThrowArgumentNullException(parameterName);
- }
+ ArgumentNullException.ThrowIfNull(value);
if (string.IsNullOrWhiteSpace(value))
{
- ThrowArgumentException("Must not be empty or whitespace.", parameterName);
+ ThrowArgumentException("Must not be empty or whitespace.", paramName!);
}
}
@@ -155,7 +148,9 @@ public static void MustBeBetweenOrEqualTo(TValue value, TValue min, TVal
{
if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0)
{
- ThrowArgumentOutOfRangeException(parameterName, $"Value {value} must be greater than or equal to {min} and less than or equal to {max}.");
+ ThrowArgumentOutOfRangeException(
+ parameterName,
+ $"Value {value} must be greater than or equal to {min} and less than or equal to {max}.");
}
}
@@ -282,8 +277,4 @@ private static void ThrowArgumentException(string message, string parameterName)
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ThrowArgumentOutOfRangeException(string parameterName, string message) =>
throw new ArgumentOutOfRangeException(parameterName, message);
-
- [MethodImpl(MethodImplOptions.NoInlining)]
- private static void ThrowArgumentNullException(string parameterName) =>
- throw new ArgumentNullException(parameterName);
}
diff --git a/src/SharedInfrastructure/Guard.cs b/src/SharedInfrastructure/Guard.cs
index 8fa2c9a..7dd3569 100644
--- a/src/SharedInfrastructure/Guard.cs
+++ b/src/SharedInfrastructure/Guard.cs
@@ -2,6 +2,7 @@
// Licensed under the Six Labors Split License.
using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace SixLabors;
@@ -20,16 +21,9 @@ internal static partial class Guard
/// The type of the value.
/// is null.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static void NotNull(TValue value, string parameterName)
- where TValue : class
- {
- if (value is not null)
- {
- return;
- }
-
- ThrowHelper.ThrowArgumentNullExceptionForNotNull(parameterName);
- }
+ public static void NotNull([NotNull]TValue? value, [CallerArgumentExpression("value")] string? parameterName = null)
+ where TValue : class =>
+ ArgumentNullException.ThrowIfNull(value, parameterName);
///
/// Ensures that the target value is not null, empty, or whitespace.
diff --git a/src/SharedInfrastructure/ThrowHelper.cs b/src/SharedInfrastructure/ThrowHelper.cs
index 3516df2..29afa66 100644
--- a/src/SharedInfrastructure/ThrowHelper.cs
+++ b/src/SharedInfrastructure/ThrowHelper.cs
@@ -13,21 +13,13 @@ namespace SixLabors;
internal static partial class ThrowHelper
#pragma warning restore RCS1043 // Remove 'partial' modifier from type with a single part.
{
- ///
- /// Throws an when fails.
- ///
- /// The argument name.
- [DoesNotReturn]
- public static void ThrowArgumentNullExceptionForNotNull(string name)
- => ThrowArgumentNullException(name, $"Parameter \"{name}\" must be not null.");
-
///
/// Throws an when fails.
///
/// The value.
/// The argument name.
[DoesNotReturn]
- public static void ThrowArgumentExceptionForNotNullOrWhitespace(string value, string name)
+ public static void ThrowArgumentExceptionForNotNullOrWhitespace(string? value, string name)
{
if (value is null)
{
diff --git a/tests/SharedInfrastructure.Tests/SharedInfrastructure.Tests.csproj b/tests/SharedInfrastructure.Tests/SharedInfrastructure.Tests.csproj
index 2878748..65dfed4 100644
--- a/tests/SharedInfrastructure.Tests/SharedInfrastructure.Tests.csproj
+++ b/tests/SharedInfrastructure.Tests/SharedInfrastructure.Tests.csproj
@@ -4,6 +4,7 @@
net6.0;
SharedInfrastructure.Tests
SharedInfrastructure.Tests
+ enable