cmd/compile: reject type switch with guarded declaration and no cases

Fixes #23116.

Change-Id: I5db5c5c39bbb50148ffa18c9393b045f255f80a3
Reviewed-on: https://go-review.googlesource.com/100459
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Matthew Dempsky 2018-03-13 14:26:45 -07:00
parent 363bcd7b4f
commit e601c07908
2 changed files with 21 additions and 0 deletions

View File

@ -70,6 +70,12 @@ func typecheckswitch(n *Node) {
if t != nil && !t.IsInterface() {
yyerrorl(n.Pos, "cannot type switch on non-interface value %L", n.Left.Right)
}
if v := n.Left.Left; v != nil && !isblank(v) && n.List.Len() == 0 {
// We don't actually declare the type switch's guarded
// declaration itself. So if there are no cases, we
// won't notice that it went unused.
yyerrorl(v.Pos, "%v declared and not used", v.Sym)
}
} else {
// expression switch
top = Erv

View File

@ -0,0 +1,15 @@
// errorcheck
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package p
func f(x interface{}) {
switch x.(type) {
}
switch t := x.(type) { // ERROR "declared and not used"
}
}