Skip to content

slice-preallocation

Default severity: warning

Conservatively reports an empty slice followed by exactly one direct append per iteration of a range with a useful len. Preallocating that capacity avoids repeated growth while preserving zero length.

var result []Item
for _, item := range source {
result = append(result, convert(item))
}
result := make([]Item, 0, len(source))
for _, item := range source {
result = append(result, convert(item))
}