Default Clause Not Last
ID |
javascript.switch_default_not_last |
Severity |
high |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
JavaScript |
Tags |
code-style, reliability |
Description
Reports a switch whose default clause is not its last clause. A switch with no default at all is out of scope — javascript.default_case covers that.
Rationale
// Bad
switch (level) {
default:
log("unknown level"); // falls through into case "warn"
case "warn":
warn(message);
break;
case "error":
fail(message);
break;
}
-
Readers expect the catch-all branch at the end, so a
defaultplaced earlier is easy to miss when scanning for the clause that handles a given value. -
It is a live hazard rather than a style preference: a
defaultthat does not end inbreak,returnorthrowfalls through into whichever clause happens to follow it. -
Reordering an unrelated
casethen changes what the fall-through runs, so the bug appears in a commit that never touched thedefault.