Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions python/extractor/semmle/python/passes/pruner.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ def __init__(self):
self.nodes = set()

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

class NonlocalVisitor(ASTVisitor):
def __init__(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
category: fix
---

- `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.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,3 @@
| test.py:21:5:21:38 | For | This statement is unreachable. |
| test.py:28:9:28:21 | ExprStmt | This statement is unreachable. |
| test.py:84:5:84:21 | ExceptStmt | This statement is unreachable. |
| test.py:158:9:159:16 | Case | This statement is unreachable. |
| test.py:162:13:162:16 | Pass | This statement is unreachable. |
| test.py:167:13:167:16 | Pass | This statement is unreachable. |