Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for missing default case in switch statement

For ReSharper 6.1, there is no built-in inspection item for missing default statements within a switch for C#, however the custom patterns seem generally robust. I've messed around with them a bit for cases like missing else statements for if blocks, but I'm not sure how to do a check for missing default.

Here's what I have so far:

Search Pattern

switch($expr$)
{
    case $val$:
        $statement$
        break;
    $missingDefault$
}

Replacement Pattern

switch($expr$)
{
    case $val$:
        $statement$
        break;
    default:
        break;
}

Where $expr$ is an expression, $val is an expression, $statement$ is any number of statements, and $missingDefault$ is a maximum of 0 statements.

The problems here are the following:

  • We can have any number of cases, which are themselves a collection made up of one or more statements (case + break, etc.) and any number of expressions
  • For search pattern matching, we should only match against occurrences where there is nothing after the last case (ie. no default)
  • We need the 'break' in the search pattern such that we can define nonexistence of statements thereafter. This break is required by the compiler, anyway.

Obviously, this search pattern only matches against occurrences containing a single case and no default, so is relatively useless. I need a pattern that will match against switches with any number of cases, any number of which may or may not contain a break (except the last case) and can contain any number of statements, and no default.

Thanks for your help.

like image 921
jropella Avatar asked Aug 08 '12 14:08

jropella


People also ask

What if there is no default case in switch?

If there's no default statement, and no case match is found, none of the statements in the switch body get executed. There can be at most one default statement. The default statement doesn't have to come at the end. It may appear anywhere in the body of the switch statement.

Is it mandatory to write default in switch case?

default is not an mandatory in switch case. If no cases are matched and default is not provided, just nothing will be executed.

Is default optional in switch case?

3) The default statement is optional. Even if the switch case statement do not have a default statement, it would run without any problem.

What happens if default is written first in switch case?

The position of default doesn't matter, it is still executed if no match found. // The default block is placed above other cases. 5) The statements written above cases are never executed After the switch statement, the control transfers to the matching case, the statements written before case are not executed.


Video Answer


1 Answers

I've had a good crack at this and I don't believe it is currently possible (Resharper 7)

Having said that you can always ask on the Resharper forum

The only thing I can provide that may be of any use is the pattern to find

1) all switch statements

switch($expr$)
    $statement$

2) the switch statements that end in default; break:

switch($expr$)
{
    $statement$
    default:
    break;
}

You could then use the difference of these two lists to determine which ones are missing the default;break; statement. For example in my project I have 231 occurrences of the first and only 58 of the second.

I realise this is a long way off what you wanted (no replace!) but its the best I can muster.

like image 131
wal Avatar answered Oct 30 '22 02:10

wal