A sort's less function compares with <= or >=, which is not a strict order

tx0/go:less-with-or-equalrecommendedwarningreliability/equalitydepth fastbug

What it finds

sort.Slice(s, func(i, j int) bool { return s[i] <= s[j] }): the function must say whether element i sorts strictly before element j. With <= two equal elements each sort before the other, which breaks the ordering the algorithm relies on: the result may be out of order, and sort.SliceStable no longer keeps equal elements in place.

Reported are calls of sort.Slice and sort.SliceStable whose function is a literal whose only statement returns a comparison with <= or >=.

From the rule's source, less_with_or_equal.rs.

Example from the rule's tests
package p import "sort" func order(s []int) {	sort.Slice(s, func(i, j int) bool { return s[i] <= s[j] })tx0 reports: <= and >= make equal elements sort before each other	sort.SliceStable(s, func(i, j int) bool { return s[i] < s[j] })}
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 recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/go:less-with-or-equal" = "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/go:less-with-or-equal] reason="..."

tx0 explain tx0/go:less-with-or-equal shows the same in the terminal. Found a false positive? Report it.