Cases in switch statement should not fall through to the next one

  • SWITCH_CASE_FALL_THROUGH
  • Code Quality
  • Low
  • cwe

This rule applies when the execution of a case in switch statement continues to the following case.

It might be intentional, but it often is a programmer's mistake forgetting break statement.

Noncompliant Code Example

View with compliant examples side by side
switch (foo) {
case 1: // SWITCH_CASE_FALL_THROUGH alarm
    doSomething(1);
case 2:
    doSomething(2);
}

Compliant Code Example

View with noncompliant examples side by side
switch (foo) {
case 1:
    doSomething(1);
    break;
case 2:
    doSomething(2);
}

Version

This rule was introduced in DeepScan 1.0.0-alpha.

See

  • CWE-484

  • MISRA C:2004, Rule 15.2: An unconditional break statement shall terminate every non-empty switch clause

  • MISRA C:2012, Rule 16.3: An unconditional break statement shall terminate every switch-clause

Was this documentation helpful?