A local variable is never read or written after its declaration
What it finds
A local variable that is declared and never mentioned again is left over from an edit, or stands for work that was meant to use it.
A variable counts as used when its name occurs anywhere else in the method, lambda or initializer it is declared in. The variables of enhanced for loops, which have to be declared, are left alone, and so are _ and names that start with ignored or unused, which say that the value is dropped on purpose. The initializer still runs, so there is no fix that removes it.
From the rule's source, unused_local.rs.
import java.util.List; class Totals { int sum(List<Integer> values) { int total = 0; int count = values.size();tx0 reports: the local variable count is never used for (int value : values) { total += value; } for (Integer ignored : values) { total++; } Runnable r = () -> {tx0 reports: the local variable r is never used int inner = 1;tx0 reports: the local variable inner is never used }; return total; }} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:dead-local-variable" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/java:dead-local-variable] reason="..."tx0 explain tx0/java:dead-local-variable shows the same in the terminal. Found a false positive? Report it.