Skip to content

append-to-sized-slice

Detect appends to slices created with a known positive length.

Updated View as Markdown

Default severity: warning

make([]T, n) with a compile-time positive n creates n existing zero values. If the intent is to append up to n values, use make([]T, 0, n) so the result does not begin with zeros. Runtime lengths are left alone when the analyzer cannot prove that they are positive.

Bad

values := make([]int, count)
values = append(values, next)

Good

values := make([]int, 0, count)
values = append(values, next)
Navigation

Type to search…

↑↓ navigate↵ selectEsc close