Skip to content

Commit b763342

Browse files
committed
Java 14: account for instanceof pattern matching
1 parent 9d2f768 commit b763342

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,17 @@ deprecated class ParExpr extends Expr, @parexpr {
11301130
/** An `instanceof` expression. */
11311131
class InstanceOfExpr extends Expr, @instanceofexpr {
11321132
/** Gets the expression on the left-hand side of the `instanceof` operator. */
1133-
Expr getExpr() { result.isNthChildOf(this, 0) }
1133+
Expr getExpr() {
1134+
if isPattern()
1135+
then result = getLocalVariableDeclExpr().getInit()
1136+
else result.isNthChildOf(this, 0)
1137+
}
1138+
1139+
/** Holds if this `instanceof` expression uses pattern matching. */
1140+
predicate isPattern() { exists(getLocalVariableDeclExpr()) }
1141+
1142+
/** Gets the local variable declaration of this `instanceof` expression if pattern matching is used. */
1143+
LocalVariableDeclExpr getLocalVariableDeclExpr() { result.isNthChildOf(this, 0) }
11341144

11351145
/** Gets the access to the type on the right-hand side of the `instanceof` operator. */
11361146
Expr getTypeName() { result.isNthChildOf(this, 1) }
@@ -1161,6 +1171,8 @@ class LocalVariableDeclExpr extends Expr, @localvariabledeclexpr {
11611171
exists(ForStmt fs | fs.getAnInit() = this | result.isNthChildOf(fs, 0))
11621172
or
11631173
exists(EnhancedForStmt efs | efs.getVariable() = this | result.isNthChildOf(efs, -1))
1174+
or
1175+
exists(InstanceOfExpr ioe | this.getParent() = ioe | result.isNthChildOf(ioe, 1))
11641176
}
11651177

11661178
/** Gets the name of the variable declared by this local variable declaration expression. */

0 commit comments

Comments
 (0)