afonasev / flake8-return

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

R505+R508 false positive #137

Open luisrayas3 opened 7 months ago

luisrayas3 commented 7 months ago

Description

R505 & R508 produce false-positives when an earlier branch does not return/break.

What I did

$ cat f.py
def f():
    if True:
        print("1")
    elif False:
        return False
    elif True:
        print("2")
    return True

if __name__ == "__main__":
    f()
$ flake3 f.py
/tmp/f.py:6:10: R505 unnecessary elif after return statement.
    elif False:
         ^
...
1     R505 unnecessary elif after return statement.
...

Same thing if the if-chain is contained in a loop and the return is a break instead.

Note that not using an elif after causes a behavior change: print("2") will be called when it shouldn't be. These warnings should only trigger if every case in the if/elif/else chain returns, breaks, or raises.