]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/matches.rs
New upstream version 1.25.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / matches.rs
1
2 #![feature(exclusive_range_pattern)]
3
4
5 #![warn(clippy)]
6 #![allow(unused, if_let_redundant_pattern_matching)]
7 #![warn(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 ExprNode::ExprAddrOf => Some(&NODE),
28 _ => { let x = 5; None },
29 }
30 }
31
32 fn single_match(){
33 let x = Some(1u8);
34
35 match x {
36 Some(y) => { println!("{:?}", y); }
37 _ => ()
38 };
39
40 let z = (1u8,1u8);
41 match z {
42 (2...3, 7...9) => dummy(),
43 _ => {}
44 };
45
46 // Not linted (pattern guards used)
47 match x {
48 Some(y) if y == 0 => println!("{:?}", y),
49 _ => ()
50 }
51
52 // Not linted (no block with statements in the single arm)
53 match z {
54 (2...3, 7...9) => println!("{:?}", z),
55 _ => println!("nope"),
56 }
57 }
58
59 fn single_match_know_enum() {
60 let x = Some(1u8);
61 let y : Result<_, i8> = Ok(1i8);
62
63 match x {
64 Some(y) => dummy(),
65 None => ()
66 };
67
68 match y {
69 Ok(y) => dummy(),
70 Err(..) => ()
71 };
72
73 let c = Cow::Borrowed("");
74
75 match c {
76 Cow::Borrowed(..) => dummy(),
77 Cow::Owned(..) => (),
78 };
79
80 let z = Foo::Bar;
81 // no warning
82 match z {
83 Bar => println!("42"),
84 Baz(_) => (),
85 }
86
87 match z {
88 Baz(_) => println!("42"),
89 Bar => (),
90 }
91 }
92
93 fn match_bool() {
94 let test: bool = true;
95
96 match test {
97 true => 0,
98 false => 42,
99 };
100
101 let option = 1;
102 match option == 1 {
103 true => 1,
104 false => 0,
105 };
106
107 match test {
108 true => (),
109 false => { println!("Noooo!"); }
110 };
111
112 match test {
113 false => { println!("Noooo!"); }
114 _ => (),
115 };
116
117 match test && test {
118 false => { println!("Noooo!"); }
119 _ => (),
120 };
121
122 match test {
123 false => { println!("Noooo!"); }
124 true => { println!("Yes!"); }
125 };
126
127 // Not linted
128 match option {
129 1 ... 10 => 1,
130 11 ... 20 => 2,
131 _ => 3,
132 };
133 }
134
135 fn ref_pats() {
136 {
137 let v = &Some(0);
138 match v {
139 &Some(v) => println!("{:?}", v),
140 &None => println!("none"),
141 }
142 match v { // this doesn't trigger, we have a different pattern
143 &Some(v) => println!("some"),
144 other => println!("other"),
145 }
146 }
147 let tup =& (1, 2);
148 match tup {
149 &(v, 1) => println!("{}", v),
150 _ => println!("none"),
151 }
152 // special case: using & both in expr and pats
153 let w = Some(0);
154 match &w {
155 &Some(v) => println!("{:?}", v),
156 &None => println!("none"),
157 }
158 // false positive: only wildcard pattern
159 let w = Some(0);
160 match w {
161 _ => println!("none"),
162 }
163
164 let a = &Some(0);
165 if let &None = a {
166 println!("none");
167 }
168
169 let b = Some(0);
170 if let &None = &b {
171 println!("none");
172 }
173 }
174
175 fn overlapping() {
176 const FOO : u64 = 2;
177
178 match 42 {
179 0 ... 10 => println!("0 ... 10"),
180 0 ... 11 => println!("0 ... 11"),
181 _ => (),
182 }
183
184 match 42 {
185 0 ... 5 => println!("0 ... 5"),
186 6 ... 7 => println!("6 ... 7"),
187 FOO ... 11 => println!("0 ... 11"),
188 _ => (),
189 }
190
191 match 42 {
192 2 => println!("2"),
193 0 ... 5 => println!("0 ... 5"),
194 _ => (),
195 }
196
197 match 42 {
198 2 => println!("2"),
199 0 ... 2 => println!("0 ... 2"),
200 _ => (),
201 }
202
203 match 42 {
204 0 ... 10 => println!("0 ... 10"),
205 11 ... 50 => println!("11 ... 50"),
206 _ => (),
207 }
208
209 match 42 {
210 2 => println!("2"),
211 0 .. 2 => println!("0 .. 2"),
212 _ => (),
213 }
214
215 match 42 {
216 0 .. 10 => println!("0 .. 10"),
217 10 .. 50 => println!("10 .. 50"),
218 _ => (),
219 }
220
221 match 42 {
222 0 .. 11 => println!("0 .. 11"),
223 0 ... 11 => println!("0 ... 11"),
224 _ => (),
225 }
226
227 if let None = Some(42) {
228 // nothing
229 } else if let None = Some(42) {
230 // another nothing :-)
231 }
232 }
233
234 fn match_wild_err_arm() {
235 let x: Result<i32, &str> = Ok(3);
236
237 match x {
238 Ok(3) => println!("ok"),
239 Ok(_) => println!("ok"),
240 Err(_) => panic!("err")
241 }
242
243 match x {
244 Ok(3) => println!("ok"),
245 Ok(_) => println!("ok"),
246 Err(_) => {panic!()}
247 }
248
249 match x {
250 Ok(3) => println!("ok"),
251 Ok(_) => println!("ok"),
252 Err(_) => {panic!();}
253 }
254
255 // allowed when not with `panic!` block
256 match x {
257 Ok(3) => println!("ok"),
258 Ok(_) => println!("ok"),
259 Err(_) => println!("err")
260 }
261
262 // allowed when used with `unreachable!`
263 match x {
264 Ok(3) => println!("ok"),
265 Ok(_) => println!("ok"),
266 Err(_) => {unreachable!()}
267 }
268
269 match x {
270 Ok(3) => println!("ok"),
271 Ok(_) => println!("ok"),
272 Err(_) => unreachable!()
273 }
274
275 match x {
276 Ok(3) => println!("ok"),
277 Ok(_) => println!("ok"),
278 Err(_) => {unreachable!();}
279 }
280
281 // no warning because of the guard
282 match x {
283 Ok(x) if x*x == 64 => println!("ok"),
284 Ok(_) => println!("ok"),
285 Err(_) => println!("err")
286 }
287
288 // this used to be a false positive, see #1996
289 match x {
290 Ok(3) => println!("ok"),
291 Ok(x) if x*x == 64 => println!("ok 64"),
292 Ok(_) => println!("ok"),
293 Err(_) => println!("err")
294 }
295
296 match (x, Some(1i32)) {
297 (Ok(x), Some(_)) => println!("ok {}", x),
298 (Ok(_), Some(x)) => println!("ok {}", x),
299 _ => println!("err")
300 }
301
302 // no warning because of the different types for x
303 match (x, Some(1.0f64)) {
304 (Ok(x), Some(_)) => println!("ok {}", x),
305 (Ok(_), Some(x)) => println!("ok {}", x),
306 _ => println!("err")
307 }
308
309 // because of a bug, no warning was generated for this case before #2251
310 match x {
311 Ok(_tmp) => println!("ok"),
312 Ok(3) => println!("ok"),
313 Ok(_) => println!("ok"),
314 Err(_) => {unreachable!();}
315 }
316 }
317
318 fn match_as_ref() {
319 let owned: Option<()> = None;
320 let borrowed: Option<&()> = match owned {
321 None => None,
322 Some(ref v) => Some(v),
323 };
324
325 let mut mut_owned: Option<()> = None;
326 let borrow_mut: Option<&mut ()> = match mut_owned {
327 None => None,
328 Some(ref mut v) => Some(v),
329 };
330
331 }
332
333 fn main() {
334 }