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