strings.SplitN or bytes.SplitN is asked for zero parts and returns nil

tx0/go:split-count-zerodefaultwarningbug

What it finds

strings.SplitN(s, ",", 0): the count is how many parts to return at most, and with 0 the call returns nil whatever s holds. -1 returns every part, as strings.Split does. SplitAfterN, and the same functions of bytes, count the same way.

The count must be the literal 0, and the function resolved through the imports of the file. The fix writes -1, which changes what the call does, so it is unsafe.

From the rule's source, split_count_zero.rs.

Example from the rule's tests
package p import (	"bytes"	"strings") func f(s string, b []byte, n int) ([]string, [][]byte) {	parts := strings.SplitN(s, ",", 0)tx0 reports: strings.SplitN with a count of 0 returns nil	chunks := bytes.SplitAfterN(b, []byte(","), 0)tx0 reports: bytes.SplitAfterN with a count of 0 returns nil	parts = strings.SplitN(s, ",", 2)	parts = strings.SplitN(s, ",", n)	parts = strings.SplitN(s, ",", -1)	return parts, chunks}
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:split-count-zero" = "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:split-count-zero] reason="..."

tx0 explain tx0/go:split-count-zero shows the same in the terminal. Found a false positive? Report it.