A random number is drawn below 1, which always gives 0

tx0/go:random-range-onedefaultwarningbug

What it finds

rand.Intn(1): the bounded functions of math/rand return a number from 0 up to, but not including, their argument, so a bound of 1 always gives 0. A bound of 2 was likely meant, for a coin flip, or the bound is off by one.

Reported are calls with the literal 1 of Intn, Int31n, Int63n of math/rand and IntN, Int32N, Int64N, UintN, Uint32N, Uint64N and N of math/rand/v2, resolved through the imports.

From the rule's source, random_range_one.rs.

Example from the rule's tests
package p import "math/rand" func f() bool {	if rand.Intn(1) == 0 {tx0 reports: rand.Intn(1) always returns 0		return true	}	return rand.Intn(2) == 0 || rand.Int63n(1) > 0tx0 reports: rand.Int63n(1) always returns 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:random-range-one" = "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:random-range-one] reason="..."

tx0 explain tx0/go:random-range-one shows the same in the terminal. Found a false positive? Report it.