afonasev / flake8-return

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

Incorrect error reporting R507 for `elif` + `continue` if after the `continue` statement `elif` is following #127

Open sshishov opened 1 year ago

sshishov commented 1 year ago

Description

Invalid error reported for R507 because of some complex context

What I Did

We have some if...elif... clause like this:

def main():
    for a in range(10):                     # | 0 div 4
        if a % 4 == 0:                      # | 1 div nothing
            print(a, 'div', 4)              # | 2 div 2
        elif a % 3 == 0:                    # | 3 div 3
            print(a, 'div', 3)              # | 4 div 4
            continue                        # | 5 div nothing
        elif a % 2 == 0:                    # | 6 div 3
            print(a, 'div', 2)              # | 7 div nothing
        else:                               # | 8 div 4
            print(a, 'div', 'nothing')      # | 9 div 3
    return 0

if __name__ == '__main__':
    main()

For this case we are getting this error:

myfile.py:5:9: R507 unnecessary elif after continue statement.
        elif a % 3 == 0:
        ^

If we follow the suggestion and fix the issue, then we have different output:

def main():                                 # | 0 div 4
    for a in range(10):                     # | 0 div 2
        if a % 4 == 0:                      # | 1 div nothing
            print(a, 'div', 4)              # | 2 div 2
        elif a % 3 == 0:                    # | 3 div 3
            print(a, 'div', 3)              # | 4 div 4
            continue                        # | 4 div 2
        if a % 2 == 0:                      # | 5 div nothing
            print(a, 'div', 2)              # | 6 div 3
        else:                               # | 7 div nothing
            print(a, 'div', 'nothing')      # | 8 div 4
    return 0                                # | 8 div 2
                                            # | 9 div 3

if __name__ == '__main__':
    main()

Can we do something to this? I have tried to apply the suggested errors on our codebase and this issue appeared.

kageurufu commented 1 year ago

R506 is affected by this as well