jendrikseipp / vulture

Find dead Python code
MIT License
3.38k stars 148 forks source link

Should report on unreachable code after try-return-except-raise #270

Open kretes opened 2 years ago

kretes commented 2 years ago

For a following file:

def test() -> int:
    try:
        return 2
    except Exception as e:
        raise e
    return 1
test()

vulture dead_return.py doesn't report anything, while it should report code on line 6, as this one is unreachable.

vulture 2.3, python 3.9

jendrikseipp commented 2 years ago

Thanks for the report! Put a little more generally, Vulture should detect that code is unreachable if it occurs after an if-else block or a try-except block where all cases exit the function (with return or raise).

Here is another test case that Vulture currently misses:

def test2(a) -> int:
    if a:
        return 1
    else:
        return 2
    return 3
test2(5)

I'm happy to review a PR for this :-)

adamchainz commented 1 year ago

Mypy has good unreachable code detection, and it can work for such cases even if you don’t use type hints. https://adamj.eu/tech/2021/05/19/python-type-hints-mypy-unreachable-code-detection/

I am not sure it’s worth duplicating this kind of functionality in vulture.

kykyi commented 1 year ago

I disagree, mostly because this is dead-code and users of vulture are expecting it to find dead-code 🤷