22// Licensed under the Six Labors Split License.
33
44using System . Diagnostics ;
5+ using System . Diagnostics . CodeAnalysis ;
56using System . Runtime . CompilerServices ;
67
78namespace 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}
0 commit comments