Nullness library: recognise switches with null checks

This commit is contained in:
Chris Smowton
2023-10-27 16:14:00 +01:00
parent 79b77ae805
commit 6b9aed21df
6 changed files with 36 additions and 3 deletions

View File

@@ -1542,6 +1542,11 @@ class SwitchExpr extends Expr, StmtParent, @switchexpr {
exists(YieldStmt yield | yield.getTarget() = this and result = yield.getValue())
}
/** Holds if this switch has a case handling a null literal. */
predicate hasNullCase() {
this.getAConstCase().getValue(_) instanceof NullLiteral
}
/** Gets a printable representation of this expression. */
override string toString() { result = "switch (...)" }

View File

@@ -409,6 +409,11 @@ class SwitchStmt extends Stmt, @switchstmt {
/** Gets the expression of this `switch` statement. */
Expr getExpr() { result.getParent() = this }
/** Holds if this switch has a case handling a null literal. */
predicate hasNullCase() {
this.getAConstCase().getValue(_) instanceof NullLiteral
}
override string pp() { result = "switch (...)" }
override string toString() { result = "switch (...)" }

View File

@@ -100,9 +100,9 @@ predicate dereference(Expr e) {
or
exists(SynchronizedStmt synch | synch.getExpr() = e)
or
exists(SwitchStmt switch | switch.getExpr() = e)
exists(SwitchStmt switch | switch.getExpr() = e and not switch.hasNullCase())
or
exists(SwitchExpr switch | switch.getExpr() = e)
exists(SwitchExpr switch | switch.getExpr() = e and not switch.hasNullCase())
or
exists(FieldAccess fa, Field f | fa.getQualifier() = e and fa.getField() = f and not f.isStatic())
or