Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9035ba1

Browse files
committed
Fix isImplicitInit; use it in empty-container query
1 parent 7106ec7 commit 9035ba1

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,22 +1657,41 @@ class LocalVariableDeclExpr extends Expr, @localvariabledeclexpr {
16571657

16581658
/**
16591659
* Gets the switch statement or expression whose pattern declares this identifier, if any.
1660-
*
1661-
* Note this only applies to a direct binding pattern, such as `case T t`, not a record pattern.
16621660
*/
1663-
StmtParent getAssociatedSwitch() { result = this.getParent().(PatternCase).getParent() }
1661+
StmtParent getAssociatedSwitch() {
1662+
exists(PatternCase pc |
1663+
pc = result.(SwitchStmt).getAPatternCase()
1664+
or
1665+
pc = result.(SwitchExpr).getAPatternCase()
1666+
|
1667+
this = pc.getPattern().getAChildExpr*()
1668+
)
1669+
}
16641670

16651671
/** Holds if this is a declaration stemming from a pattern switch case. */
16661672
predicate hasAssociatedSwitch() { exists(this.getAssociatedSwitch()) }
16671673

1674+
/**
1675+
* Gets the instanceof expression whose pattern declares this identifier, if any.
1676+
*/
1677+
InstanceOfExpr getAssociatedInstanceOfExpr() {
1678+
result.getPattern().getAChildExpr*() = this
1679+
}
1680+
1681+
/** Holds f this is a declaration stemming from a pattern instanceof expression. */
1682+
predicate hasAssociatedInstanceOfExpr() {
1683+
exists(this.getAssociatedInstanceOfExpr())
1684+
}
1685+
16681686
/** Gets the initializer expression of this local variable declaration expression, if any. */
16691687
Expr getInit() { result.isNthChildOf(this, 0) }
16701688

16711689
/** Holds if this variable declaration implicitly initializes the variable. */
16721690
predicate hasImplicitInit() {
16731691
exists(CatchClause cc | cc.getVariable() = this) or
16741692
exists(EnhancedForStmt efs | efs.getVariable() = this) or
1675-
this.hasAssociatedSwitch()
1693+
this.hasAssociatedSwitch() or
1694+
this.hasAssociatedInstanceOfExpr()
16761695
}
16771696

16781697
/** Gets a printable representation of this expression. */

java/ql/src/Likely Bugs/Collections/ReadOnlyContainer.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ where
4040
) and
4141
// Also, any value that `v` is initialized to is a fresh container,
4242
forall(Expr e | e = v.getAnAssignedValue() | e instanceof FreshContainer) and
43-
// and `v` is not implicitly initialized by a for-each loop.
44-
not exists(EnhancedForStmt efs | efs.getVariable().getVariable() = v)
43+
// and `v` is not implicitly initialized.
44+
not v.(LocalVariableDecl).getDeclExpr().hasImplicitInit()
4545
select v, "The contents of this container are never initialized."

0 commit comments

Comments
 (0)