Skip to content

Commit 88d9caf

Browse files
committed
Unused local query: exclude mandatory declarations
1 parent 9177409 commit 88d9caf

File tree

1 file changed

+2
-11
lines changed

1 file changed

+2
-11
lines changed

java/ql/src/Violations of Best Practice/Dead Code/UnusedLocal.ql

+2-11
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,13 @@
1111
import java
1212
import DeadLocals
1313

14-
predicate exceptionVariable(LocalVariableDeclExpr ve) {
15-
exists(CatchClause catch | catch.getVariable() = ve)
16-
}
17-
18-
predicate enhancedForVariable(LocalVariableDeclExpr ve) {
19-
exists(EnhancedForStmt for | for.getVariable() = ve)
20-
}
21-
2214
from LocalVariableDeclExpr ve, LocalVariableDecl v
2315
where
2416
v = ve.getVariable() and
2517
not assigned(v) and
2618
not read(v) and
2719
(not exists(ve.getInit()) or exprHasNoEffect(ve.getInit())) and
28-
// Remove contexts where Java forces a variable declaration: enhanced-for and catch clauses.
20+
// Remove contexts where Java forces a variable declaration: enhanced-for, catch clauses and pattern cases.
2921
// Rules about catch clauses belong in an exception handling query
30-
not exceptionVariable(ve) and
31-
not enhancedForVariable(ve)
22+
not ve.hasImplicitInit()
3223
select v, "Variable " + v.getName() + " is not used."

0 commit comments

Comments
 (0)