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
proposal: cmd/vet: warn if sort.Interface implementation does not use both i and j #45780
Comments
/cc @dominikh for staticcheck consideration |
I would generalize slightly to checking something else:
|
@mdlayher already exists: https://staticcheck.io/docs/checks#SA4000 |
Does https://staticcheck.io/docs/checks#SA4018 also cover @guodongli-google's “useless assignment” case? (It's not obvious to me whether it would recognize index expressions as “variables”.) |
@nik0sc Your expectation could be easily handled by a static check tool, like |
|
@bcmills and @guodongli-google |
One thing to consider with the proposal is lexicographic order for a struct:
The proposed checker would not report this as both An alternative check to this proposal is to have vet warn when a parameter is not used (and not named "_"). This generalization would also not handle the example I just gave. I assume this proposal has come up before if someone has a reference. |
@timothy-king I now realize that a rule like I proposed would not help in complex implementations of Less like yours. The more general rule of warning for unused parameters is better, but would probably break some CI pipelines (I know I've written code that ignores parameters just so it fulfills an interface...) |
But what is the relationship between go vet and staticcheck? Is staticcheck simply the more inclusive version of go vet? |
@nik0sc vet and Staticcheck are two independent static analysis tools for Go. vet is part of the Go project and focuses on a smaller number of checks. Staticcheck is a third party tool with more checks than vet, but it is not a superset of vet – that is, vet has some checks that aren't in Staticcheck. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
In case of mixing up i and j in sort.Interface, it is possible to produce broken code that compiles without any warning from go vet. (This is really easy since those characters appear similar in monospaced fonts.)
I wrote a small buggy program to test this behavior:
What did you expect to see?
Some warning that this is probably wrong, as (sort.Interface).Less needs to use both i and j in the function body for it to be useful.
What did you see instead?
No output by go vet
The text was updated successfully, but these errors were encountered: