Kotlin: Improve java/abstract-to-concrete-cast to handle when branches

This commit is contained in:
Tamas Vajk
2022-10-25 10:17:43 +02:00
parent 690d6517d7
commit a0490f454b
2 changed files with 10 additions and 4 deletions

View File

@@ -16,8 +16,11 @@ import java
import semmle.code.java.Collections
predicate guardedByInstanceOf(VarAccess e, RefType t) {
exists(IfStmt s, InstanceOfExpr instanceCheck, RefType checkType |
s.getCondition() = instanceCheck and
exists(Stmt s, InstanceOfExpr instanceCheck, RefType checkType |
(
s.(IfStmt).getCondition() = instanceCheck or
s.(WhenBranch).getCondition() = instanceCheck
) and
instanceCheck.getCheckedType() = checkType and
// The same variable appears as the subject of the `instanceof`.
instanceCheck.getExpr() = e.getVariable().getAnAccess() and
@@ -27,7 +30,11 @@ predicate guardedByInstanceOf(VarAccess e, RefType t) {
(checkType = t or checkType = t.getSourceDeclaration().(GenericType).getRawType()) and
// The expression appears in one of the branches.
// (We do not verify here whether the guard is correctly implemented.)
exists(Stmt branch | branch = s.getThen() or branch = s.getElse() |
exists(Stmt branch |
branch = s.(IfStmt).getThen() or
branch = s.(IfStmt).getElse() or
branch = s.(WhenBranch).getRhs()
|
branch = e.getEnclosingStmt().getEnclosingStmt+()
)
)

View File

@@ -1 +0,0 @@
| 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> |