-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cmd/vet: warn when ignoring the return value of slices.Delete and friends #62729
Comments
One of the criteria for vet checks is frequency. How often do people make this mistake? |
(Compare #20803 (comment).) |
@ianlancetaylor There is already an |
Frequency: looking at the results of this GitHub search, the rate of incorrect usage seems low. Almost all of the matched lines are using either |
doesn't appear to be too common https://github.com/search?type=code&q=language%3AGo+%2F%5E%5Cs%2Bslices.Delete%5C%28%2F&p=1 |
Given how new But this is also one of those kinds of bugs where the bug is likely to be detected during testing: the |
The GitHub search results include usage of |
Some more results when searching for DeleteFunc and for Compact. Nothing for CompactFunc. |
We often see this pattern:
It's not obvious at first glance for everyone that the first line is correct, and the second is not. |
I literally ran into this issue yesterday with It took a little bit to fix, but a vet would have told me before even getting to the unittests. I think i made the mistake as I was updating a map at the same time with I am not sure how to capture hard number data on issues that are not checked into code. |
In theory, given the new telemetry mechanism we could add the proposed check to That said, that approach seems strictly more complicated than just adding this case to the existing |
agree! A profileration of telemetry metrics comes with subtle security concerns. A proliferation of vet sanity checks only comes with a (modest and measurable) performance burden. |
In #57433, @rsc said an unused result check would be added for the slices functions that need to be reassigned to work. That resolved my concern about using pointers to slices to prevent misuse. I still think we should do it. |
I am persuaded. |
Change https://go.dev/cl/530116 mentions this issue: |
I now realize that |
Inside Google there's been a few instances where people were confused about this API, used it incorrectly, and then spend time to understand what is happening. This analyzer would have saved them a lot of time. |
I now realize that
|
I was blissfully unaware of this proposal and independently implemented the change today in https://go.dev/cl/554317. (How much faster things move when you completely bypass the proper process!) Sorry about that. The new list includes Clip, Compact{,Func}, Delete{,Func}, Grow, Insert, Replace, but not Clone for (by chance) the same reason given in the previous comment. Happy to revert the change if necessary, but it seems like there's a clear consensus in favor of accepting this proposal; I'll add this to the agenda for next week's proposal review. |
The pending CL https://go.dev/cl/530116 has tests! So we'll want to merge them somehow. |
I'm not convinced it's necessary. The tests exercise the mechanism; they needn't exercise every data point in the input configuration. |
Based on the discussion above, this proposal seems like a likely accept. Details: Warn about ignored return values for the new package slices functions: Clip, Compact, CompactFunc, Delete, DeleteFunc, Grow, Insert, Replace. |
No change in consensus, so accepted. 🎉 Details: Warn about ignored return values for the new package slices functions: Clip, Compact, CompactFunc, Delete, DeleteFunc, Grow, Insert, Replace. |
Phew. ;-) This was implemented (prematurely) in #62729 (comment). |
Calling slices.Delete while ignoring the returned slice value is almost certainly a bug, doing some element shifting but losing track of which elements are kept/discarded.
See sample: https://go.dev/play/p/2jV918bTDAX
Suggestion
vet warning:
Same for DeleteFunc, Compact and CompactFunc.
The text was updated successfully, but these errors were encountered: