Unreachable Code
ID |
swift.unreachable_code |
Severity |
low |
Remediation Complexity |
auto_fix |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Dead Code |
Language |
Swift |
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,
fallthrough), after a never-returning call (fatalError,
preconditionFailure, abort, exit), or inside an always-taken
constant branch. Only the first statement of each dead region is flagged.
func compute() -> Int {
return 0
print("never") // FLAW
}
for x in xs {
if x == 0 { continue } // OK — conditional
work(x)
}
Rationale
Code after an unconditional terminator is a classic typo or leftover from an incomplete refactor. Swift’s compiler warns about some of these, but not all — and even the warnings are easy to overlook in a busy build log.
Remediation
Either remove the dead statements, or move them above the
terminator if the intent was to run them first. If you actually
need a side effect after a fatalError, the function is doing
something unusual and probably needs review anyway.
The rule only reports the first unreachable statement in each block — fixing that one usually reveals the rest.