modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/typeswitch2.go (about)

     1  // errorcheck
     2  
     3  // Copyright 2009 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 various erroneous type switches are caught be the compiler.
     8  // Does not compile.
     9  
    10  package main
    11  
    12  import "io"
    13  
    14  func whatis(x interface{}) string {
    15  	switch x.(type) {
    16  	case int:
    17  		return "int"
    18  	case int: // ERROR "duplicate"
    19  		return "int8"
    20  	case io.Reader:
    21  		return "Reader1"
    22  	case io.Reader: // ERROR "duplicate"
    23  		return "Reader2"
    24  	case interface {
    25  		r()
    26  		w()
    27  	}:
    28  		return "rw"
    29  	case interface {	// ERROR "duplicate"
    30  		w()
    31  		r()
    32  	}:
    33  		return "wr"
    34  
    35  	}
    36  	return ""
    37  }
    38  
    39  func notused(x interface{}) {
    40  	// The first t is in a different scope than the 2nd t; it cannot
    41  	// be accessed (=> declared and not used error); but it is legal
    42  	// to declare it.
    43  	switch t := 0; t := x.(type) { // ERROR "declared and not used"
    44  	case int:
    45  		_ = t // this is using the t of "t := x.(type)"
    46  	}
    47  }