python.lang.maintainability.return.code-after-unconditional-return

profile photo of semgrepsemgrep
Author
6,393
Download Count*

code after return statement will not be executed

Run Locally

Run in CI

Defintion

rules:
  - id: code-after-unconditional-return
    pattern: |
      return ...
      $S
    message: code after return statement will not be executed
    languages:
      - python
    severity: WARNING
    metadata:
      category: maintainability
      technology:
        - python
      license: Commons Clause License Condition v1.0[LGPL-2.1-only]

Examples

return.py



def alwaysblue():
    if isblue():
        return 'blue'
    # ruleid: code-after-unconditional-return
    return 'red'
    return 'green'


def alwaysblue():
    if isblue():
        return 'blue'
    # ruleid: code-after-unconditional-return
    return 'red'
    x = 5


def resolve(key: str):
    key = os.path.join(path, "keys", key)
    # ok: code-after-unconditional-return
    return key


def resolve(key: str) -> str:
    key = os.path.join(path, "keys", key)
    # ok: code-after-unconditional-return
    return key

def resolve(key: str) -> str:
    key = os.path.join(path, "keys", key)
    # ok: code-after-unconditional-return
    return key, key

# ruleid: return-not-in-function
return (a, b)