-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Missing warning for non-exhaustive switch-expression #52714
Comments
The warning is emitted in NullableWalker, but lost because of:
Here a new DiagnosticBag is created and thrown away - all diagnostics are lost... With |
@gafter any chance you could comment here? When the switch expression is |
To fix this, we should not throw diagnostics away and remove the following lines from "nullable warnings" in "ErrorFacts": nullableWarnings.Add(GetId(ErrorCode.WRN_SwitchExpressionNotExhaustiveForNull));
nullableWarnings.Add(GetId(ErrorCode.WRN_SwitchExpressionNotExhaustiveForNullWithWhen)); see: #52715 |
#nullable enable
using System.Diagnostics.CodeAnalysis;
int M([DisallowNull] int? i) => i switch { int j => j }; // ok I feel like there are two "weird" things you could potentially do if you wanted to change this:
Given that, it may be best to leave the existing behavior as-is. |
Yes, this is what I would expect. I expect nullable flow state to help the compiler understand that a null check is unnecessary. The fact that we're not doing this is severely breaking my brain right now. |
The situation is at least inconsistent. If you write the same with a switch statement: namespace Tests
{
public class Prog
{
static int EvalPoint((int, int)? point){
switch(point)
{
case (0, 0):
return 1;
case var (_, _):
return 2;
}
}
static void Main()
{
EvalPoint(null);
}
}
} You get the error message:
regardless of which nullable settings you have set. I would expect something similar for switch expressions. |
There isn't a precedent for If we do proceed on this, then it seems like the new warning should be under a warning wave, and I would prefer that both nullable reference types and nullable value types are handled, since both can be null, and the behavior if you don't handle null is the same. |
It appears that the LDM decision is that the nullable context controls whether an exhaustiveness warning is issued when null is the only unhandled value. Perhaps this can be revisited on the csharplang side or internally if someone on the compiler/language team feels strongly about it, but this roslyn issue is considered to be "by design" at this time. |
That decision is strictly about reference types? This does look like a bug to me. |
I find the notes slightly ambiguous because the section Nullable reference types vs switch analysis
But the subheading Conclusion states:
This does appear to be written after the decision was made to track nullable value types akin to nullable reference types: dotnet/csharplang#1865. It isn't clear to me why it was indicated that nullable value types should be handled separately. But going by all this you may be right @alrz. |
I think the next step is probably to confirm with LDT whether the shipped behavior shown in the issue description is what they intended or not. |
I don't think there's a meaningful distinction between reference types and value types here. |
If I recall correctly, this is by design. Examples that are not exhaustive due to not checking for some null are only intended to get a warning in a nullable enabled context. |
Yes, this matches my recollection as well (this is by-design). The exhaustiveness analysis with regards to nulls is part of nullability analysis and requires a nullable-enabled context. |
Version Used: c31d8af
Steps to Reproduce:
Compile:
Expected Behavior:
Warning: warning CS8509: The switch expression does not handle all possible values of its input type (it is not exhaustive). For example, the pattern 'null' is not covered.
or
warning CS8655: The switch expression does not handle some null inputs (it is not exhaustive). For example, the pattern 'null' is not covered.
Actual Behavior:
No warning
The text was updated successfully, but these errors were encountered: