switch expression: Warning CS8524 issued although all cases have been covered
Version Used
- .NET 8
- Visual Studio 2022, Version 17.10.2
Steps to Reproduce
Create an enum like this:
public enum Orientation
{
Horizontal,
Vertical,
Random
}
Now create a switch expression like this one:
_orientation = Orientation switch
{
Orientation.Horizontal => 0,
Orientation.Vertical => 1,
Orientation.Random => _random.Next(2)
};
Diagnostic Id
(Translated from German into English by me):
Warning (aktive) CS8524 The switch expression doesn't process some of the corresponding input type values, including an anonymous enumeration value (non-comprehensive expression). The expression "(Orientation)3" isn't covered, for example.
Expected Behavior
No warning should be issued.
Actual Behavior
The above warning is generated, suggesting a value that isn't even included in the enum.
Warnung (aktiv) CS8524 Der switch-Ausdruck verarbeitet einige Werte des zugehörigen Eingabetyps einschließlich eines unbenannten Enumerationswerts nicht (nicht umfassender Ausdruck). Das Muster "(Orientation)3" wird beispielsweise nicht abgedeckt.
The warning is correct, you can do this:
Orientation orientation = (Orientation)3;
@jjonescz: You could also do anything that's valid for an integer. Yet, who would do that?
I don't think it's reasonable to warn about hypothetical assignments. It would make more sense to warn about assigning invalid values to an enum type.
I don't think it's reasonable to warn about hypothetical assignments.
In that case you should suppress the warning for your whole project or solution.
It would make more sense to warn about assigning invalid values to an enum type.
There might be an analyzer for that. But it's an explicit cast, so it should be obvious you are doing something advanced and warning doesn't seem necessary.
Otherwise this is a language design question, those should be discussed in https://github.com/dotnet/csharplang first. Thanks!
With all due respect, the current warning text doesn't make any sense to this regard.
In the end, it all boils down to "add an anonymous switch case". Period. Nothing else.
Anything else, like the current "you are missing this or that value" endlessly is just a ridiculous cat-and-mouse game. You would end up adding 32768 switch cases.
Have you ever thought about providing a quick fix for remedying this warning by adding all the corresponding switch cases? The current warning message ("you are missing a particular value") is plain nonsense.
There should be a fix offered here.
re-open for IDE fixer
New feature/Temp put under Sam's bucket because it's related to CodeStyle.
@CyrusNajmabadi It seems like the fix here is project specific. For example, in Roslyn the fix would be throwing ExceptionUtilities.UnexpectedValue, but that method doesn't exist generally.
After reviewing the SharpLab output for the original code sample, I'm doubting our ability to improve upon the current behavior. Before implementing a code fix, I would need to see a proposed code fix that achieves the following:
- The performance matches the current behavior (the exception is thrown from a different method, or the use of a throw helper is known to no longer provide specific value)
- The exception type and message is of similar quality to the current one
- The clarity of the switch expression is not negatively impacted
The fixer can throw an InvalidOperation exception, or whatever you want. If you do not believe we shoudl do anything here, close this out.