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

Comments

Projects
None yet
3 participants
@josharian
Contributor

josharian commented May 31, 2016

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

This comment has been minimized.

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

This comment has been minimized.

Contributor

ianlancetaylor commented Nov 7, 2017

@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

This comment has been minimized.

Member

mvdan commented Nov 7, 2017

@ianlancetaylor that makes sense, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment