flake8-return icon indicating copy to clipboard operation
flake8-return copied to clipboard

R505+R508 false positive

Open luisrayas3 opened this issue 2 years ago • 0 comments

  • Date you used flake8-return: 2024-01-24
  • flake8-return version used, if any: 1.2.0
  • Python version, if any: 3.7
  • Operating System: Linux (Ubuntu)

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.

luisrayas3 avatar Jan 24 '24 21:01 luisrayas3