Skip to content
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: check for duplicate cases in switches #15906

Open
josharian opened this issue May 31, 2016 · 3 comments
Open

cmd/vet: check for duplicate cases in switches #15906

josharian opened this issue May 31, 2016 · 3 comments
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis)
Milestone

Comments

@josharian
Copy link
Contributor

Consider:

type A [1]int

func f(x A) {
    switch x {
    case A([1]int{1}):
    case A([1]int{1}):
    }
}

This is almost certainly a typo; the second case is unreachable. The compiler may reject duplicate constant cases in expression switches, but not e.g. nil or composite literals containing constants. (See #15896 for discussion.) Perhaps cmd/vet should step in to help catch these likely bugs. This issue is a reminder to investigate.

@mvdan
Copy link
Member

mvdan commented Nov 7, 2017

Why not make this a compiler error instead of a vet warning? The compiler already errors on more trivial duplicate case expressions.

@ianlancetaylor
Copy link
Contributor

@mvdan Let's try it in vet first. It's not always clear when two different cases are identical. Do they have to be written precisely the same way? What if one says 1 and one says int(1)? And so forth.

Also, doing it in the compiler would require a spec change, probably an addition to the implementation restriction in https://golang.org/ref/spec#Switch_statements . Which can of course be done but again requires some thought as to when cases are identical.

@mvdan
Copy link
Member

mvdan commented Nov 7, 2017

@ianlancetaylor that makes sense, thank you.

@adonovan adonovan added the Analysis Issues related to static analysis (vet, x/tools/go/analysis) label Apr 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis)
Projects
None yet
Development

No branches or pull requests

4 participants