-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/vet: warn when user uses non-slice parameter to sort.Slice #33817
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
Comments
I intend to implement this. |
Change https://golang.org/cl/191778 mentions this issue: |
Change https://golang.org/cl/191598 mentions this issue: |
Will this new vet check complain when sort.Slice is used with an interface type as first argument, which can be a valid use case when the comparison function is abstracted with an interface and the concrete underlying type may still be slice? Simplified example: https://play.golang.org/p/KEFLcgiO_xU |
Great question! It probably will, as implemented. I will fix it in the proposed implementation. |
Oh shoot, is that going to be really hard to do? I had a quick look at it and I guess I can't get the concrete implementation from the parameter? Any ideas? |
We cant always statically prove what the concrete type is that was put into an interface typed variable because that information might be far away (different function) and only known at runtime (due to different control flow). Solving that for all programs is not possible as it would mean solving the halting problem. I think to avoid false alarms the vet check would need to only warn when a non interface type that is not a slice is passed to sort.Slice. How often does this mistake happen and wont be discovered by simple tests resulting in a runtime error/panic? |
Adding vet experts to discuss the proposed check and semantics. |
Thanks for the input, I can certainly remove the false positive regarding an interface parameter being used. I underestimated the overall complexity of this vetting so I understand if this means we don't think it's useful enough for its own analyzer. |
First you must satisfy the three conditions in the vet README: correctness, frequency, and precision. |
To review:
I think this check passes this test
I am unsure it passes this test, but then I am also unsure where the threshold lies. I got the impression that the new analyzer implementation might make the cost of new checks very small, but I am unsure enough as to not argue that it passes this test.
As explained by Martin in #33817 (comment), this test will necessarily suffer from a small set of false negatives in the case of interface types being used incorrectly. I again am not confident enough to say it passes this test. Any more thoughts on my analysis? @matloob and @empijei were part of the discussion with me to add this check in the first place and may want to weigh in on either side. |
Correctness: I agree that this check clearly passes the Correctness test. For now I think it should be uncontroversial to check this analysis into x/tools/go/analysis/passes and not hook it up into vet until a decision is made. We'll still be able to expose it through gopls. |
The frequency criterion is about how often the problem arises, not how often the function appears in source. Personally, I have never seen it happen, so I am skeptical. |
This bug will be noticed when the program crashes the first time the code path is taken. It's not subtle. As @martisch said, any tests that exercise such code would expose the bug. |
Sounds to me like this doesn't quite pass the threshold to be considered for |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What did you do?
I used
sort.Slice
with a non-slice first parameter. Itpanic
ed at runtime.https://play.golang.org/p/SqdG2hd5yE8
What did you expect to see?
A
go vet
warningWhat did you see instead?
No
go vet
warningI have already implemented an analyzer for this (thanks @matloob and @empijei).
The text was updated successfully, but these errors were encountered: