SWITCH-fall-through
In this section:
Synopsis
There are non-empty switch cases not terminated by break and without 'fallthrough' comment.
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
There are non-empty switch cases not terminated by a break. A non-empty switch clause should be terminated by an unconditional break statement, unless explicitly commented as a 'fallthrough'.
Coding standards
- CERT MSC17-C
Finish every set of statements associated with a case label with a break statement
Code examples
The following code example fails the check and will give a warning:
void example(int input) {
switch(input) {
case 0:
if (rand()) {
break;
}
default:
break;
}
}
The following code example passes the check and will not give a warning about this issue:
void example(int input) {
switch(input) {
case 0:
if (rand()) {
break;
}
break;
case 1:
if (rand()) {
break;
}
// fallthrough
case 2:
// this should also fall through
if (!rand()) {
return;
}
default:
break;
}
}