Both operands of an operator are the same

tx0/go:identical-operandsdefaultwarningbug

What it finds

a && a, n % n: an operator applied to the same operand twice gives a fixed or redundant result, which usually means one side was meant to be something else.

Left out are the operators where a repeated operand is an idiom for floating-point values, which cannot be told apart without types: x != x detects a NaN and f - f an infinity, so ==, !=, - and / are not checked. So are |, & and +, and operands made only of literals, such as 8 / 8 in a table of constants.

From the rule's source, identical_operands.rs.

Example from the rule's tests
package p import "math/rand" func g(a, b bool, x, y float64, n int) {	if a && a {tx0 reports: both operands of && are a	}	if a || (a) {tx0 reports: both operands of || are a	}	if x != x {	}	if x == x {	}	_ = x - x	_ = x / x	_ = 8 / 8	_ = 1 < 1	_ = n % ntx0 reports: both operands of % are n	_ = rand.Int() - rand.Int()	_ = x - y	_ = n + n	_ = n | n	if x < x {tx0 reports: both operands of < are x	}	_ = b}
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/go:identical-operands" = "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/go:identical-operands] reason="..."

tx0 explain tx0/go:identical-operands shows the same in the terminal. Found a false positive? Report it.