2
2
// Licensed under the Six Labors Split License.
3
3
4
4
using System . Diagnostics ;
5
+ using System . Diagnostics . CodeAnalysis ;
5
6
using System . Runtime . CompilerServices ;
6
7
7
8
namespace SixLabors ;
@@ -22,33 +23,25 @@ internal static partial class DebugGuard
22
23
/// <typeparam name="TValue">The type of the value.</typeparam>
23
24
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
24
25
[ 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 ) ;
33
29
34
30
/// <summary>
35
31
/// Ensures that the target value is not null, empty, or whitespace.
36
32
/// </summary>
37
33
/// <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>
39
35
/// <exception cref="ArgumentNullException"><paramref name="value"/> is null.</exception>
40
36
/// <exception cref="ArgumentException"><paramref name="value"/> is empty or contains only blanks.</exception>
41
37
[ Conditional ( "DEBUG" ) ]
42
- public static void NotNullOrWhiteSpace ( string value , string parameterName )
38
+ public static void NotNullOrWhiteSpace ( [ NotNull ] string ? value , [ CallerArgumentExpression ( "value" ) ] string ? paramName = null )
43
39
{
44
- if ( value is null )
45
- {
46
- ThrowArgumentNullException ( parameterName ) ;
47
- }
40
+ ArgumentNullException . ThrowIfNull ( value ) ;
48
41
49
42
if ( string . IsNullOrWhiteSpace ( value ) )
50
43
{
51
- ThrowArgumentException ( "Must not be empty or whitespace." , parameterName ) ;
44
+ ThrowArgumentException ( "Must not be empty or whitespace." , paramName ! ) ;
52
45
}
53
46
}
54
47
@@ -155,7 +148,9 @@ public static void MustBeBetweenOrEqualTo<TValue>(TValue value, TValue min, TVal
155
148
{
156
149
if ( value . CompareTo ( min ) < 0 || value . CompareTo ( max ) > 0 )
157
150
{
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 } .") ;
159
154
}
160
155
}
161
156
@@ -282,8 +277,4 @@ private static void ThrowArgumentException(string message, string parameterName)
282
277
[ MethodImpl ( MethodImplOptions . NoInlining ) ]
283
278
private static void ThrowArgumentOutOfRangeException ( string parameterName , string message ) =>
284
279
throw new ArgumentOutOfRangeException ( parameterName , message ) ;
285
-
286
- [ MethodImpl ( MethodImplOptions . NoInlining ) ]
287
- private static void ThrowArgumentNullException ( string parameterName ) =>
288
- throw new ArgumentNullException ( parameterName ) ;
289
280
}
0 commit comments