A comparison puts the constant before the value it tests
What it finds
if nil == err or if "admin" == role: putting the constant first guards against assigning by mistake in languages where = is allowed in a condition, which Go rejects anyway. In Go it only reverses the usual reading order, the variable first.
Reported are comparisons with == or != whose left operand is a literal or the predeclared nil, true or false, and whose right operand is not. The fix swaps the operands, which keeps the result, so it is safe.
From the rule's source, yoda_condition.rs.
package p func f(err error, role string) bool { if nil != err {tx0 reports: the constant nil comes before the value it is compared with return false } return "admin" == role || role == "root"tx0 reports: comes before the value it is compared with} 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:yoda-condition" = "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:yoda-condition] reason="..."tx0 explain tx0/go:yoda-condition shows the same in the terminal. Found a false positive? Report it.