Skip to content

Commit a0490f4

Browse files
committed
Kotlin: Improve java/abstract-to-concrete-cast to handle when branches
1 parent 690d651 commit a0490f4

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

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

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

1818
predicate guardedByInstanceOf(VarAccess e, RefType t) {
19-
exists(IfStmt s, InstanceOfExpr instanceCheck, RefType checkType |
20-
s.getCondition() = instanceCheck and
19+
exists(Stmt s, InstanceOfExpr instanceCheck, RefType checkType |
20+
(
21+
s.(IfStmt).getCondition() = instanceCheck or
22+
s.(WhenBranch).getCondition() = instanceCheck
23+
) and
2124
instanceCheck.getCheckedType() = checkType and
2225
// The same variable appears as the subject of the `instanceof`.
2326
instanceCheck.getExpr() = e.getVariable().getAnAccess() and
@@ -27,7 +30,11 @@ predicate guardedByInstanceOf(VarAccess e, RefType t) {
2730
(checkType = t or checkType = t.getSourceDeclaration().(GenericType).getRawType()) and
2831
// The expression appears in one of the branches.
2932
// (We do not verify here whether the guard is correctly implemented.)
30-
exists(Stmt branch | branch = s.getThen() or branch = s.getElse() |
33+
exists(Stmt branch |
34+
branch = s.(IfStmt).getThen() or
35+
branch = s.(IfStmt).getElse() or
36+
branch = s.(WhenBranch).getRhs()
37+
|
3138
branch = e.getEnclosingStmt().getEnclosingStmt+()
3239
)
3340
)
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
| Test.kt:3:9:3:9 | <implicit cast> | $@ is cast to the concrete type $@, losing abstraction. | file:///modules/java.base/java/util/List.class:0:0:0:0 | List | List<Integer> | file:///modules/java.base/java/util/ArrayList.class:0:0:0:0 | ArrayList | ArrayList<Integer> |

0 commit comments

Comments
 (0)