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

Invalid error reported for R505 simple if - elif pattern

Open heavenshell opened this issue 2 years ago • 3 comments

  • Date you used flake8-return: 2022-10-31
  • flake8-return version used, if any: 1.2.0
  • Python version, if any: Python 3.10.6
  • Operating System: macOS

Description

Invalid error reported for R505 simple if - elif pattern

def get_name(name):
    if name == 'foo':
        return 'foo'
    elif name == 'bar':
        return 'bar'

    return 'baz'

What I Did


a.py:2:5: R505 unnecessary elif after return statement.

I think this pattern should be passed.

heavenshell avatar Oct 31 '22 02:10 heavenshell

The R505 rule was ported from pylint as suggested in #14.

As plyint also throws an error in this case, I do not believe this is a false positive.

src/a.py:2:4: R1705: Unnecessary "elif" after "return", remove the leading "el" from "elif" (no-else-return)

calumy avatar Oct 31 '22 09:10 calumy

I see the same behavior within classes using the 1.2.0 version.

def foo(name):
    [...]
    return True

def bar(name):
    if "foo" == "bar": <----
        return 'baz' 

The if statement in bar gets flagged with an R505 because of the return in foo.

williamwissemann avatar Nov 01 '22 13:11 williamwissemann

I encountered a similar issue e.g:

        if op == Op.EQUAL:
            return left_value == right_value
        elif op == Op.NOT_EQUAL:
            return left_value != right_value

I deactivated R505 because of too many false positives.

cleder avatar Nov 23 '22 13:11 cleder