MISRAC++2023-9.4.2_f
Synopsis
(Required) The structure of a switch statement shall be appropriate
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
Misplaced case label. Must be in the switch statements outmost block scope. This check is identical to MISRAC++2008-6-4-4, MISRAC2012-Rule-16.2, MISRAC2004-15.1.
Coding standards
- MISRA C:2004 15.1
(Required) A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement.
- MISRA C:2012 Rule-16.2
(Required) A switch label shall only be used when the most closely-enclosing compound statement is the body of a switch statement
- MISRA C++ 2008 6-4-4
(Required) A switch-label shall only be used when the most closely-enclosing compound statement is the body of a switch statement.
Code examples
The following code example fails the check and will give a warning:
#include <stdlib.h>
void example(void) {
switch(rand()) {
{case 1:}
case 2:
case 3:
default:
}
}
The following code example passes the check and will not give a warning about this issue:
#include <stdlib.h>
void example(void) {
switch(rand()) {
case 1:
case 2:
case 3:
default:
}
}