Skip to content

Commit

Permalink
Modify rule S2583: Migrate Python description to LaYC format (#3198)
Browse files Browse the repository at this point in the history
  • Loading branch information
Swalkyn authored Oct 4, 2023
1 parent e12217f commit 1507896
Showing 1 changed file with 58 additions and 11 deletions.
69 changes: 58 additions & 11 deletions rules/S2583/python/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,23 +1,70 @@
Conditional expressions which are always `true` or `false` can lead to https://en.wikipedia.org/wiki/Unreachable_code[unreachable code].
To fix this issue, either update the condition or remove the unreachable code.

== Why is this an issue?

include::../description.adoc[]
Unreachable code is never executed, so it has no effect on the behaviour of the program.
If it is not executed because it no longer serves a purpose, then it adds unnecessary complexity.
Otherwise, it indicates that there is a logical error in the condition.

=== What is the potential impact?

Unreachable code affects maintainability.
It is harder for programmers to read, understand and modify the code when some parts are unreachable.
If the code should be reachable instead, then the program may not always behave as intended.

=== Exceptions

This rule will not raise an issue when

* The condition to reach the dead code is a boolean literal.
* The unreachable code is a simple statement, such as `pass`.

== How to fix it

You should first decide whether the unreachable code is useful.
If it is, then you should understand why the condition that leads to it is always false, and correct logical errors.
Otherwise, the code should be removed along with the condition.

=== Code examples

=== Noncompliant code example
==== Noncompliant code example

[source,python]
[source,python,diff-id=1,diff-type=noncompliant]
----
a = False
if a: # Noncompliant
doSomething() # never executed
def foo(a, b):
flag = True
n = None
if (a and not a): # Noncompliant
doSomething() # Never executed
if not n: # Noncompliant; n is None, which is always equivalent to "False" in a condition, "doSomethingElse()" is never evaluated
doSomething()
else:
doSomethingElse() # never executed
if (flag): # Noncompliant
return "Result 1"
return "Result 2" # Never executed
----

==== Compliant solution

[source,python,diff-id=1,diff-type=compliant]
----
def foo(a, b):
if (a and not b):
doSomething()
return "Result 1"
----

=== How does this work?

The first condition is always false.
In this case, we decide that the call to `doSomething` is useful.
The problem is then a bug in the condition; for example, using `a` instead of `b`.

The second condition is always true.
Here, we decided that the second return statement is not useful anymore.
There may have been code manipulating the variable `flag` that was previously removed.
We can remove the unreachable code, the condition and the variable.

include::../see.adoc[]

ifdef::env-github,rspecator-view[]
Expand Down

0 comments on commit 1507896

Please sign in to comment.