afonasev / flake8-return

Flake8 plugin for return expressions checking.
MIT License
62 stars 69 forks source link

Variable assignment inside try/except blocks is not handled properly #116

Closed BulbaWarrior closed 2 years ago

BulbaWarrior commented 2 years ago

Description

Describe what you were trying to get done. Tell us what happened, what went wrong, and what you expected to happen.

Consider the following functions

def no_exception_loop():
    success = False
    for _ in range(10):
        try:
            success = True
        except Exception:
            print("exception")  # noqa: T201
    return success

def no_exception():
    success = False
    try:
        success = True
    except Exception:
        print("exception")  # noqa: T201
    return success

def exception():
    success = True
    try:
        print("raising")  # noqa: T201
        raise Exception
    except Exception:
        success = False
    return success

The variable success is changed (or may be changed) inside try/except blocks. However, running flake8 generates the following error: R504 unecessary variable assignement before return statement.

What I Did

$ poetry run flake8 --show-source debug.py
Unable to find qualified name for module: debug.py
debug.py:8:12: R504 unecessary variable assignement before return statement.
    return success
           ^
debug.py:17:12: R504 unecessary variable assignement before return statement.
    return success
           ^
debug.py:27:12: R504 unecessary variable assignement before return statement.
    return success
           ^
afonasev commented 2 years ago

Thx, for your issue!