org.sonar.l10n.py.rules.python.S2711.html Maven / Gradle / Ivy
This rule raises an issue when return
or yield
are used outside of a function.
Why is this an issue?
yield
and return
only make sense in the context of functions. Using them outside a function raises a
SyntaxError
.
If the goal is to break out of a loop, use break
instead of return
.
Code examples
Noncompliant code example
a = 1
while a < 3:
if a % 2 == 0:
return # Noncompliant: return is outside of a function
a += 1
for n in range(5):
yield n # Noncompliant: yield is outside of a function
Compliant solution
a = 1
while a < 3:
if a % 2 == 0:
break
a += 1
def gen():
for n in range(5):
yield n
Resources
Documentation
- Python Documentation - Yield expressions
- Python Documentation - Return
statement
© 2015 - 2024 Weber Informatics LLC | Privacy Policy