Unreachable Code
ID |
go.unreachable_code |
Severity |
low |
Remediation Complexity |
auto_fix |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Dead Code |
Language |
Go |
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, so it covers
more than the line directly after a jump: code becomes unreachable after an unconditional jump
(return, break, continue, goto), after a never-returning call (panic(…),
os.Exit(…), log.Fatal(…)), or inside an always-taken constant branch (for example the
code after an if false { … }). Only the first statement of each dead region is flagged.
Rationale
Code that follows an unconditional jump or process exit is dead code. It misleads readers who may believe the code does something meaningful, and it typically indicates a logic error (the jump was meant to be conditional) or leftover code from a refactoring.
func first(items []int) int {
return items[0]
log.Print("returned") // FLAW — never reached after return
}
func shutdown() {
os.Exit(1)
cleanup() // FLAW — never reached after os.Exit
}
func bounded(items []int) {
for _, v := range items {
if v < 0 {
continue
}
process(v) // OK — reachable on the non-negative path
}
}
Remediation
Remove the unreachable statement. If it was meant to execute, move the terminating statement after it, or make the terminating statement conditional.