A list is copied only to be reversed, where reversed() gives a reversed view without copying

tx0/java:reversed-copystrictwarningperformance/allocationdepth fastperformance

What it finds

List<T> copy = new ArrayList<>(items); Collections.reverse(copy); copies every element and then moves each one. Since Java 21, items.reversed() is a view in reverse order; when the copy is only read, the view does the job without either step.

Reported is a local variable initialized with new ArrayList<>(...) or new LinkedList<>(...) of one argument whose next statement is Collections.reverse of it.

Files whose build file (pom.xml, build.gradle or build.gradle.kts) compiles for a Java before 21 are left alone.

From the rule's source, reversed_copy.rs.

Example from the rule's tests
import java.util.ArrayList;import java.util.Collections;import java.util.List; class Reverse {  void run(List<String> items) {    List<String> copy = new ArrayList<>(items);    Collections.reverse(copy);tx0 reports: reversed() gives a view    List<String> other = new ArrayList<>(items);    other.add("x");    Collections.reverse(other);  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

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:

tx0.toml
[rules]
"tx0/java:reversed-copy" = "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:reversed-copy] reason="..."

tx0 explain tx0/java:reversed-copy shows the same in the terminal. Found a false positive? Report it.