org.sonar.l10n.java.rules.squid.S1219.html Maven / Gradle / Ivy
The newest version!
Even if it is legal, mixing case and non-case labels in the body of a switch statement is very confusing and can even be the result of a typing error.
Noncompliant Code Examples
Case 1, the code is syntactically correct but the behavior is not the expected one
switch (day) {
case MONDAY:
break;
case TUESDAY:
foo:for(int i = 0 ; i < X ; i++) {
/* ... */
break foo; // this break statement doesn't relate to the nesting case TUESDAY
/* ... */
}
break;
/* ... */
}
Compliant Solution
Case 1
switch (day) {
case MONDAY:
case TUESDAY:
case WEDNESDAY:
doSomething();
break;
...
}
Case 2
switch (day) {
case MONDAY:
break;
case TUESDAY:
compute(args); // put the content of the labelled "for" statement in a dedicated method
break;
/* ... */
}
See
- MISRA C:2004, 15.0 - The MISRA C switch syntax shall be used.
- MISRA C++:2008, 6-4-3 - A switch statement shall be a well-formed switch statement.
- MISRA C:2012, 16.1 - All switch statements shall be well-formed
© 2015 - 2025 Weber Informatics LLC | Privacy Policy