All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.l10n.py.rules.python.S2711.html Maven / Gradle / Ivy

There is a newer version: 4.23.0.17664
Show newest version

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





© 2015 - 2024 Weber Informatics LLC | Privacy Policy