Skip to content

Commit 87ec58a

Browse files
authored
Merge pull request #2221 from tausbn/python-unreachable-catch-all-assert
Python: Do not report unreachable "catch-all" cases in `elif`-chains.
2 parents 6e6dab9 + 5e62da7 commit 87ec58a

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

python/ql/src/Statements/UnreachableCode.ql

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ import python
1616
predicate typing_import(ImportingStmt is) {
1717
exists(Module m |
1818
is.getScope() = m and
19-
exists(TypeHintComment tc |
20-
tc.getLocation().getFile() = m.getFile()
21-
)
19+
exists(TypeHintComment tc | tc.getLocation().getFile() = m.getFile())
2220
)
2321
}
2422

@@ -39,18 +37,26 @@ predicate suppression_in_scope(Stmt s) {
3937
)
4038
}
4139

40+
/** Holds if `s` is a statement that raises an exception at the end of an if-elif-else chain. */
41+
predicate marks_an_impossible_else_branch(Stmt s) {
42+
exists(If i | i.getOrelse().getItem(0) = s |
43+
s.(Assert).getTest() instanceof False
44+
or
45+
s instanceof Raise
46+
)
47+
}
48+
4249
predicate reportable_unreachable(Stmt s) {
4350
s.isUnreachable() and
4451
not typing_import(s) and
4552
not suppression_in_scope(s) and
4653
not exists(Stmt other | other.isUnreachable() |
4754
other.contains(s)
4855
or
49-
exists(StmtList l, int i, int j |
50-
l.getItem(i) = other and l.getItem(j) = s and i < j
51-
)
56+
exists(StmtList l, int i, int j | l.getItem(i) = other and l.getItem(j) = s and i < j)
5257
) and
53-
not unique_yield(s)
58+
not unique_yield(s) and
59+
not marks_an_impossible_else_branch(s)
5460
}
5561

5662
from Stmt s

python/ql/test/query-tests/Statements/unreachable/test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,20 @@ def foo(x):
120120
print(x, "== 0")
121121

122122

123+
# Unreachable catch-all case
124+
125+
def unreachable_catch_all_assert_false(x):
126+
if x < 0:
127+
return "negative"
128+
elif x >= 0:
129+
return "positive"
130+
else:
131+
assert False, x
132+
133+
def unreachable_catch_all_raise(x):
134+
if x < 0:
135+
pass
136+
elif x >= 0:
137+
pass
138+
else:
139+
raise ValueError(x)

0 commit comments

Comments
 (0)