Skip to content

Commit 6061730

Browse files
committed
Java: Add InstanceOfExpr.getCheckedType()
Additionally change `EqualsUsesInstanceOf.ql` to check for all RefTypes instead of only Class.
1 parent 0e9d36b commit 6061730

File tree

12 files changed

+18
-15
lines changed

12 files changed

+18
-15
lines changed

java/ql/src/Language Abuse/DubiousTypeTestOfThis.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ from InstanceOfExpr ioe, RefType t, RefType ct
1717
where
1818
ioe.getExpr() instanceof ThisAccess and
1919
t = ioe.getExpr().getType() and
20-
ct = ioe.getTypeName().getType() and
20+
ct = ioe.getCheckedType() and
2121
ct.getASupertype*() = t
2222
select ioe,
2323
"Testing whether 'this' is an instance of $@ in $@ introduces a dependency cycle between the two types.",

java/ql/src/Language Abuse/UselessTypeTest.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import java
1515
from InstanceOfExpr ioe, RefType t, RefType ct
1616
where
1717
t = ioe.getExpr().getType() and
18-
ct = ioe.getTypeName().getType() and
18+
ct = ioe.getCheckedType() and
1919
ct = t.getASupertype+()
2020
select ioe,
2121
"There is no need to test whether an instance of $@ is also an instance of $@ - it always is.", t,

java/ql/src/Likely Bugs/Comparison/EqualsUsesInstanceOf.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ predicate instanceofInEquals(EqualsMethod m, InstanceOfExpr e) {
1717
m.fromSource() and
1818
e.getEnclosingCallable() = m and
1919
e.getExpr().(VarAccess).getVariable() = m.getParameter() and
20-
exists(Class instanceofType |
21-
instanceofType = e.getTypeName().getType() and
20+
exists(RefType instanceofType |
21+
instanceofType = e.getCheckedType() and
2222
not instanceofType.isFinal()
2323
)
2424
}

java/ql/src/Likely Bugs/Likely Typos/ContradictoryTypeChecks.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import semmle.code.java.dataflow.SSA
1818
/** `ioe` is of the form `va instanceof t`. */
1919
predicate instanceOfCheck(InstanceOfExpr ioe, VarAccess va, RefType t) {
2020
ioe.getExpr() = va and
21-
ioe.getTypeName().getType().(RefType).getSourceDeclaration() = t
21+
ioe.getCheckedType().getSourceDeclaration() = t
2222
}
2323

2424
/** Expression `e` assumes that `va` could be of type `t`. */

java/ql/src/Violations of Best Practice/Implementation Hiding/AbstractToConcreteCollection.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import java
1616
import semmle.code.java.Collections
1717

1818
predicate guardedByInstanceOf(VarAccess e, RefType t) {
19-
exists(IfStmt s, InstanceOfExpr instanceCheck, Type checkType |
19+
exists(IfStmt s, InstanceOfExpr instanceCheck, RefType checkType |
2020
s.getCondition() = instanceCheck and
21-
instanceCheck.getTypeName().getType() = checkType and
21+
instanceCheck.getCheckedType() = checkType and
2222
// The same variable appears as the subject of the `instanceof`.
2323
instanceCheck.getExpr() = e.getVariable().getAnAccess() and
2424
// The checked type is either the type itself, or a raw version. For example, it is usually

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ predicate depends(RefType t, RefType dep) {
7676
or
7777
// the type accessed in an `instanceof` expression in `t`.
7878
exists(InstanceOfExpr ioe | t = ioe.getEnclosingCallable().getDeclaringType() |
79-
usesType(ioe.getTypeName().getType(), dep)
79+
usesType(ioe.getCheckedType(), dep)
8080
)
8181
)
8282
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ predicate numDepends(RefType t, RefType dep, int value) {
100100
elem = ioe and
101101
t = ioe.getEnclosingCallable().getDeclaringType()
102102
|
103-
usesType(ioe.getTypeName().getType(), dep)
103+
usesType(ioe.getCheckedType(), dep)
104104
)
105105
)
106106
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,9 @@ class InstanceOfExpr extends Expr, @instanceofexpr {
13331333
/** Gets the access to the type on the right-hand side of the `instanceof` operator. */
13341334
Expr getTypeName() { result.isNthChildOf(this, 1) }
13351335

1336+
/** Gets the type this `instanceof` expression checks for. */
1337+
RefType getCheckedType() { result = getTypeName().getType() }
1338+
13361339
/** Gets a printable representation of this expression. */
13371340
override string toString() { result = "...instanceof..." }
13381341

java/ql/src/semmle/code/java/dataflow/NullGuards.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ Expr enumConstEquality(Expr e, boolean polarity, EnumConstant c) {
2525
}
2626

2727
/** Gets an instanceof expression of `v` with type `type` */
28-
InstanceOfExpr instanceofExpr(SsaVariable v, Type type) {
29-
result.getTypeName().getType() = type and
28+
InstanceOfExpr instanceofExpr(SsaVariable v, RefType type) {
29+
result.getCheckedType() = type and
3030
result.getExpr() = v.getAUse()
3131
}
3232

java/ql/src/semmle/code/java/dataflow/Nullness.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ private predicate correlatedConditions(
504504
inverted = pol1.booleanXor(pol2)
505505
)
506506
or
507-
exists(SsaVariable v, Type type |
507+
exists(SsaVariable v, RefType type |
508508
cond1.getCondition() = instanceofExpr(v, type) and
509509
cond2.getCondition() = instanceofExpr(v, type) and
510510
inverted = false

0 commit comments

Comments
 (0)