Skip to content

Commit 2c26991

Browse files
committed
Add CallerArgumentExpression and NotNull attributes
* Added CallerArgumentExpression to auto infer paramName * Added NotNull Attribute to tell the compiler that the parameter is not null when the method return * Removed unused method * Add nullable to the test project
1 parent 47ff841 commit 2c26991

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

src/SharedInfrastructure/DebugGuard.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,25 @@ internal static partial class DebugGuard
2323
/// <typeparam name="TValue">The type of the value.</typeparam>
2424
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
2525
[Conditional("DEBUG")]
26-
public static void NotNull<TValue>([NotNull] TValue? value, string parameterName)
26+
public static void NotNull<TValue>([NotNull] TValue? value, [CallerArgumentExpression("value")] string? parameterName = null)
2727
where TValue : class =>
28-
ArgumentNullException.ThrowIfNull(value,parameterName);
28+
ArgumentNullException.ThrowIfNull(value, parameterName);
2929

3030
/// <summary>
3131
/// Ensures that the target value is not null, empty, or whitespace.
3232
/// </summary>
3333
/// <param name="value">The target string, which should be checked against being null or empty.</param>
34-
/// <param name="parameterName">Name of the parameter.</param>
34+
/// <param name="paramName">Name of the parameter.</param>
3535
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
3636
/// <exception cref="ArgumentException"><paramref name="value"/> is empty or contains only blanks.</exception>
3737
[Conditional("DEBUG")]
38-
public static void NotNullOrWhiteSpace(string value, string parameterName)
38+
public static void NotNullOrWhiteSpace([NotNull] string? value, [CallerArgumentExpression("value")] string? paramName = null)
3939
{
40-
if (value is null)
41-
{
42-
ThrowArgumentNullException(parameterName);
43-
}
40+
ArgumentNullException.ThrowIfNull(value);
4441

4542
if (string.IsNullOrWhiteSpace(value))
4643
{
47-
ThrowArgumentException("Must not be empty or whitespace.", parameterName);
44+
ThrowArgumentException("Must not be empty or whitespace.", paramName!);
4845
}
4946
}
5047

@@ -151,7 +148,9 @@ public static void MustBeBetweenOrEqualTo<TValue>(TValue value, TValue min, TVal
151148
{
152149
if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0)
153150
{
154-
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}.");
155154
}
156155
}
157156

@@ -278,8 +277,4 @@ private static void ThrowArgumentException(string message, string parameterName)
278277
[MethodImpl(MethodImplOptions.NoInlining)]
279278
private static void ThrowArgumentOutOfRangeException(string parameterName, string message) =>
280279
throw new ArgumentOutOfRangeException(parameterName, message);
281-
282-
[MethodImpl(MethodImplOptions.NoInlining)]
283-
private static void ThrowArgumentNullException(string parameterName) =>
284-
throw new ArgumentNullException(parameterName);
285280
}

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)