go: cmd/compile: missing "declared but not used" error for unused variable in type switch with no cases
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (go version
)?
1.9.2
Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (go env
)?
Ubuntu 16.04 (“amd64” architecture)
What did you do?
This code compiles and generates no errors. Why?
func f(v interface{}) {
switch x := v.(type) {
}
}
https://play.golang.org/p/BnQbG6RKRB
What did you expect to see?
A compilation error.
What did you see instead?
It worked.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 20 (17 by maintainers)
Some more information about this decision: Despite some arguments in favor of the status quo, the majority of the proposal review team felt pretty strongly that this was “just a bug”. Notably, gccgo and go/types already support report an error.
With respect to the spec wording: While the spec does say that the variable is declared in each case branch, it is sufficient for the variable to be used at least once - it doesn’t need to be used in each case branch. At least from the programmer’s point of view this makes it look like there’s only one variable that needs to be used, which is the one declared in the short variable declaration in the switch clause. Thus, this would be a more technical argument if favor of the decision.
I agree that, even if the compiler is technically correct, the lack of an error is unexpected to any Go programmer not familiar with all the spec details. I think we should add the error.
I wonder if a change in the spec would be necessary for
cmd/compile
andgo/types
to add the error, though. I don’t know to what extent are the reference tools able to add stuff of their own that isn’t in the spec explicitly./cc @griesemer @mdempsky