org.sonar.l10n.py.rules.python.S1854.html Maven / Gradle / Ivy
Why is this an issue?
Dead stores refer to assignments made to local variables that are subsequently never used or immediately overwritten. Such assignments are
unnecessary and don’t contribute to the functionality or clarity of the code. They may even negatively impact performance. Removing them enhances code
cleanliness and readability. Even if the unnecessary operations do not do any harm in terms of the program’s correctness, they are - at best - a waste
of computing resources.
Exceptions
This rule ignores initializations to -1
, 0
, 1
, None
, True
, False
and
""
. No issue will be raised on unpacked variables.
How to fix it
Remove the unnecesarry assignment, then test the code to make sure that the right-hand side of a given assignment had no side effects (e.g. a
method that writes certain data to a file and returns the number of written bytes).
Code examples
Noncompliant code example
def func(a, b, compute):
i = a + b # Noncompliant; calculation result not used before value is overwritten
i = compute()
return i
Compliant solution
def func(a, b, compute):
i = a + b
i += compute()
return i
Resources
Standards
Related rules
- {rule:python:S1763} - All code should be reachable
- {rule:python:S3516} - Functions returns should not be invariant
- {rule:python:S3626} - Jump statements should not be redundant