Unreachable Code
ID |
python.unreachable_code |
Severity |
low |
Remediation Complexity |
auto_fix |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Dead Code |
Language |
Python |
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, raise, break, continue), after a never-returning call (sys.exit(…), os._exit(…), exit, quit), or inside an always-taken constant branch. Only the first statement of each dead region is flagged.
def foo():
return 1
print("never") # FLAW
def bar(items):
for x in items:
if cond(x):
continue
log(x) # FLAW
print("after loop") # OK — `for` body's continue does not reach here
Rationale
Unreachable code is almost always either a leftover of a refactor or a sign that the developer misunderstood the control flow. Either way, the dead lines are noise that future readers have to evaluate.
Remediation
Delete the unreachable lines. If the lines were meant to run, check whether the terminator is in the wrong branch — e.g. an if-less return that should have been guarded.