-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathUnreadLocal.ql
More file actions
36 lines (32 loc) · 1.1 KB
/
UnreadLocal.ql
File metadata and controls
36 lines (32 loc) · 1.1 KB
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
33
34
35
36
/**
* @name Unread local variable
* @description A local variable that is never read is redundant.
* @kind problem
* @problem.severity recommendation
* @precision high
* @id java/local-variable-is-never-read
* @tags quality
* maintainability
* useless-code
* external/cwe/cwe-561
*/
import java
VarAccess getARead(LocalVariableDecl v) {
v.getAnAccess() = result and
not exists(Assignment assign | assign.getDest() = result)
}
predicate readImplicitly(LocalVariableDecl v) {
exists(TryStmt t | t.getAResourceDecl().getAVariable() = v.getDeclExpr())
}
from LocalVariableDecl v
where
not exists(getARead(v)) and
// Discarded exceptions are covered by another query.
not exists(CatchClause cc | cc.getVariable().getVariable() = v) and
// Exclude common Kotlin pattern to do something n times: `for(i in 1..n) { doSomething() }
not exists(EnhancedForStmt f |
f.getVariable().getVariable() = v and
f.getExpr().getType().(RefType).hasQualifiedName("kotlin.ranges", ["IntRange", "LongRange"])
) and
not readImplicitly(v)
select v, "Variable '" + v + "' is never read."