Skip to content

Commit fdd498c

Browse files
committed
Improve throw-null-literal.ql
Tested query with Variant Analysis and had multiple results.
1 parent 666f6cf commit fdd498c

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

codeql-custom-queries-java/not-tested-queries/throw-null-literal.ql

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Finds `throw` statements which throw the `null` literal, i.e. `throw null;`.
3+
* While this does produce a `NullPointerException` (as probably desired)
4+
* it is rather obscure and misuses the `null` check of the `throw` statement.
5+
* For clarity it would be better to explicitly create a `NullPointerException`
6+
* and possibly provide a meaningful message to its constructor.
7+
*
8+
* See also Error Prone pattern [ThrowNull](https://errorprone.info/bugpattern/ThrowNull).
9+
*
10+
* @kind problem
11+
*/
12+
13+
import java
14+
15+
from ThrowStmt throwStmt
16+
where
17+
throwStmt.getExpr() instanceof NullLiteral
18+
select throwStmt, "Should use `throw new NullPointerException(...)` instead"

0 commit comments

Comments
 (0)