Home > Coding Helpers, Upcoming > Code fixes for code issues for switch (select) statements

Code fixes for code issues for switch (select) statements

April 23rd, 2012

You might have already learned that CodeRush suggests several code issues that highlight the switch (Select in VB) statement with hints and warnings when it has suspicious code. For example, when the switch statement handles only a subset of the possible enumeration values it is checking for, this may be a sign of incomplete code.

Until recently, there were no code fixes of those code issues. Now, they appear:

  • Add Missing Case Statements – adds all missing case statements to the switch statement.
  • Add Default Case Branch – adds the default case branch to the switch statement.

Consider the following code:

Switch statement code sample

The DayOfWeek enumeration contain seven enumeration elements for each day of a week. In the code above one of the days is missing (Wednesday). The expression of the switch statement is highlighted with a code issue of the suggestion type, and the following hint appears if you hover over it:

CodeRush Switch statement code issues hint

There are two code issues listed in the hint and both have the corresponding fixes which can be seen in the preview hint before applying:

Add Missing Case Statements

CodeRush Add Missing Case Statements preview

Add Default Case Branch

CodeRush Add Default Case Branch preview

NOTE: please take into account the “Versions” field at the bottom of the post, to determine the build number for which the content is applied. Click here to see the latest officially released version.

—–
Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: 2008 and up
Updated: Apr/23/2012
ID: U013

Similar Posts: