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 import "fmt" 13 14 func f0(x int) { 15 switch x { 16 case 0: 17 case 0: // ERROR "duplicate case 0 in switch" 18 } 19 20 switch x { 21 case 0: 22 case int(0): // ERROR "duplicate case 0 in switch" 23 } 24 } 25 26 func f1(x float32) { 27 switch x { 28 case 5: 29 case 5: // ERROR "duplicate case 5 in switch" 30 case 5.0: // ERROR "duplicate case 5 in switch" 31 } 32 } 33 34 func f2(s string) { 35 switch s { 36 case "": 37 case "": // ERROR "duplicate case .. in switch" 38 case "abc": 39 case "abc": // ERROR "duplicate case .abc. in switch" 40 } 41 } 42 43 func f3(e interface{}) { 44 switch e { 45 case 0: 46 case 0: // ERROR "duplicate case 0 in switch" 47 case int64(0): 48 case float32(10): 49 case float32(10): // ERROR "duplicate case float32\(10\) in switch" 50 case float64(10): 51 case float64(10): // ERROR "duplicate case float64\(10\) in switch" 52 } 53 } 54 55 func f4(e interface{}) { 56 switch e.(type) { 57 case int: 58 case int: // ERROR "duplicate case int in type switch" 59 case int64: 60 case error: 61 case error: // ERROR "duplicate case error in type switch" 62 case fmt.Stringer: 63 case fmt.Stringer: // ERROR "duplicate case fmt.Stringer in type switch" 64 case struct { 65 i int "tag1" 66 }: 67 case struct { 68 i int "tag2" 69 }: 70 case struct { 71 i int "tag1" 72 }: // ERROR "duplicate case struct { i int .tag1. } in type switch" 73 } 74 } 75 76 func f5(a [1]int) { 77 switch a { 78 case [1]int{0}: 79 case [1]int{0}: // OK -- see issue 15896 80 } 81 } 82 83 // Ensure duplicate const bool clauses are accepted. 84 func f6() int { 85 switch { 86 case 0 == 0: 87 return 0 88 case 1 == 1: // Intentionally OK, even though a duplicate of the above const true 89 return 1 90 } 91 return 2 92 } 93 94 // Ensure duplicates in ranges are detected (issue #17517). 95 func f7(a int) { 96 switch a { 97 case 0: 98 case 0, 1: // ERROR "duplicate case 0" 99 case 1, 2, 3, 4: // ERROR "duplicate case 1" 100 } 101 } 102