You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
staticcheck: misuse of slice make() and append() #112
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 onlyappend()'s to that slice could be sufficient to detect the common mistake.
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.
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.
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
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.
Activity
mvdan commentedon May 31, 2017
What if you intentionally wanted a number of zero values at the beginning? I've needed to leave a leading
0before, for example when populating a slice that will later be used for a binary search.cgilmour commentedon May 31, 2017
That consideration was noted in the third paragraph.
mvdan commentedon May 31, 2017
I read that, but my point (which I didn't communicate very well) is that I think this would cause too many false positives.
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
makeworks. But even then, it's fairly easy to spot that a slice has a chunk of zero elements at the beginning.[-]Misuse of slice make() and append()[/-][+]staticcheck: misuse of slice make() and append()[/+]ainar-g commentedon Sep 28, 2018
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
instead of
before appending.
I think this should be a check.
ainar-g commentedon Oct 29, 2018
FWIW,
doctor, the future go vet replacement, reports this:dominikh commentedon Oct 30, 2018
vetis starting to have more false positives than staticcheck ;)jcorbin commentedon Oct 30, 2019
Fwiw, I too am not new to go, and I still write
make([]T, 42)way too often when I meanmake([]T, 0, 42). In the case where i need to have some slots, it's usually more likemake([]T, 1, N); I've never usedmake([]T, N), followed by only appends, and meant it.