org.sonar.l10n.py.rules.python.S905.html Maven / Gradle / Ivy
Statements with no side effects and no change of control flow do not contribute to the functionality of the code and can indicate a programming
error.
Why is this an issue?
When writing code, it is important to ensure that each statement serves a purpose and contributes to the overall functionality of the program. When
they have no side effects or do not change the control flow, they can either indicate a programming error or be redundant:
- The code does not behave as intended: The statements are expected to have an effect but they do not. This can be caused by mistyping,
copy-and-paste errors, etc.
- The statements are residual after a refactoring.
Exceptions
Intentionally empty statement
Statements such as pass
or ...
(ellipsis) are clearly meant to have no effect and may be used to indicate an
implementation is missing. No issue will be raised in this case.
Strings
Some projects use string literals as comments. By default, this rule will not raise an issue on these strings. Reporting on string literals can be
enabled by setting the rule parameter reportOnStrings
to true
.
def foo():
bar()
"""Some comment""" # Compliant by default. Noncompliant with "reportOnStrings" set to "true"
qix()
Operators
By default, this rule considers that no arithmetic operator has a side effect. Some projects may redefine operators and add a side effect. You can
list such operators in the rule parameter ignoredOperators
.
def process(p, beam):
"""
Apache Beam redefines "|" and ">>" operators and they have a side effect.
Thus for Apache Beam projects "ignoredOperators"should be set to "|,>>"
"""
p | "create" >> beam.Create() # Noncompliant by default
How to fix it
Identify statements that do not contribute to the functionality of the code and verify if they are intended to be part of the logic. If they are,
there is a bug to be fixed. If they are not, then they are redundant and should be removed.
Code examples
Noncompliant code example
def get_result():
result = 42
if should_be_zero():
result == 0 # Noncompliant: no side effect, was an assignment intended?
return result
Compliant solution
def get_result():
result = 42
if should_be_zero():
result = 0 # Compliant
return result
Resources
Standards