Source file test/switch3.go

     1  // errorcheck
     2  
     3  // Copyright 2011 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Verify that erroneous switch statements are detected by the compiler.
     8  // Does not compile.
     9  
    10  package main
    11  
    12  type I interface {
    13  	M()
    14  }
    15  
    16  func bad() {
    17  	var i I
    18  	var s string
    19  
    20  	switch i {
    21  	case s: // ERROR "mismatched types string and I|incompatible types"
    22  	}
    23  
    24  	switch s {
    25  	case i: // ERROR "mismatched types I and string|incompatible types"
    26  	}
    27  
    28  	var m, m1 map[int]int
    29  	switch m {
    30  	case nil:
    31  	case m1: // ERROR "can only compare map m to nil|map can only be compared to nil|cannot compare"
    32  	default:
    33  	}
    34  
    35  	var a, a1 []int
    36  	switch a {
    37  	case nil:
    38  	case a1: // ERROR "can only compare slice a to nil|slice can only be compared to nil|cannot compare"
    39  	default:
    40  	}
    41  
    42  	var f, f1 func()
    43  	switch f {
    44  	case nil:
    45  	case f1: // ERROR "can only compare func f to nil|func can only be compared to nil|cannot compare"
    46  	default:
    47  	}
    48  
    49  	var ar, ar1 [4]func()
    50  	switch ar { // ERROR "cannot switch on"
    51  	case ar1:
    52  	default:
    53  	}
    54  
    55  	var st, st1 struct{ f func() }
    56  	switch st { // ERROR "cannot switch on"
    57  	case st1:
    58  	}
    59  }
    60  
    61  func good() {
    62  	var i interface{}
    63  	var s string
    64  
    65  	switch i {
    66  	case s:
    67  	}
    68  
    69  	switch s {
    70  	case i:
    71  	}
    72  }
    73  

View as plain text