Python: Don't prune any MatchLiteralPatterns

Extends the mechanism introduced in
https://github.com/github/codeql/pull/18030
to behave the same for _all_ `MatchLiteralPattern`s, not just the ones
that happen to be the constant `True` or `False`.

Co-authored-by: yoff <yoff@github.com>
This commit is contained in:
Taus
2025-02-11 12:58:52 +00:00
parent 7476d9ed6b
commit 918c05c538
2 changed files with 3 additions and 6 deletions

View File

@@ -203,7 +203,8 @@ class NotBooleanTestVisitor(ASTVisitor):
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:
@@ -212,8 +213,7 @@ class NotBooleanTestVisitor(ASTVisitor):
#
# (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):

View File

@@ -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. |