Statement With No Effect
ID |
python.statement_no_effect |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Python |
Tags |
reliability, suspicious-construct |
Description
Reports an expression statement whose only content is a comparison. Python evaluates the expression and discards the result, so the line does nothing — almost always a typo for an assignment (x == 5 typed instead of x = 5).
def f(x):
x == 5 # FLAW — typo for `x = 5`
return x
def g(x):
if x == 5: # OK — comparison as condition
return True
x = 5 # OK — assignment
The rule is intentionally narrow: bare-identifier statements (x) and other expression-as-statement forms can be intentional (REPL probing, IPython line-magic), but a comparison as a statement is essentially never what the writer meant.
Rationale
The bug class is one of the highest-signal in the language: the typed character is one keystroke off the intended one, the result silently does nothing, and the typo is hard to spot in review.
Remediation
Replace the comparison operator with = (assignment) — or wrap the comparison in assert / if if you intended to evaluate it.