Fix isImplicitInit; use it in empty-container query

This commit is contained in:
Chris Smowton
2023-11-06 11:49:44 +00:00
parent 7106ec77bc
commit 9035ba1f30
2 changed files with 25 additions and 6 deletions

View File

@@ -1657,14 +1657,32 @@ class LocalVariableDeclExpr extends Expr, @localvariabledeclexpr {
/**
* Gets the switch statement or expression whose pattern declares this identifier, if any.
*
* Note this only applies to a direct binding pattern, such as `case T t`, not a record pattern.
*/
StmtParent getAssociatedSwitch() { result = this.getParent().(PatternCase).getParent() }
StmtParent getAssociatedSwitch() {
exists(PatternCase pc |
pc = result.(SwitchStmt).getAPatternCase()
or
pc = result.(SwitchExpr).getAPatternCase()
|
this = pc.getPattern().getAChildExpr*()
)
}
/** Holds if this is a declaration stemming from a pattern switch case. */
predicate hasAssociatedSwitch() { exists(this.getAssociatedSwitch()) }
/**
* Gets the instanceof expression whose pattern declares this identifier, if any.
*/
InstanceOfExpr getAssociatedInstanceOfExpr() {
result.getPattern().getAChildExpr*() = this
}
/** Holds f this is a declaration stemming from a pattern instanceof expression. */
predicate hasAssociatedInstanceOfExpr() {
exists(this.getAssociatedInstanceOfExpr())
}
/** Gets the initializer expression of this local variable declaration expression, if any. */
Expr getInit() { result.isNthChildOf(this, 0) }
@@ -1672,7 +1690,8 @@ class LocalVariableDeclExpr extends Expr, @localvariabledeclexpr {
predicate hasImplicitInit() {
exists(CatchClause cc | cc.getVariable() = this) or
exists(EnhancedForStmt efs | efs.getVariable() = this) or
this.hasAssociatedSwitch()
this.hasAssociatedSwitch() or
this.hasAssociatedInstanceOfExpr()
}
/** Gets a printable representation of this expression. */

View File

@@ -40,6 +40,6 @@ where
) and
// Also, any value that `v` is initialized to is a fresh container,
forall(Expr e | e = v.getAnAssignedValue() | e instanceof FreshContainer) and
// and `v` is not implicitly initialized by a for-each loop.
not exists(EnhancedForStmt efs | efs.getVariable().getVariable() = v)
// and `v` is not implicitly initialized.
not v.(LocalVariableDecl).getDeclExpr().hasImplicitInit()
select v, "The contents of this container are never initialized."