Skip to content

Faulty prealloc #21

@ghost

Description

I currently work in a team and we had a code issue a few times that made it beyond code review.
It is pre-allocating a slice with size instead of cap, then use append on it. Example

ar := make([]int, 10)
for i:=0; i<10; i++ {
  ar = append(ar, i)
  }

The correct pre-alloc would be ar := make([]int, 0, 10). Because of the missing 0,, the faulty code produces an array of size 20.

Would it be possible to add a check for this into your linter? I could not find any linter that has this check.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions