jendrikseipp / vulture

Find dead Python code
MIT License
3.41k stars 150 forks source link

Ignoring intentional unreachable if statements? (if False: ...) #222

Closed jingw closed 4 years ago

jingw commented 4 years ago

How would you feel about loosening _handle_conditional_node to ignore cases where the unreachable branch is obviously intentional? When running vulture on my giant codebase, I found that most of the unreachable code flagged by vulture was of the form if False or if False and other_conditions, though it did also flag buggy code of the form if x == 1 or 2.

Concretely, I propose ignoring the following patterns at the top level in _handle_conditional_node

False
False and ...
True
True or ...

Less obvious cases where the True/False is somewhere in the middle would continue to be reported, as done currently.

I can throw up a PR if you like the idea.

jendrikseipp commented 4 years ago

I see your point and we actually were on the fence about this when we added the unreachable code feature. I think it's best to leave things as they are, because that keeps the code simple and is easiest to document and understand. In cases like these, I'd recommend to use a Boolean flag and I added a corresponding note to the README:

Unreachable code

If Vulture complains about code like if False:, you can use a Boolean flag debug = False and write if debug: instead. This makes the code more readable and silences Vulture.