Remove CS1717 warning after not null assertion #46583
-
Version Used: Steps to Reproduce:
If the developer chooses to use the suppression (bang) operator on the first line after the first assertion, the CS1717 warning will be removed, but if future refactoring causes reordering of the lines, the operator might have to be placed on the first line again, or, annoyingly, on all lines, to ensure that if lines are reordered they don't start getting the warning. Expected Behavior: Ideally, Roslyn should understand that a not null assertion is equivalent to throwing an exception if null. Else, it should understand that the suppression operator added to such a line should affect all lines below. Actual Behavior: Roslyn does not understand that the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Re: a It looks like Nunit is unable to put using System.Diagnostics.CodeAnalysis;
void AssertNotNull([NotNull] object? arg)
{
Assert.NotNull(arg);
if (arg is null)
{
throw new ArgumentNullException(nameof(arg), "Unexpected call from Assert.Multiple context");
}
} For some time Roslyn introduced |
Beta Was this translation helpful? Give feedback.
-
Also, CS1717 is for "assignment to self", is that actually the warning that appears in the code above? That seems strange. |
Beta Was this translation helpful? Give feedback.
Re: a
!
suppression operator updating the operand state to not-null. I assume the C# language team has considered this question at length and is unlikely to change the behavior. /cc @jcouv who may be able to provide context on that specifically.It looks like Nunit is unable to put
[NotNull]
on the parameter of theirAssert.NotNull
method because the method doesn't throw on a null argument in the context of a lambda passed toAssert.Multiple
. You may be able to solve your problem by introducing API within your own test project similar to the following: