astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.
https://docs.astral.sh/ruff
MIT License
32.65k stars 1.09k forks source link

SIM103: false positive #2399

Closed spaceone closed 1 year ago

spaceone commented 1 year ago
def foo(exc):
    if exc.getcode() == 401:
        return False
    else:
        return False

causes: foo.py:2:5: SIM103 Return the condition `exc.getcode() == 401` directly

charliermarsh commented 1 year ago

Definitely a bug, but where did you run into this :joy:

spaceone commented 1 year ago

https://github.com/univention/univention-corporate-server/commit/37e560e5282fab764ff6496131b28bf0d492b900#diff-45af649ef64d42abac0d51f47809588eaf0aff8501ff6fc0dc6a8580134f25c1R141-R144 ;-)

ngnpope commented 1 year ago

Amazing 😆

SIM103 definitely doesn't make sense. It could become:

def foo(exc):
    exc.getcode()  # Still have to call because of potential side effects.
    return False

But then there could also be side effects if someone overloaded __eq__ to do some evil... 🙈

Maybe a check to identify that all branches have the same statements within?