...
Run Format

Source file test/switch5.go

Documentation: test

     1  // errorcheck
     2  
     3  // Copyright 2016 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 switch statements with duplicate cases are detected by the compiler.
     8  // Does not compile.
     9  
    10  package main
    11  
    12  func f0(x int) {
    13  	switch x {
    14  	case 0:
    15  	case 0: // ERROR "duplicate case 0 in switch"
    16  	}
    17  
    18  	switch x {
    19  	case 0:
    20  	case int(0): // ERROR "duplicate case int.0. .value 0. in switch"
    21  	}
    22  }
    23  
    24  func f1(x float32) {
    25  	switch x {
    26  	case 5:
    27  	case 5: // ERROR "duplicate case 5 in switch"
    28  	case 5.0: // ERROR "duplicate case 5 in switch"
    29  	}
    30  }
    31  
    32  func f2(s string) {
    33  	switch s {
    34  	case "":
    35  	case "": // ERROR "duplicate case .. in switch"
    36  	case "abc":
    37  	case "abc": // ERROR "duplicate case .abc. in switch"
    38  	}
    39  }
    40  
    41  func f3(e interface{}) {
    42  	switch e {
    43  	case 0:
    44  	case 0: // ERROR "duplicate case 0 in switch"
    45  	case int64(0):
    46  	case float32(10):
    47  	case float32(10): // ERROR "duplicate case float32\(10\) .value 10. in switch"
    48  	case float64(10):
    49  	case float64(10): // ERROR "duplicate case float64\(10\) .value 10. in switch"
    50  	}
    51  }
    52  
    53  func f5(a [1]int) {
    54  	switch a {
    55  	case [1]int{0}:
    56  	case [1]int{0}: // OK -- see issue 15896
    57  	}
    58  }
    59  
    60  // Ensure duplicate const bool clauses are accepted.
    61  func f6() int {
    62  	switch {
    63  	case 0 == 0:
    64  		return 0
    65  	case 1 == 1: // Intentionally OK, even though a duplicate of the above const true
    66  		return 1
    67  	}
    68  	return 2
    69  }
    70  
    71  // Ensure duplicates in ranges are detected (issue #17517).
    72  func f7(a int) {
    73  	switch a {
    74  	case 0:
    75  	case 0, 1: // ERROR "duplicate case 0"
    76  	case 1, 2, 3, 4: // ERROR "duplicate case 1"
    77  	}
    78  }
    79  
    80  // Ensure duplicates with simple literals are printed as they were
    81  // written, not just their values. Particularly useful for runes.
    82  func f8(r rune) {
    83  	const x = 10
    84  	switch r {
    85  	case 33, 33: // ERROR "duplicate case 33 in switch"
    86  	case 34, '"': // ERROR "duplicate case '"' .value 34. in switch"
    87  	case 35, rune('#'): // ERROR "duplicate case rune.'#'. .value 35. in switch"
    88  	case 36, rune(36): // ERROR "duplicate case rune.36. .value 36. in switch"
    89  	case 37, '$'+1: // ERROR "duplicate case '\$' \+ 1 .value 37. in switch"
    90  	case 'b':
    91  	case 'a', 'b', 'c', 'd': // ERROR "duplicate case 'b' .value 98."
    92  	case x, x: // ERROR "duplicate case x .value 10."
    93  	}
    94  }
    95  

View as plain text