The result of strings.Index is compared with > 0, which misses a match at the start
What it finds
strings.Index(s, sub) > 0, which is false when s starts with sub. It is usually meant to ask whether s contains sub, which is >= 0 or strings.Contains.
The same holds for the other Index functions of strings and bytes that return -1 for no match, and for 0 < strings.Index(s, sub). Some code does mean "found, but not at the start", so the rule is recommended rather than on by default.
From the rule's source, strings_index_gt_zero.rs.
package p import ( "bytes" "strings" str "strings") func contains(s, sub string, b []byte) bool { if strings.Index(s, sub) > 0 {tx0 reports: this is false when strings.Index finds a match at index 0 return true } if 0 < str.IndexByte(s, ':') {tx0 reports: this is false when str.IndexByte finds a match at index 0 return true } if (bytes.IndexRune(b, 'x')) > 0 {tx0 reports: bytes.IndexRune return true } if strings.Index(s, sub) >= 0 || strings.Index(s, sub) > 1 || strings.LastIndex(s, sub) > 0 { return true } if i := strings.Index(s, sub); i > 0 { return true } return strings.Count(s, sub) > 0} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/go:strings-index-gt-zero" = "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:strings-index-gt-zero] reason="..."tx0 explain tx0/go:strings-index-gt-zero shows the same in the terminal. Found a false positive? Report it.