Rule Definition
By default, when the program reaches the end of a 'switch'-clause, it continues to the next clause. While this may prove useful in some cases, it is very uncommon and not at all obvious.
Remediation
Add a 'break', 'return' or 'throw' statement to end 'case' clauses.
Violation Code Sample
int main()
{
int i = 2;
switch(i)
{
case 0:
cout << "0" << endl;
// Violation
case 1:
// No violation: Empty clause
case 2:
cout << "1 or 2" << endl;
break; // No violation: A break
default:
cout << "Other" << endl;
// No violation: Last clause is default
}
}
Reference
MISRA C++
Related Technologies
C++
Technical Criterion
CWE-1075 - Unconditional Control Flow Transfer outside of Switch Block
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.