afonasev / flake8-return

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

Invalid error reported for R505 simple if - elif pattern #129

Open heavenshell opened 1 year ago

heavenshell commented 1 year ago

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.

calumy commented 1 year ago

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)
williamwissemann commented 1 year ago

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.

cleder commented 1 year ago

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.