roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

switch expression: Warning CS8524 issued although all cases have been covered

Open SetTrend opened this issue 1 year ago • 6 comments

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.

SetTrend avatar Jun 20 '24 15:06 SetTrend

The warning is correct, you can do this:

Orientation orientation = (Orientation)3;

jjonescz avatar Jun 21 '24 08:06 jjonescz

@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.

SetTrend avatar Jun 21 '24 18:06 SetTrend

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!

jjonescz avatar Jun 21 '24 19:06 jjonescz

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.

SetTrend avatar Jun 21 '24 20:06 SetTrend

There should be a fix offered here.

CyrusNajmabadi avatar Jun 22 '24 01:06 CyrusNajmabadi

re-open for IDE fixer

jaredpar avatar Jun 24 '24 17:06 jaredpar

New feature/Temp put under Sam's bucket because it's related to CodeStyle.

Cosifne avatar Jul 01 '24 19:07 Cosifne

@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.

sharwell avatar Jul 02 '24 13:07 sharwell

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:

  1. 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)
  2. The exception type and message is of similar quality to the current one
  3. The clarity of the switch expression is not negatively impacted

sharwell avatar Jul 02 '24 13:07 sharwell

The fixer can throw an InvalidOperation exception, or whatever you want. If you do not believe we shoudl do anything here, close this out.

CyrusNajmabadi avatar Nov 26 '24 01:11 CyrusNajmabadi