A slice made with a length is appended to, so the values follow that many zero elements
What it finds
``go names := make([]string, len(users)) for _, u := range users { names = append(names, u.Name) } ` make with a length creates that many zero values, and append adds after them, so the result has twice the length and starts with empty strings. Either the length was meant as a capacity, make([]string, 0, len(users))`, or the elements were meant to be assigned by index.
The slice must be a local variable declared with make of a slice type and a length that is not an integer literal, without a capacity argument, and the same function must append to it with v = append(v, ...) and never assign one of its elements by index, slice it, or pass it to copy or another function, any of which may fill the length first.
From the rule's source, append_after_make_length.rs.
package p type user struct{ Name string } func names(users []user) []string { out := make([]string, len(users)) for _, u := range users { out = append(out, u.Name)tx0 reports: out was made with a length, so this appends after that many zero values } return out} func indexed(users []user) []string { out := make([]string, len(users)) for i, u := range users { out[i] = u.Name } return append(out, "extra")} func capacity(users []user) []string { out := make([]string, 0, len(users)) for _, u := range users { out = append(out, u.Name) } return out} 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:append-after-make-length" = "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:append-after-make-length] reason="..."tx0 explain tx0/go:append-after-make-length shows the same in the terminal. Found a false positive? Report it.