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
32 lines (30 loc) · 1007 Bytes
/
UselessNullCheck.ql
File metadata and controls
32 lines (30 loc) · 1007 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
29
30
31
32
/**
* @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 quality
* 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 at this check, since it is guarded by $@."
else
if reason != e
then
msg = "This check is useless. $@ cannot be null at this check, 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()