The three-way Compare of strings or bytes is used only to test equality
What it finds
strings.Compare(a, b) == 0 is a == b said the long way, and the strings package documents that Compare exists only for symmetry with bytes. For byte slices, bytes.Equal(a, b) says what is meant and stops at the first difference without working out an order.
The call must be Compare of strings or bytes, resolved through the imports, compared with == or != to the literal 0. The fix writes ==, != or bytes.Equal and is safe.
From the rule's source, compare_for_equality.rs.
package p import ( "bytes" "strings") func f(a, b string, x, y []byte) bool { if strings.Compare(a, b) == 0 {tx0 reports: this can be written as a == b return true } return bytes.Compare(x, y) != 0 || strings.Compare(a, b) < 0tx0 reports: this can be written as !bytes.Equal(x, y)} 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/go:compare-for-equality" = "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:compare-for-equality] reason="..."tx0 explain tx0/go:compare-for-equality shows the same in the terminal. Found a false positive? Report it.