Switch Statement Should Have Default Branch
ID |
java.switch_on_enum_full_coverage |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
CWE:478, code-style, reliability |
Description
Reports classic switch statements that do not contain a default branch. A missing default means that new values (especially when the switched expression is an enum) silently fall through without executing any code, causing hard-to-find bugs when the type grows.
Rationale
When a switch lacks a default branch and a value is encountered that does not match any case, execution silently skips the entire switch body. This is particularly dangerous with enums: adding a new constant to an enum will compile cleanly but produce incorrect behavior at every switch site that does not handle it.
// Bad: no default branch
switch (color) {
case RED:
paint(RED);
break;
case GREEN:
paint(GREEN);
break;
// BLUE silently ignored if added later
}