Commit e601c079 authored by Matthew Dempsky's avatar Matthew Dempsky

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: 's avatarRobert Griesemer <gri@golang.org>
parent 363bcd7b
......@@ -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
......
// 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"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment