Unused Local Variable
ID |
java.unused_local_variable |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
Java |
Tags |
CWE:563, code-style, unused-code |
Description
Reports local variables that are declared but never referenced in the remaining method body.
Rationale
A local variable that is assigned but never read is dead code. It may be a leftover from a refactoring, a copy-paste error, or a sign that the developer intended to use the value but forgot. Dead variables add noise, increase cognitive load, and can mask real bugs when a similarly-named variable is expected.
// Bad -- variable is never used
void process() {
int unused = computeValue();
System.out.println("done");
}
// Good -- variable is used
void process() {
int result = computeValue();
System.out.println(result);
}
Remediation
Remove the unused variable. If the right-hand side has a side effect, keep only the expression statement without the assignment.