forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUselessNullCheck.ql
More file actions
28 lines (26 loc) · 946 Bytes
/
UselessNullCheck.ql
File metadata and controls
28 lines (26 loc) · 946 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* @name Useless null check
* @description Checking whether an expression is null when that expression cannot
* possibly be null is useless.
* @kind problem
* @problem.severity warning
* @precision very-high
* @id java/useless-null-check
* @tags maintainability
* useless-code
* external/cwe/cwe-561
*/
import java
import semmle.code.java.dataflow.NullGuards
import semmle.code.java.controlflow.Guards
from Expr guard, Expr e, Expr reason, string msg
where
guard = basicNullGuard(e, _, true) and
e = clearlyNotNullExpr(reason) and
(if reason instanceof Guard then
msg = "This check is useless, $@ cannot be null here, since it is guarded by $@."
else if reason != e then
msg = "This check is useless, $@ cannot be null here, since $@ always is non-null."
else
msg = "This check is useless, since $@ always is non-null.")
select guard, msg, e, e.toString(), reason, reason.toString()