RED-case-reach
Synopsis
A case statement within a switch statement cannot be reached.
Enabled by default
No
Severity/Certainty
Low/Medium

Full description
A case statement within a switch statement cannot be reached, because the switch statement's expression cannot have the value of the case statement's label. This often occurs because literal values have been assigned to the switch condition. An unreachable case statement is not unsafe as such, but might indicate a programming error. This check is identical to MISRAC++2008-0-1-2_c, MISRAC2012-Rule-2.1_a, MISRAC++2023-0.0.2_c.
Coding standards
- CERT MSC07-C
Detect and remove dead code
- MISRA C:2012 Rule-2.1
(Required) A project shall not contain unreachable code
- MISRA C++ 2008 0-1-2
(Required) A project shall not contain infeasible paths.
- MISRA C++ 2023 0.0.2
(Advisory) Controlling expressions should not be invariant
Code examples
The following code example fails the check and will give a warning:
void example(void) {
int x = 42;
switch(2 * x) {
case 42 : //unreachable case, as x is 84
;
default :
;
}
}
The following code example passes the check and will not give a warning about this issue:
void example(void) {
int x = 42;
switch(2 * x) {
case 84 :
;
default :
;
}
}