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:
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.
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.
default is not an mandatory in switch case. If no cases are matched and default is not provided, just nothing will be executed.
3) The default statement is optional. Even if the switch case statement do not have a default statement, it would run without any problem.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With