Skip to content

Python: Do not report unreachable "catch-all" cases in elif-chains.#2221

Merged
RasmusWL merged 1 commit intogithub:masterfrom
tausbn:python-unreachable-catch-all-assert
Oct 29, 2019
Merged

Python: Do not report unreachable "catch-all" cases in elif-chains.#2221
RasmusWL merged 1 commit intogithub:masterfrom
tausbn:python-unreachable-catch-all-assert

Conversation

@tausbn
Copy link
Contributor

@tausbn tausbn commented Oct 29, 2019

This was brought up on the LGTM.com forums here:
https://discuss.lgtm.com/t/warn-when-always-failing-assert-is-reachable-rather-than-unreachable/2436

I think it's a fair point. This is a clear instance where the alert, even if it is technically correct, is not useful for the end user.

Also, I decided to make the exclusion fairly restrictive: it only applies if the unreachable statement is an assert False, ... or raise ..., and only if said statement is the first in the else block. Any other statements will still be reported.

(Also applied a bit of autoformatting, which led to a few changes to existing code. It seems this hadn't been done to the entire file previously.)

Copy link
Member

@RasmusWL RasmusWL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

I'm not too big a fan of the name catch_all_exception though. It sounds too much like catching all exceptions (as in the code below). Could we name it just catch_all?

try:
    ...
except Exception:
    ...

@tausbn
Copy link
Contributor Author

tausbn commented Oct 29, 2019

Perhaps something like raises_exception_in_catch_all would be a bit more descriptive? (Also, if you want to catch all exceptions, you could just do except: .... Indeed, this will also catch things that extend BaseException directly. 🙂)

@RasmusWL
Copy link
Member

raises_exception_in_catch_all could still mean

except:
    raise Exception('naming is hard')

😄

@tausbn
Copy link
Contributor Author

tausbn commented Oct 29, 2019

Very well, raises_exception_in_final_catch_all_else_block_in_a_chain_of_if_elif_else_statements it is, then. :trollface:

This was brought up on the LGTM.com forums here:
https://discuss.lgtm.com/t/warn-when-always-failing-assert-is-reachable-rather-than-unreachable/2436

Essentially, in a complex chain of `elif` statements, like

```python
if x < 0:
    ...
elif x >= 0:
    ...
else:
    ...
```

the `else` clause is redundant, since the preceding conditions completely
exhaust the possible values for `x` (assuming `x` is an integer). Rather than
promoting the final `elif` clause to an `else` clause, it is common to instead
raise an explicit exception in the `else` clause. During execution, this
exception will never actually be raised, but its presence indicates that the
preceding conditions are intended to cover all possible cases.

I think it's a fair point. This is a clear instance where the alert, even if it
is technically correct, is not useful for the end user.

Also, I decided to make the exclusion fairly restrictive: it only applies if
the unreachable statement is an `assert False, ...` or `raise ...`, and only
if said statement is the first in the `else` block. Any other statements will
still be reported.
@taus-semmle taus-semmle force-pushed the python-unreachable-catch-all-assert branch from dd0be70 to 5e62da7 Compare October 29, 2019 14:31
@RasmusWL RasmusWL merged commit 87ec58a into github:master Oct 29, 2019
@tausbn tausbn deleted the python-unreachable-catch-all-assert branch February 12, 2021 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants