Skip to content

unreachable-code

Reports a statement that can never run: after return, throw, break or continue, after an if/else or try that always jumps, or after a loop that never ends. Only the first statement of such a run is reported. Where the language has a flow layer (C#) it also covers constant conditions and calls that do not return (does_not_return in semantics:, the option non_returning_calls). Hoisted declarations and empty statements are left out. Removing the statement is offered as an editor quick fix only, because unreachable code usually signals a bug.

Code that cannot run is either a leftover or a sign that an earlier return or throw is in the wrong place, in which case what the author expected to run does not.

To fix, delete the statement, or move the jump that makes it unreachable.

Reported
...
func h(a bool) int {
return 1
g()
}
func k(a bool) int {
...
} else {
return 2
}
g()
}
func m(a bool) int {
...
c:
lint:
rules:
unreachable-code:
level: warn # off, warn (default), error
options:
non_returning_calls: true
cpp:
lint:
rules:
unreachable-code:
level: warn # off, warn (default), error
options:
non_returning_calls: true
csharp:
lint:
rules:
unreachable-code:
level: warn # off, warn (default), error
options:
non_returning_calls: true
go:
lint:
rules:
unreachable-code:
level: warn # off, warn (default), error
options:
non_returning_calls: true
java:
lint:
rules:
unreachable-code:
level: warn # off, warn (default), error
options:
non_returning_calls: true
js:
lint:
rules:
unreachable-code:
level: warn # off, warn (default), error
options:
non_returning_calls: true
kotlin:
lint:
rules:
unreachable-code:
level: warn # off, warn (default), error
options:
non_returning_calls: true
php:
lint:
rules:
unreachable-code:
level: warn # off, warn (default), error
options:
non_returning_calls: true
python:
lint:
rules:
unreachable-code:
level: warn # off, warn (default), error
options:
non_returning_calls: true
ruby:
lint:
rules:
unreachable-code:
level: warn # off, warn (default), error
options:
non_returning_calls: true
Option Type Default Description
non_returning_calls bool true With a flow layer, treat the code after a call that never returns ([DoesNotReturn], does_not_return in semantics:) as unreachable. A compiler does not, so turn this off to report exactly what it reports.

Enabled by the presets: go/revive, js/eslint-recommended, js/biome-recommended, js/oxlint.

Suppress one line with sudsy-disable-next-line unreachable-code in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
eslint no-unreachable full
revive unreachable-code partial the code after panic, os.Exit, log.Fatal* and t.Fatal* is not read as unreachable
Roslynator RCS1148 full