Each call of iterator() hands out the same object, so all loops share one position
What it finds
iterator() of an Iterable that returns this hands every caller the same iterator: the second loop over the object starts where the first one stopped, usually at the end, and two loops at once take elements from each other. Each call is meant to return a fresh iterator.
Reported in classes that implement Iterable and Iterator themselves.
From the rule's source, shared_iterator_state.rs.
import java.util.Iterator; class Range implements Iterable<Integer>, Iterator<Integer> { private int next; @Override public Iterator<Integer> iterator() { return this;tx0 reports: iterator() returns the object itself } @Override public boolean hasNext() { return next < 10; } @Override public Integer next() { return next++; }} Turn it on, off or suppress it
The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:
[rules]
"tx0/java:shared-iterator-state" = "off"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:shared-iterator-state] reason="..."tx0 explain tx0/java:shared-iterator-state shows the same in the terminal. Found a false positive? Report it.