Skip to content

unused-local-variable

JavaRubycleanupno fixdefault: offruby/rubocop

Reports a local variable that is declared, and perhaps assigned, but never read. No fix.

A variable that is assigned and never read is dead code. It is a leftover of an earlier version, or a sign that the code was meant to use it (a misspelled name, a result that is forgotten), and it misleads the reader about what the code computes.

To fix, remove the variable, or use it.

Reported
...
class A {
int f(int a, List<String> xs) {
int unused = 1;
int written = 0;
written = 2;
int used = a;
int counter = 0;
...
}
for (int i = 0; i < 3; i++) { // keep
}
Runnable r = () -> {
int inner = 1;
};
return used;
}
...
java:
lint:
rules:
unused-local-variable: warn # off (default), warn, error
ruby:
lint:
rules:
unused-local-variable: warn # off (default), warn, error

Enabled by the presets: ruby/rubocop.

Suppress one line with sudsy-disable-next-line unused-local-variable in a comment of the file’s language.

The same check in other tools. full: Sudsy reports what the upstream rule reports; partial: only part of it, as the last column says.

Tool Rule Match What differs
checkstyle UnusedLocalVariable full
rubocop Lint/UselessAssignment partial not an assignment that is overwritten before any read