Void Function Return Value Used
ID |
python.void_return_used |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Python |
Tags |
reliability, suspicious-construct |
Description
Reports assignments whose right-hand side is a call to a builtin method that always returns None. The left-hand side is then bound to None regardless of what the call does — usually because the developer expected the call to return the modified collection.
items = items.append(1) # FLAW — items is now None
sorted_items = items.sort() # FLAW — sorted_items is None; use sorted(items)
items.append(1) # OK — discard the None return
items.sort()
Rationale
Python’s collection methods mutate in place and return None, which catches developers who come from JavaScript / Ruby / Go expecting list.sort() to return the sorted list. The bug shows up as a confusing AttributeError later — 'NoneType' object has no attribute 'foo' — far from the actual mistake.
Remediation
Drop the assignment if the call mutates in place:
items.append(1)
items.sort()
Or use the non-mutating equivalent when you want a value:
sorted_items = sorted(items)
new_items = items + [1]
joined = {**a, **b} # instead of: c = a.update(b)