Switch Case Falls Through Without Comment

ID

javascript.switch_fallthrough_no_comment

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

JavaScript

Tags

CWE:484, reliability, switch

Description

Reports switch cases that fall through to the next case without a break, return, throw or continue statement, and without a comment indicating that the fall-through is intentional. Empty case bodies (label grouping such as case 0: case 1: …​) are excluded because they represent the deliberate multi-label pattern. The last case of a switch is also excluded — there is no subsequent case to fall through to.

Fall-through markers (case-insensitive) recognised in either the current case body or the leading comment of the next case: falls through, fall through, fallthrough, fall-through, no break, intentional.

Rationale

// Bad — `case 1` silently falls through to `case 2`.
switch (x) {
  case 1:
    doA();
  case 2:
    return "two";
}

Most fall-through bugs come from a forgotten break after a refactor. Requiring an explicit comment when the fall-through is intentional makes the bug class trivial to review.

Remediation

Either terminate the case (break / return / throw / continue) or document the intentional fall-through:

// Good — break.
switch (x) {
  case 1:
    doA();
    break;
  case 2:
    return "two";
}

// Good — explicit fall-through comment.
switch (x) {
  case 1:
    doA();
    // falls through
  case 2:
    return "shared";
}

// Good — label grouping (empty bodies).
switch (x) {
  case 0:
  case 1:
  case 2:
    return "low";
}