A reference to max, min or sum is passed where a comparator is expected

tx0/java:arithmetic-comparatordefaulterrorbug

What it finds

numbers.stream().max(Integer::max) compiles, since Integer.max takes two ints and returns an int like a Comparator, but it is not a comparison: its result is a number, whose sign says nothing about the order of the two values. Comparator asks for a negative, zero or positive result by order. The intended comparator is Integer::compare or Comparator.naturalOrder().

Reported is a reference to max, min or sum of Math or a numeric box, passed to sort, sorted, max, min, maxBy, minBy, comparing, thenComparing or to the constructor of a TreeMap, TreeSet or PriorityQueue.

From the rule's source, arithmetic_comparator.rs.

Example from the rule's tests
import java.util.List;import java.util.Optional;import java.util.TreeSet; class Ranking {  void order(List<Integer> scores) {    Optional<Integer> best = scores.stream().max(Integer::max);tx0 reports: Integer::max returns a number, not an order    scores.sort(Math::min);tx0 reports: Math::min returns a number    TreeSet<Integer> set = new TreeSet<>(Integer::sum);tx0 reports: Integer::sum returns a number    Optional<Integer> fine = scores.stream().max(Integer::compare);    Optional<Integer> total = scores.stream().reduce(Integer::sum);  }}
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 in every tier. To turn it off for a project, set its level in tx0.toml:

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

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