A variable is updated on each pass of a loop, but only its own updates ever read it
What it finds
int retries = 0; while (!done()) { retries++; work(); } counts something that nothing ever looks at: each pass computes the next value from the last one, and that is all that reads it. A check of the counter is missing, or the variable is left over.
Reported is an update of a local variable inside a loop, x++, x += n or x = f(x), whose value the IR shows is only ever read by later updates of the same variable, and never by anything else, on any path. The variable is named once, at its first such update. Names such as ignored or unused are left alone, and so are updates in the block of a try statement with a handler or a finally block to a variable declared outside it.
From the rule's source, loop_value_never_read.rs.
class Work { void drain(java.util.Queue<String> queue) { int handled = 0; while (!queue.isEmpty()) { process(queue.poll()); handled++;tx0 reports: handled is updated here on each pass, but nothing else reads it } } int counted(java.util.Queue<String> queue) { int handled = 0; while (!queue.isEmpty()) { process(queue.poll()); handled++; } return handled; } void process(String item) {}} 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:loop-value-never-read" = "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:loop-value-never-read] reason="..."tx0 explain tx0/java:loop-value-never-read shows the same in the terminal. Found a false positive? Report it.