Two loops in a row walk the same collection, where one loop could do the work of both
What it finds
for (x : items) a(x); for (x : items) b(x); walks the collection twice. When the two bodies do not depend on each other's results, one loop does both.
Reported is a for loop over a simple name that directly follows a for loop over the same name in the same block.
From the rule's source, repeated_loop_over_collection.rs.
import java.util.List; class Loops { void run(List<String> items, List<String> other) { for (String s : items) { a(s); } for (String s : items) {tx0 reports: already walks this collection b(s); } for (String s : other) { b(s); } } void a(String s) {} void b(String s) {}} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:repeated-loop-over-collection" = "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:repeated-loop-over-collection] reason="..."tx0 explain tx0/java:repeated-loop-over-collection shows the same in the terminal. Found a false positive? Report it.