Skip to content

Commit 10a9b78

Browse files
authored
Merge pull request #18738 from github/tausbn/python-fix-match-pruning-logic
Python: Don't prune any `MatchLiteralPattern`s
2 parents 40903a9 + 6546bb1 commit 10a9b78

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

python/extractor/semmle/python/passes/pruner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,8 @@ def __init__(self):
203203
self.nodes = set()
204204

205205
def visit_MatchLiteralPattern(self, node):
206-
# MatchLiteralPatterns _look_ like boolean tests, but are not.
206+
# MatchLiteralPatterns _look_ like boolean tests in that they have both a true ("matched")
207+
# and false ("didn't match") successor, but are not.
207208
# Thus, without this check, we would interpret
208209
#
209210
# match x:
@@ -212,8 +213,7 @@ def visit_MatchLiteralPattern(self, node):
212213
#
213214
# (and similarly for True) as if it was a boolean test. This would cause the true edge
214215
# (leading to pass) to be pruned later on.
215-
if isinstance(node.literal, ast.Name) and node.literal.id in ('True', 'False'):
216-
self.nodes.add(node.literal)
216+
self.nodes.add(node.literal)
217217

218218
class NonlocalVisitor(ASTVisitor):
219219
def __init__(self):
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
category: fix
3+
---
4+
5+
- `MatchLiteralPattern`s such as `case None: ...` are now never pruned from the extracted source code. This fixes some situations where code was wrongly identified as unreachable.

python/ql/test/query-tests/Statements/unreachable/UnreachableCode.expected

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,3 @@
44
| test.py:21:5:21:38 | For | This statement is unreachable. |
55
| test.py:28:9:28:21 | ExprStmt | This statement is unreachable. |
66
| test.py:84:5:84:21 | ExceptStmt | This statement is unreachable. |
7-
| test.py:158:9:159:16 | Case | This statement is unreachable. |
8-
| test.py:162:13:162:16 | Pass | This statement is unreachable. |
9-
| test.py:167:13:167:16 | Pass | This statement is unreachable. |

0 commit comments

Comments
 (0)