8000 staticcheck: misuse of slice make() and append() · Issue #112 · dominikh/go-tools · GitHub
[go: up one dir, main page]

Skip to content

staticcheck: misuse of slice make() and append() #112

Open

Description

@cgilmour

A problem that seems to occur regularly is creating a slice with a length component, then only appending to it.
Example: https://play.golang.org/p/CehTV0VFKp

Generally, the intention would be making a zero-length slice with an initial capacity, then appending to it. The effect of this occurs only at runtime and the cause can be hard to spot when debugging and/or reading the related code -- it's a very subtle detail.

There may be cases where this was done intentionally to allocate some space for other functions to set the data, or to suit a particular protocol or data structure.
However, I expect that it's exceptionally rare compared to the common misuse.

A check that observes a slice created with make(slice-type, len) followed by only append()'s to that slice could be sufficient to detect the common mistake.

Activity

  1. mvdan commented on May 31, 2017

    @mvdan
    Contributor

    What if you intentionally wanted a number of zero values at the beginning? I've needed to leave a leading 0 before, for example when populating a slice that will later be used for a binary search.

  2. cgilmour commented on May 31, 2017

    @cgilmour
    Author

    That consideration was noted in the third paragraph.

  3. mvdan commented on May 31, 2017

    @mvdan
    Contributor

    I read that, but my point (which I didn't communicate very well) is that I think this would cause too many false positives.

    However, I expect that it's exceptionally rare compared to the common misuse.

    The example was to show a not-so-rare use :)

    I'm also surprised that this issue would happen regularly. I can see it happening with people who are new to Go and aren't very familiar with how make works. But even then, it's fairly easy to spot that a slice has a chunk of zero elements at the beginning.

  4. changed the title [-]Misuse of slice make() and append()[/-] [+]staticcheck: misuse of slice make() and append()[/+] on Aug 10, 2017
  5. ainar-g commented on Sep 28, 2018

    @ainar-g

    I am not new to Go, and I've committed this mistake several times over the last year. When you're tired you're just thinking "Hm, I should allocate space before appending." and you have a brain-fart and write

    s2 := make([]int, len(s1))
    

    instead of

    s2 := make([]int, 0, len(s1))
    

    before appending.

    I think this should be a check.

  6. ainar-g commented on Oct 29, 2018

    @ainar-g

    FWIW, doctor, the future go vet replacement, reports this:

    example.com/foo/bar.go:301:18: append after make(pq.Int64Array, n); did you mean make(pq.Int64Array, 0, n)?
    
  7. dominikh commented on Oct 30, 2018

    @dominikh
    Owner

    vet is starting to have more false positives than staticcheck ;)

  8. jcorbin commented on Oct 30, 2019

    @jcorbin

    Fwiw, I too am not new to go, and I still write make([]T, 42) way too often when I mean make([]T, 0, 42). In the case where i need to have some slots, it's usually more like make([]T, 1, N); I've never used make([]T, N), followed by only appends, and meant it.

  9. added
    aggressiveA set of checks that is more prone to false positives but is helpful during code review
    and removed
    needs-decisionWe have to decide if this check is feasible and desirable
    on Oct 17, 2021
  10. added this to the Staticcheck 2022.2 milestone on Jan 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    aggressiveA set of checks that is more prone to false positives but is helpful during code reviewnew-check

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0