Duplicate Switch Case
ID |
javascript.no_duplicate_case |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
JavaScript |
Tags |
dead-code, reliability, suspicious-comparison |
Description
Reports switch statements that contain two case clauses with the same condition. The duplicate case is dead code — the switch matches the first occurrence and the second never fires.
Rationale
-
The duplicate is almost always a copy-paste leftover where the case value was meant to be edited.
-
The bug stays silent because tests typically only exercise the first branch.
-
The fall-through behaviour expected by readers is broken in subtle ways when the duplicate sits between other cases.
// Bad
switch (x) {
case 1: return "one";
case 2: return "two";
case 1: return "unreachable";
}
Remediation
Either fix the duplicate to the value it was meant to match, or remove it.
// Good
switch (x) {
case 1: return "one";
case 2: return "two";
case 3: return "three";
}