Skip to content

Commit 3812b3e

Browse files
committed
Java: Improve NullGuards.clearlyNotNullExpr()
1 parent a7030c7 commit 3812b3e

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

java/ql/src/semmle/code/java/dataflow/NullGuards.qll

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,22 @@ Expr clearlyNotNullExpr(Expr reason) {
4444
or
4545
result instanceof ArrayCreationExpr and reason = result
4646
or
47+
result instanceof FunctionalExpr and reason = result
48+
or
4749
result instanceof TypeLiteral and reason = result
4850
or
4951
result instanceof ThisAccess and reason = result
5052
or
5153
result instanceof StringLiteral and reason = result
5254
or
55+
// Add and AssignAdd performing String concatenation never have null result
5356
result instanceof AddExpr and result.getType() instanceof TypeString and reason = result
5457
or
55-
exists(Field f |
56-
result = f.getAnAccess() and
57-
(f.hasName("TRUE") or f.hasName("FALSE")) and
58-
f.getDeclaringType().hasQualifiedName("java.lang", "Boolean") and
59-
reason = result
60-
)
58+
result instanceof AssignAddExpr and result.getType() instanceof TypeString and reason = result
59+
or
60+
result.getType() instanceof PrimitiveType and reason = result
61+
or
62+
result instanceof CompileTimeConstantExpr and reason = result
6163
or
6264
result.(CastExpr).getExpr() = clearlyNotNullExpr(reason)
6365
or
@@ -70,6 +72,11 @@ Expr clearlyNotNullExpr(Expr reason) {
7072
(reason = r1 or reason = r2)
7173
)
7274
or
75+
exists(SwitchExpr s |
76+
s = result and
77+
forall(Expr resultExpr | resultExpr = s.getAResult() | resultExpr = clearlyNotNullExpr(reason))
78+
)
79+
or
7380
exists(SsaVariable v, boolean branch, RValue rval, Guard guard |
7481
guard = directNullGuard(v, branch, false) and
7582
guard.controls(rval.getBasicBlock(), branch) and

0 commit comments

Comments
 (0)