Unused local query: exclude mandatory declarations

This commit is contained in:
Chris Smowton
2023-11-09 15:47:52 +00:00
parent 91774099fa
commit 88d9caff8c

View File

@@ -11,22 +11,13 @@
import java
import DeadLocals
predicate exceptionVariable(LocalVariableDeclExpr ve) {
exists(CatchClause catch | catch.getVariable() = ve)
}
predicate enhancedForVariable(LocalVariableDeclExpr ve) {
exists(EnhancedForStmt for | for.getVariable() = ve)
}
from LocalVariableDeclExpr ve, LocalVariableDecl v
where
v = ve.getVariable() and
not assigned(v) and
not read(v) and
(not exists(ve.getInit()) or exprHasNoEffect(ve.getInit())) and
// Remove contexts where Java forces a variable declaration: enhanced-for and catch clauses.
// Remove contexts where Java forces a variable declaration: enhanced-for, catch clauses and pattern cases.
// Rules about catch clauses belong in an exception handling query
not exceptionVariable(ve) and
not enhancedForVariable(ve)
not ve.hasImplicitInit()
select v, "Variable " + v.getName() + " is not used."