]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: reject type switch with guarded declaration and no cases
authorMatthew Dempsky <mdempsky@google.com>
Tue, 13 Mar 2018 21:26:45 +0000 (14:26 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Tue, 13 Mar 2018 22:02:46 +0000 (22:02 +0000)
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>
src/cmd/compile/internal/gc/swt.go
test/fixedbugs/issue23116.go [new file with mode: 0644]

index c9fb67e916a4e155adcac45c85597c2eb3ddd039..404a88444ac0a264f01519cb70166ed820b82337 100644 (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
diff --git a/test/fixedbugs/issue23116.go b/test/fixedbugs/issue23116.go
new file mode 100644 (file)
index 0000000..1737fee
--- /dev/null
@@ -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"
+       }
+}