-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathswitch_stmt2.swift
152 lines (130 loc) · 2.9 KB
/
switch_stmt2.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// RUN: %target-typecheck-verify-swift
enum E {
case e1
case e2
}
func foo1(e : E) {
switch e { // expected-error{{switch must be exhaustive}}
// expected-note@-1 {{add missing case: '.e2'}}
case .e1: return
}
}
func foo2(i : Int) {
// expected-note@+1{{add a default clause}}{{+2:3-3=default:\n<#code#>\n}}
switch i { // expected-error{{switch must be exhaustive}}
case 1: return
}
}
// Treat nil as .none and do not emit false
// non-exhaustive warning.
func testSwitchEnumOptionalNil(_ x: Int?) -> Int {
switch x { // no warning
case .some(_):
return 1
case nil:
return -1
}
}
// Do not emit false non-exhaustive warnings if both
// true and false are covered by the switch.
func testSwitchEnumBool(_ b: Bool, xi: Int) -> Int {
var x = xi
let Cond = b
switch Cond { // no warning
default:
x += 1
}
switch Cond { // expected-error{{switch must be exhaustive}}
// expected-note@-1 {{missing case: 'false'}}
case true:
x += 1
}
switch Cond { // expected-error{{switch must be exhaustive}}
// expected-note@-1 {{missing case: 'true'}}
case false:
x += 1
}
switch Cond { // no warning
case true:
x += 1
case false:
x -= 1
}
return x
}
func testSwitchOptionalBool(_ b: Bool?, xi: Int) -> Int {
var x = xi
switch b { // No warning
case .some(true):
x += 1
case .some(false):
x += 1
case .none:
x -= 1
}
switch b { // expected-error{{switch must be exhaustive}}
// expected-note@-1 {{add missing case: '.some(false)'}}
case .some(true):
x += 1
case .none:
x -= 1
}
return xi
}
// Do not emit false non-exhaustive warnings if both
// true and false are covered for a boolean element of a tuple.
func testSwitchEnumBoolTuple(_ b1: Bool, b2: Bool, xi: Int) -> Int {
var x = xi
let Cond = (b1, b2)
switch Cond { // no warning
default:
x += 1
}
switch Cond { // expected-error{{switch must be exhaustive}}
// expected-note@-1 {{add missing case: '(false, _)'}}
// expected-note@-2 {{add missing case: '(_, false)'}}
case (true, true):
x += 1
}
switch Cond { // expected-error{{switch must be exhaustive}}
// expected-note@-1 {{add missing case: '(true, _)'}}
// expected-note@-2 {{add missing case: '(_, false)'}}
case (false, true):
x += 1
}
switch Cond { // no warning
case (true, true):
x += 1
case (true, false):
x += 1
case (false, true):
x -= 1
case (false, false):
x -= 1
}
return x
}
func non_fully_covered_switch(x: Int) -> Int {
var x = x
switch x { // expected-error{{switch must be exhaustive}}
// expected-note@-1{{add a default clause}}
case 0:
x += 1
case 3:
x -= 1
}
return x
}
// Do not crash if another switch statement follows a fallthrough.
func fallthrough_not_last(i: Int) {
switch i {
case 1:
fallthrough
switch i {
case 1: break
default: break
}
default:
break
}
}