]> git.lizzy.rs Git - rust.git/blob - tests/ui/matches.rs
00faed26818ae62760a7a829ceee5fb6fe95d324
[rust.git] / tests / ui / matches.rs
1 #![feature(plugin)]
2 #![feature(exclusive_range_pattern)]
3
4 #![plugin(clippy)]
5 #![deny(clippy)]
6 #![allow(unused, if_let_redundant_pattern_matching)]
7 #![deny(single_match_else)]
8
9 use std::borrow::Cow;
10
11 enum Foo { Bar, Baz(u8) }
12 use Foo::*;
13
14 enum ExprNode {
15     ExprAddrOf,
16     Butterflies,
17     Unicorns,
18 }
19
20 static NODE: ExprNode = ExprNode::Unicorns;
21
22 fn dummy() {
23 }
24
25 fn unwrap_addr() -> Option<&'static ExprNode> {
26     match ExprNode::Butterflies {
27
28
29
30         ExprNode::ExprAddrOf => Some(&NODE),
31         _ => { let x = 5; None },
32     }
33 }
34
35 fn single_match(){
36     let x = Some(1u8);
37
38     match x {
39
40
41
42         Some(y) => { println!("{:?}", y); }
43         _ => ()
44     };
45
46     let z = (1u8,1u8);
47     match z {
48
49
50
51         (2...3, 7...9) => dummy(),
52         _ => {}
53     };
54
55     // Not linted (pattern guards used)
56     match x {
57         Some(y) if y == 0 => println!("{:?}", y),
58         _ => ()
59     }
60
61     // Not linted (no block with statements in the single arm)
62     match z {
63         (2...3, 7...9) => println!("{:?}", z),
64         _ => println!("nope"),
65     }
66 }
67
68 fn single_match_know_enum() {
69     let x = Some(1u8);
70     let y : Result<_, i8> = Ok(1i8);
71
72     match x {
73
74
75
76         Some(y) => dummy(),
77         None => ()
78     };
79
80     match y {
81
82
83
84         Ok(y) => dummy(),
85         Err(..) => ()
86     };
87
88     let c = Cow::Borrowed("");
89
90     match c {
91
92
93
94         Cow::Borrowed(..) => dummy(),
95         Cow::Owned(..) => (),
96     };
97
98     let z = Foo::Bar;
99     // no warning
100     match z {
101         Bar => println!("42"),
102         Baz(_) => (),
103     }
104
105     match z {
106         Baz(_) => println!("42"),
107         Bar => (),
108     }
109 }
110
111 fn match_bool() {
112     let test: bool = true;
113
114     match test {
115
116
117
118         true => 0,
119         false => 42,
120     };
121
122     let option = 1;
123     match option == 1 {
124
125
126
127         true => 1,
128         false => 0,
129     };
130
131     match test {
132
133
134
135         true => (),
136         false => { println!("Noooo!"); }
137     };
138
139     match test {
140
141
142
143         false => { println!("Noooo!"); }
144         _ => (),
145     };
146
147     match test && test {
148
149
150
151
152         false => { println!("Noooo!"); }
153         _ => (),
154     };
155
156     match test {
157
158
159
160         false => { println!("Noooo!"); }
161         true => { println!("Yes!"); }
162     };
163
164     // Not linted
165     match option {
166         1 ... 10 => 1,
167         11 ... 20 => 2,
168         _ => 3,
169     };
170 }
171
172 fn ref_pats() {
173     {
174         let v = &Some(0);
175         match v {
176
177
178
179             &Some(v) => println!("{:?}", v),
180             &None => println!("none"),
181         }
182         match v {  // this doesn't trigger, we have a different pattern
183             &Some(v) => println!("some"),
184             other => println!("other"),
185         }
186     }
187     let tup =& (1, 2);
188     match tup {
189
190
191
192         &(v, 1) => println!("{}", v),
193         _ => println!("none"),
194     }
195     // special case: using & both in expr and pats
196     let w = Some(0);
197     match &w {
198
199
200
201         &Some(v) => println!("{:?}", v),
202         &None => println!("none"),
203     }
204     // false positive: only wildcard pattern
205     let w = Some(0);
206     match w {
207         _ => println!("none"),
208     }
209
210     let a = &Some(0);
211     if let &None = a {
212
213
214
215         println!("none");
216     }
217
218     let b = Some(0);
219     if let &None = &b {
220
221
222
223         println!("none");
224     }
225 }
226
227 fn overlapping() {
228     const FOO : u64 = 2;
229
230     match 42 {
231         0 ... 10 => println!("0 ... 10"),
232         0 ... 11 => println!("0 ... 11"),
233         _ => (),
234     }
235
236     match 42 {
237         0 ... 5 => println!("0 ... 5"),
238         6 ... 7 => println!("6 ... 7"),
239         FOO ... 11 => println!("0 ... 11"),
240         _ => (),
241     }
242
243     match 42 {
244         2 => println!("2"),
245         0 ... 5 => println!("0 ... 5"),
246         _ => (),
247     }
248
249     match 42 {
250         2 => println!("2"),
251         0 ... 2 => println!("0 ... 2"),
252         _ => (),
253     }
254
255     match 42 {
256         0 ... 10 => println!("0 ... 10"),
257         11 ... 50 => println!("11 ... 50"),
258         _ => (),
259     }
260
261     match 42 {
262         2 => println!("2"),
263         0 .. 2 => println!("0 .. 2"),
264         _ => (),
265     }
266
267     match 42 {
268         0 .. 10 => println!("0 .. 10"),
269         10 .. 50 => println!("10 .. 50"),
270         _ => (),
271     }
272
273     match 42 {
274         0 .. 11 => println!("0 .. 11"),
275         0 ... 11 => println!("0 ... 11"),
276         _ => (),
277     }
278
279     if let None = Some(42) {
280         // nothing
281     } else if let None = Some(42) {
282         // another nothing :-)
283     }
284 }
285
286 fn main() {
287 }