Skip to content

Commit 5dff99f

Browse files
authored
[shared] Use nameof for CallerArgumentExpression in Guard helpers (#5917)
1 parent 25a9366 commit 5dff99f

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/Shared/Guard.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ internal static class Guard
5555
/// <param name="paramName">The parameter name to use in the thrown exception.</param>
5656
[DebuggerHidden]
5757
[MethodImpl(MethodImplOptions.AggressiveInlining)]
58-
public static void ThrowIfNull([NotNull] object? value, [CallerArgumentExpression("value")] string? paramName = null)
58+
public static void ThrowIfNull([NotNull] object? value, [CallerArgumentExpression(nameof(value))] string? paramName = null)
5959
{
6060
if (value is null)
6161
{
@@ -70,7 +70,7 @@ public static void ThrowIfNull([NotNull] object? value, [CallerArgumentExpressio
7070
/// <param name="paramName">The parameter name to use in the thrown exception.</param>
7171
[DebuggerHidden]
7272
[MethodImpl(MethodImplOptions.AggressiveInlining)]
73-
public static void ThrowIfNullOrEmpty([NotNull] string? value, [CallerArgumentExpression("value")] string? paramName = null)
73+
public static void ThrowIfNullOrEmpty([NotNull] string? value, [CallerArgumentExpression(nameof(value))] string? paramName = null)
7474
#pragma warning disable CS8777 // Parameter must have a non-null value when exiting.
7575
{
7676
if (string.IsNullOrEmpty(value))
@@ -87,7 +87,7 @@ public static void ThrowIfNullOrEmpty([NotNull] string? value, [CallerArgumentEx
8787
/// <param name="paramName">The parameter name to use in the thrown exception.</param>
8888
[DebuggerHidden]
8989
[MethodImpl(MethodImplOptions.AggressiveInlining)]
90-
public static void ThrowIfNullOrWhitespace([NotNull] string? value, [CallerArgumentExpression("value")] string? paramName = null)
90+
public static void ThrowIfNullOrWhitespace([NotNull] string? value, [CallerArgumentExpression(nameof(value))] string? paramName = null)
9191
#pragma warning disable CS8777 // Parameter must have a non-null value when exiting.
9292
{
9393
if (string.IsNullOrWhiteSpace(value))
@@ -105,7 +105,7 @@ public static void ThrowIfNullOrWhitespace([NotNull] string? value, [CallerArgum
105105
/// <param name="paramName">The parameter name to use in the thrown exception.</param>
106106
[DebuggerHidden]
107107
[MethodImpl(MethodImplOptions.AggressiveInlining)]
108-
public static void ThrowIfZero(int value, string message = "Must not be zero", [CallerArgumentExpression("value")] string? paramName = null)
108+
public static void ThrowIfZero(int value, string message = "Must not be zero", [CallerArgumentExpression(nameof(value))] string? paramName = null)
109109
{
110110
if (value == 0)
111111
{
@@ -120,7 +120,7 @@ public static void ThrowIfZero(int value, string message = "Must not be zero", [
120120
/// <param name="paramName">The parameter name to use in the thrown exception.</param>
121121
[DebuggerHidden]
122122
[MethodImpl(MethodImplOptions.AggressiveInlining)]
123-
public static void ThrowIfInvalidTimeout(int value, [CallerArgumentExpression("value")] string? paramName = null)
123+
public static void ThrowIfInvalidTimeout(int value, [CallerArgumentExpression(nameof(value))] string? paramName = null)
124124
{
125125
ThrowIfOutOfRange(value, paramName, min: Timeout.Infinite, message: $"Must be non-negative or '{nameof(Timeout)}.{nameof(Timeout.Infinite)}'");
126126
}
@@ -137,7 +137,7 @@ public static void ThrowIfInvalidTimeout(int value, [CallerArgumentExpression("v
137137
/// <param name="message">An optional custom message to use in the thrown exception.</param>
138138
[DebuggerHidden]
139139
[MethodImpl(MethodImplOptions.AggressiveInlining)]
140-
public static void ThrowIfOutOfRange(int value, [CallerArgumentExpression("value")] string? paramName = null, int min = int.MinValue, int max = int.MaxValue, string? minName = null, string? maxName = null, string? message = null)
140+
public static void ThrowIfOutOfRange(int value, [CallerArgumentExpression(nameof(value))] string? paramName = null, int min = int.MinValue, int max = int.MaxValue, string? minName = null, string? maxName = null, string? message = null)
141141
{
142142
Range(value, paramName, min, max, minName, maxName, message);
143143
}
@@ -154,7 +154,7 @@ public static void ThrowIfOutOfRange(int value, [CallerArgumentExpression("value
154154
/// <param name="message">An optional custom message to use in the thrown exception.</param>
155155
[DebuggerHidden]
156156
[MethodImpl(MethodImplOptions.AggressiveInlining)]
157-
public static void ThrowIfOutOfRange(double value, [CallerArgumentExpression("value")] string? paramName = null, double min = double.MinValue, double max = double.MaxValue, string? minName = null, string? maxName = null, string? message = null)
157+
public static void ThrowIfOutOfRange(double value, [CallerArgumentExpression(nameof(value))] string? paramName = null, double min = double.MinValue, double max = double.MaxValue, string? minName = null, string? maxName = null, string? message = null)
158158
{
159159
Range(value, paramName, min, max, minName, maxName, message);
160160
}
@@ -168,7 +168,7 @@ public static void ThrowIfOutOfRange(double value, [CallerArgumentExpression("va
168168
/// <returns>The value casted to the specified type.</returns>
169169
[DebuggerHidden]
170170
[MethodImpl(MethodImplOptions.AggressiveInlining)]
171-
public static T ThrowIfNotOfType<T>([NotNull] object? value, [CallerArgumentExpression("value")] string? paramName = null)
171+
public static T ThrowIfNotOfType<T>([NotNull] object? value, [CallerArgumentExpression(nameof(value))] string? paramName = null)
172172
{
173173
if (value is not T result)
174174
{

0 commit comments

Comments
 (0)