Enable one more errorcheck test.
Updates #43110.
Updates #43200.
Change-Id: Ib7b971d5e9989c65320579f75d65266bbbbeec53
Reviewed-on: https://go-review.googlesource.com/c/go/+/278132
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
--- /dev/null
+// Copyright 2020 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
+
+type P *struct{}
+
+func _() {
+ // want an error even if the switch is empty
+ var a struct{ _ func() }
+ switch a /* ERROR cannot switch on a */ {
+ }
+
+ switch a /* ERROR cannot switch on a */ {
+ case a: // no follow-on error here
+ }
+
+ // this is ok because f can be compared to nil
+ var f func()
+ switch f {
+ }
+
+ switch f {
+ case nil:
+ }
+
+ switch (func())(nil) {
+ case nil:
+ }
+
+ switch (func())(nil) {
+ case f /* ERROR cannot compare */ :
+ }
+
+ switch nil /* ERROR use of untyped nil in switch expression */ {
+ }
+
+ // this is ok
+ switch P(nil) {
+ case P(nil):
+ }
+}
// By checking assignment of x to an invisible temporary
// (as a compiler would), we get all the relevant checks.
check.assignment(&x, nil, "switch expression")
+ if x.mode != invalid && !Comparable(x.typ) && !hasNil(x.typ) {
+ check.errorf(&x, "cannot switch on %s (%s is not comparable)", &x, x.typ)
+ x.mode = invalid
+ }
} else {
// spec: "A missing switch expression is
// equivalent to the boolean value true."
"initializerr.go": true, // types2 reports extra errors
"linkname2.go": true, // error reported by noder (not running for types2 errorcheck test)
"shift1.go": true, // issue #42989
- "switch3.go": true, // issue #43110
"switch4.go": true, // error reported by noder (not running for types2 errorcheck test)
"typecheck.go": true, // invalid function is not causing errors when called
var m, m1 map[int]int
switch m {
case nil:
- case m1: // ERROR "can only compare map m to nil|map can only be compared to nil"
+ case m1: // ERROR "can only compare map m to nil|map can only be compared to nil|cannot compare"
default:
}
var a, a1 []int
switch a {
case nil:
- case a1: // ERROR "can only compare slice a to nil|slice can only be compared to nil"
+ case a1: // ERROR "can only compare slice a to nil|slice can only be compared to nil|cannot compare"
default:
}
var f, f1 func()
switch f {
case nil:
- case f1: // ERROR "can only compare func f to nil|func can only be compared to nil"
+ case f1: // ERROR "can only compare func f to nil|func can only be compared to nil|cannot compare"
default:
}