2023-08-04 16:41:02 +02:00
This rule raises an issue when a loop with an ``++else++`` clause doesn't contain any ``++break++`` statement in its body.
2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-04-28 16:49:39 +02:00
The ``++else++`` clause of a loop is skipped when a ``++break++`` is executed in this loop. In other words, a loop with an ``++else++`` but no ``++break++`` statement will always execute the ``++else++`` part (unless of course an exception is raised or ``++return++`` is used). If this is what the developer intended, it would be much simpler to have the ``++else++`` statement removed and its body unindented. Thus having a loop with an ``++else++`` and no ``++break++`` is most likely an error.
2023-08-04 16:41:02 +02:00
== How to fix it
Add a ``++break++`` statement to the loop body containing an ``++else++`` clause or remove the ``++else++`` clause.
2021-04-28 18:08:03 +02:00
2023-08-04 16:41:02 +02:00
=== Code examples
2021-04-28 16:49:39 +02:00
2023-08-04 16:41:02 +02:00
==== Noncompliant code example
[source,python,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
from typing import List
2023-08-04 16:41:02 +02:00
def foo(elements: List[str]):
2021-04-28 16:49:39 +02:00
for elt in elements:
if elt.isnumeric():
return elt
2023-08-04 16:41:02 +02:00
else: # Noncompliant: no break in the loop
raise ValueError("List does not contain any number")
def bar(elements: List[str]):
for elt in elements:
if elt.isnumeric():
return elt
else: # Noncompliant: no break in the loop
2021-04-28 16:49:39 +02:00
raise ValueError("List does not contain any number")
----
2021-04-28 18:08:03 +02:00
2023-08-04 16:41:02 +02:00
==== Compliant solution
2021-04-28 16:49:39 +02:00
2023-08-04 16:41:02 +02:00
[source,python,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
from typing import List
2023-08-04 16:41:02 +02:00
def foo(elements: List[str]):
2021-04-28 16:49:39 +02:00
for elt in elements:
if elt.isnumeric():
break
else:
raise ValueError("List does not contain any number")
return elt
2023-08-04 16:41:02 +02:00
def bar(elements: List[str]):
2021-04-28 16:49:39 +02:00
for elt in elements:
if elt.isnumeric():
return elt
raise ValueError("List does not contain any number")
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
== Resources
2021-04-28 16:49:39 +02:00
2023-08-04 16:41:02 +02:00
=== Documentation
* Python documentation - https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops[Break and continue Statements, and else Clauses on Loops]
2021-04-28 18:08:03 +02:00
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Add a "break" statement or remove this "else" clause.
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== on 14 Apr 2020, 09:49:58 Pierre-Yves Nicolas wrote:
I would expect the compliant solution to suggest something like:
----
for i in range(50):
if i == 42:
print('Magic number in range')
print('Magic number not found')
----
That behaves the same way as the noncompliant example but it doesn't contain the suspicious `else`.
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]