Since Checkstyle 9.3
Checks that a local variable is declared and/or assigned, but not used. Doesn't support pattern variables yet. Doesn't check array components as array components are classified as different kind of variables by JLS.
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="UnusedLocalVariable"/>
</module>
</module>
Example:
class Example1 {
{
int k = 12; // violation, assign and update but never use 'k'
k++;
}
int a;
Example1(int a) { // ok, as 'a' is a constructor parameter not a local variable
this.a = 12;
}
void method(int b) {
int[] arr = {1, 2, 3}; // violation, unused local variable 'arr'
int[] anotherArr = {1}; // ok, 'anotherArr' is accessed
anotherArr[0] = 4;
}
String convertValue(String newValue) {
String s = newValue.toLowerCase(); // violation, unused local variable 's'
return newValue.toLowerCase();
}
void read() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s; // violation, unused local variable 's'
while ((s = reader.readLine()) != null) {}
try (BufferedReader reader1 = // ok, 'reader1' is a resource
new BufferedReader(new FileReader("abc.txt"))) {}
try {
} catch (Exception e) { // ok, 'e' is an exception parameter
}
}
void loops() {
int j = 12;
for (int i = 0; j < 11; i++) { // violation, unused local variable 'i'
}
for (int p = 0; j < 11; p++) // ok, 'p' is used
p /= 2;
}
void lambdas() {
Predicate<String> obj = (String str) -> true; // ok, 'str' is a lambda parameter
obj.test("Test");
}
}
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.coding