rspec/rules/S5797/rule.adoc

41 lines
990 B
Plaintext
Raw Normal View History

2023-08-04 16:43:49 +02:00
This rule raises an issue when a constant expression is used as a condition in an ``++if++``, ``++elif++``, a conditional expression or other boolean expressions.
== Why is this an issue?
2020-06-30 12:50:28 +02:00
When a constant is used as a condition, either it has no effect on the execution flow and it can be removed, or some code will never be executed and it is a bug.
2021-02-02 15:02:10 +01:00
=== Noncompliant code example
2020-06-30 12:50:28 +02:00
2023-08-04 16:43:49 +02:00
[source,python,diff-id=1,diff-type=noncompliant]
2020-06-30 12:50:28 +02:00
----
2023-08-04 16:43:49 +02:00
def foo():
a = True
if a: # Noncompliant: the condition is always true
return 1
2020-06-30 12:50:28 +02:00
else:
2023-08-04 16:43:49 +02:00
return 2
2020-06-30 12:50:28 +02:00
----
=== Compliant solution
2020-06-30 12:50:28 +02:00
2023-08-04 16:43:49 +02:00
[source,python,diff-id=1,diff-type=compliant]
2020-06-30 12:50:28 +02:00
----
2023-08-04 16:43:49 +02:00
def foo():
a = bar()
if a:
return 1
2020-06-30 12:50:28 +02:00
else:
2023-08-04 16:43:49 +02:00
return 2
2020-06-30 12:50:28 +02:00
----
== Resources
2020-06-30 12:50:28 +02:00
2023-08-04 16:43:49 +02:00
=== Documentation
* Python documentation - https://www.python.org/dev/peps/pep-0285/[PEP 285 - Adding a bool type]
* Python documentation - https://docs.python.org/3/library/stdtypes.html#truth-value-testing[Python documentation - Truth Value Testing]
2020-06-30 12:50:28 +02:00