A length or an unsigned number is compared with zero in a way that is always true or always false

tx0/go:impossible-sign-checkdefaultwarningbug

What it finds

len(s) < 0 is always false and len(s) >= 0 always true, since lengths are never negative; the same holds for cap and for variables of unsigned types. Usually a different bound or a different operand was meant, such as len(s) <= 0 or the signed value before a conversion.

One operand must be the literal 0 and the other a call of the builtin len or cap, or a variable declared in the file with a written unsigned type such as uint or uint32.

From the rule's source, impossible_sign_check.rs.

Example from the rule's tests
package p func f(s []int, n uint, m int) bool {	if len(s) < 0 {tx0 reports: len(s) is never negative, so this is always false		return false	}	if 0 <= cap(s) {tx0 reports: cap(s) is never negative, so this is always true		return true	}	if n < 0 {tx0 reports: n is unsigned, so this is always false		return false	}	return len(s) > 0 && m >= 0 && n > 0 && len(s) <= 0}
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:impossible-sign-check" = "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:impossible-sign-check] reason="..."

tx0 explain tx0/go:impossible-sign-check shows the same in the terminal. Found a false positive? Report it.