Unreachable Code
ID |
javascript.unreachable_code |
Severity |
low |
Remediation Complexity |
auto_fix |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Dead Code |
Language |
JavaScript |
Tags |
dead-code, reliability |
Description
Reports a statement that can never execute because control flow always leaves the enclosing region before reaching it. The analysis walks the function’s control-flow graph: code becomes unreachable after an unconditional jump (return, throw, break, continue), after a never-returning call (process.exit(…)), or inside an always-taken constant branch. Only the first statement of each dead region is flagged.
Hoisted forms (function, async function, function* declarations and empty statements ;) are not reported, because the engine moves them to the start of the enclosing scope, so they remain reachable regardless of where they appear textually.
Rationale
-
Unreachable code is dead weight — it ships in the bundle, costs reviewer attention, and very often hides a logic mistake (the early return was meant to be inside an
if, thebreakwas meant to be after the work, etc.). -
Unlike many style rules, this one nearly always points at a real bug. The author either expected the early exit to be conditional, or expected the trailing code to run before exiting.
// Bad
function fetchUser(id) {
return cache.get(id);
cache.set(id, build(id)); // never runs
}
Remediation
Either remove the unreachable statement, or move it before the terminator if it was supposed to run.
// Good
function fetchUser(id) {
if (cache.has(id)) {
return cache.get(id);
}
const user = build(id);
cache.set(id, user);
return user;
}