Skip to content

Commit 17193ac

Browse files
committed
Distinguish record patterns that do or don't declare identifiers
1 parent a440196 commit 17193ac

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

java/ql/lib/semmle/code/java/Expr.qll

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2739,4 +2739,14 @@ class RecordPatternExpr extends Expr, @recordpatternexpr {
27392739
)
27402740
)
27412741
}
2742+
2743+
/**
2744+
* Holds if this record pattern declares any identifiers (i.e., at least one leaf declaration is named).
2745+
*/
2746+
predicate declaresAnyIdentifiers() {
2747+
exists(PatternExpr subPattern | subPattern = this.getSubPattern(_) |
2748+
subPattern.asRecordPattern().declaresAnyIdentifiers() or
2749+
not subPattern.asBindingOrUnnamedPattern().isAnonymous()
2750+
)
2751+
}
27422752
}

java/ql/lib/semmle/code/java/dataflow/internal/DataFlowUtil.qll

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,17 @@ predicate simpleAstFlowStep(Expr e1, Expr e2) {
194194
// In the following three cases only record patterns need this flow edge, leading from the bound instanceof
195195
// or switch tested expression to a record pattern that will read its fields. Simple binding patterns are
196196
// handled via VariableAssign.getSource instead.
197-
// We only consider unique patterns because cases that declare multiple patterns are not allowed to declare
198-
// any identifiers, so can't participate in dataflow.
199-
exists(SwitchExpr se |
200-
e1 = se.getExpr() and e2 = se.getACase().(PatternCase).getUniquePattern().asRecordPattern()
197+
// We only consider patterns that declare any identifiers
198+
exists(SwitchExpr se, RecordPatternExpr recordPattern | recordPattern = e2 |
199+
e1 = se.getExpr() and
200+
recordPattern = se.getACase().(PatternCase).getAPattern() and
201+
recordPattern.declaresAnyIdentifiers()
201202
)
202203
or
203-
exists(SwitchStmt ss |
204-
e1 = ss.getExpr() and e2 = ss.getACase().(PatternCase).getUniquePattern().asRecordPattern()
204+
exists(SwitchStmt ss, RecordPatternExpr recordPattern | recordPattern = e2 |
205+
e1 = ss.getExpr() and
206+
recordPattern = ss.getACase().(PatternCase).getAPattern() and
207+
recordPattern.declaresAnyIdentifiers()
205208
)
206209
or
207210
exists(InstanceOfExpr ioe | e1 = ioe.getExpr() and e2 = ioe.getPattern().asRecordPattern())

0 commit comments

Comments
 (0)