You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was surprised that the compiler gives me an error when replacing throw new ArgumentOutOfRangeException(nameof(x));
with CommunityToolkit.Diagnostics.ThrowHelper.ThrowArgumentOutOfRangeException(nameof(x));
where the helper is implemented as:
[DoesNotReturn]
public static void ThrowArgumentOutOfRangeException(string? name)
{
throw new ArgumentOutOfRangeException(name);
}
For example
int ThisFunctionMayNotReturnAndDoesNotCauseACompilationError(int x)
{
if (x > 100) { return 99; }
throw new ArgumentOutOfRangeException(nameof(x));
}
// we get CS0161 here
int ThisFunctionMayNotReturnAndDoesCauseACompilationError(int x)
{
if (x > 100) { return 99; }
CommunityToolkit.Diagnostics.ThrowHelper.ThrowArgumentOutOfRangeException(nameof(x));
}
I initially thought this was a bug, but then realised that [DoesNotReturn] only applies to nullability analysis.
It would be useful if static code analysis took DoesNotReturn into account so we don't have to add redundant code when using throw helpers
int ThisFunctionMayNotReturnAndDoesCauseACompilationError(int x)
{
if (x > 100) { return 99; }
CommunityToolkit.Diagnostics.ThrowHelper.ThrowArgumentOutOfRangeException(nameof(x));
// stop compiler complaining
return 0;
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I was surprised that the compiler gives me an error when replacing
throw new ArgumentOutOfRangeException(nameof(x));
with
CommunityToolkit.Diagnostics.ThrowHelper.ThrowArgumentOutOfRangeException(nameof(x));
where the helper is implemented as:
For example
I initially thought this was a bug, but then realised that
[DoesNotReturn]
only applies to nullability analysis.It would be useful if static code analysis took
DoesNotReturn
into account so we don't have to add redundant code when using throw helpersBeta Was this translation helpful? Give feedback.
All reactions