Home > Code Analysis > Code Issues for Switch (Select) and Case statements

Code Issues for Switch (Select) and Case statements

December 14th, 2011

A switch statement executes logic, dependent on the value of a given expression (parameter). The types of values a switch statement operates on can be boolean, enum, integral types, and strings. Each switch statement can contain any number of case statements, but no two case constants within the same switch statement can have the same value. Each case statement defines a value to compare against the original expression. You may also include a default label following all other case statements. If none of the other choices match, then the default choice is taken and its statements are executed. If there is no default label, control is transferred outside the switch.

CodeRush Pro provides several code issues specific to a switch statement (C#) or Select statement (VB) as well as its nested case statements. These code issues are of type warning, dead code and code smell.

Empty case statement

Cause:

This code issue is shown when the Switch or Select statement is empty and can be safely removed. If no code is executed inside the case statement, it is redundant.

Sample:

Empty switch statement CodeRush code issue

How to fix:

  • Remove the empty case statement.

Default branch is missing

Cause:

This is a code issue of the warning type, no matter that the default label is optional. It is shown when the switch or select statement is missing a default label which may be unintentional.

Sample:

CodeRush Default Branch Is Missing Sample

How to fix:

CodeRush Default Branch Is Missing Fix

Case statements do not explicitly handle all enum values

Cause:

An expression of a case statement can be of the enumeration type. Enumeration types have a definite number of enum elements. If the switch or select statement handles only a subset of the possible enumeration values it’s checking for, the code issue will be shown because this may be a sign of incomplete code.

Sample:

CodeRush Case Statement Do Not Explocitly Handle All Enum Values Sample

How to fix:

CodeRush Case Statement Do Not Explocitly Handle All Enum Values Fix

  • Add a default branch:

CodeRush Case Statement Do Not Explicitly Handle All Enum Values Fix #2

Case statement has incorrect range of integers expression

Cause:

This code issue is useful for the Select statement in Visual Basic, because case statements in VB can handle a specific range of values, not only a single constant value. The range of the values is defined according to the language specification using the “To” operator as follows:

“Case 20 To 30”.

However, if the range expression is specified incorrectly, the code issue of the code smell type is shown.

Sample:

Case statement has incorrect range of integers expression CodeRush code issue

The case statement in the sample actually specifies a single value “-10”, because the expression range is treated as a mathematical minus operation, in other words: 20 – 30 = -10.

How to fix:

  • Replace the incorrect integer expressions with the “To” operator:

CodeRush Case Statement Has Incorrect Range Of Integers Expression Fix

—–
Products: CodeRush Pro
Versions: 12.1 and up
VS IDEs: any
Updated: Nov/12/2012
ID: C144

Similar Posts: