Switch Fallthrough Without Comment

ID

java.switch_fallthrough_no_comment

Severity

info

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Java

Tags

CWE:484, best-practice, code-style

Description

Reports switch cases that fall through to the next case without a break statement and without a comment indicating the fall-through is intentional. Empty case bodies used for label grouping are not flagged.

Rationale

Fall-through in switch statements is a common source of bugs. When a case lacks a break, execution continues into the next case. If this is intentional, a comment makes the intent explicit for future readers:

switch (x) {
    case 0:
        init();
        // Missing break - bug or intentional?
    case 1:
        process();
        break;
}

Remediation

Either add a break statement or add a comment indicating the fall-through is intentional:

switch (x) {
    case 0:
        init();
        // fall through
    case 1:
        process();
        break;
}