"Some values of the enum are not processed inside switch statement"

I am making use of some external library that comes with an enum with several dozens of values. I would not have designed things this way, but that's how it is. 

Most of these values I do not care about, so every single one of my `switch` statements that deal with this particular enum check for just a few values, and delegate the rest to the `default` clause.

ReSharper flags every single one of my `switch` statements with the `Some values of the enum are not processed inside switch statement` inspection, despite the presence of the `default` clause.

The way I see it, this inspection only makes sense when the programmer omits the `default` clause. 

When you intend to cover all the enum values, leaving none unhandled, that's when you are likely to want to omit the `default` clause, and as a matter of fact in this case the `default` clause could perhaps even be flagged with the `unreachable code` inspection.  (But it doesn't. That's another thing you might want to look into.)

When you do use the `default` clause, you are explicitly indicating that you are deliberately not examining all enum values. Therefore, in the presence of the `default` clause, it is nonsensical to be slapped with a "Some values not processed" inspection.

As it stands right now, I have to either specifically disable this inspection on every single instance of a switch statement on this particular huge enum, which would be ugly, or to disable this inspection entirely, which would leave all other switch statements in my entire code base vulnerable to bugs.

 

1 comment
Comment actions Permalink

The default case can't be marked as "unreachable", even if you've already covered all existing enum values. It's possible to cast an invalid number to an enum:

enum Foo { A = 1, B = 2 }

static void Bar(Foo foo)
{
switch (foo)
{
case Foo.A:
{
Console.WriteLine("Bar(A)");
break;
}
case Foo.B:
{
Console.WriteLine("Bar(B)");
break;
}
default:
{
Console.WriteLine("Unreachable?");
break;
}
}
}

Bar((Foo)42);

Even discounting that possibility, what happens when another value is added to the enum? The new value isn't covered by any existing case, and the formerly "unreachable" default case is reachable again.

0

Please sign in to leave a comment.