]> git.lizzy.rs Git - rust.git/blob - tests/ui/matches.rs
Split up some single_match UI tests
[rust.git] / 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, match_same_arms)]
8
9 enum ExprNode {
10     ExprAddrOf,
11     Butterflies,
12     Unicorns,
13 }
14
15 static NODE: ExprNode = ExprNode::Unicorns;
16
17 fn dummy() {
18 }
19
20 fn unwrap_addr() -> Option<&'static ExprNode> {
21     match ExprNode::Butterflies {
22         ExprNode::ExprAddrOf => Some(&NODE),
23         _ => { let x = 5; None },
24     }
25 }
26
27 fn match_bool() {
28     let test: bool = true;
29
30     match test {
31         true => 0,
32         false => 42,
33     };
34
35     let option = 1;
36     match option == 1 {
37         true => 1,
38         false => 0,
39     };
40
41     match test {
42         true => (),
43         false => { println!("Noooo!"); }
44     };
45
46     match test {
47         false => { println!("Noooo!"); }
48         _ => (),
49     };
50
51     match test && test {
52         false => { println!("Noooo!"); }
53         _ => (),
54     };
55
56     match test {
57         false => { println!("Noooo!"); }
58         true => { println!("Yes!"); }
59     };
60
61     // Not linted
62     match option {
63         1 ... 10 => 1,
64         11 ... 20 => 2,
65         _ => 3,
66     };
67 }
68
69 fn ref_pats() {
70     {
71         let v = &Some(0);
72         match v {
73             &Some(v) => println!("{:?}", v),
74             &None => println!("none"),
75         }
76         match v {  // this doesn't trigger, we have a different pattern
77             &Some(v) => println!("some"),
78             other => println!("other"),
79         }
80     }
81     let tup =& (1, 2);
82     match tup {
83         &(v, 1) => println!("{}", v),
84         _ => println!("none"),
85     }
86     // special case: using & both in expr and pats
87     let w = Some(0);
88     match &w {
89         &Some(v) => println!("{:?}", v),
90         &None => println!("none"),
91     }
92     // false positive: only wildcard pattern
93     let w = Some(0);
94     match w {
95         _ => println!("none"),
96     }
97
98     let a = &Some(0);
99     if let &None = a {
100         println!("none");
101     }
102
103     let b = Some(0);
104     if let &None = &b {
105         println!("none");
106     }
107 }
108
109 fn overlapping() {
110     const FOO : u64 = 2;
111
112     match 42 {
113         0 ... 10 => println!("0 ... 10"),
114         0 ... 11 => println!("0 ... 11"),
115         _ => (),
116     }
117
118     match 42 {
119         0 ... 5 => println!("0 ... 5"),
120         6 ... 7 => println!("6 ... 7"),
121         FOO ... 11 => println!("0 ... 11"),
122         _ => (),
123     }
124
125     match 42 {
126         2 => println!("2"),
127         0 ... 5 => println!("0 ... 5"),
128         _ => (),
129     }
130
131     match 42 {
132         2 => println!("2"),
133         0 ... 2 => println!("0 ... 2"),
134         _ => (),
135     }
136
137     match 42 {
138         0 ... 10 => println!("0 ... 10"),
139         11 ... 50 => println!("11 ... 50"),
140         _ => (),
141     }
142
143     match 42 {
144         2 => println!("2"),
145         0 .. 2 => println!("0 .. 2"),
146         _ => (),
147     }
148
149     match 42 {
150         0 .. 10 => println!("0 .. 10"),
151         10 .. 50 => println!("10 .. 50"),
152         _ => (),
153     }
154
155     match 42 {
156         0 .. 11 => println!("0 .. 11"),
157         0 ... 11 => println!("0 ... 11"),
158         _ => (),
159     }
160
161     if let None = Some(42) {
162         // nothing
163     } else if let None = Some(42) {
164         // another nothing :-)
165     }
166 }
167
168 fn match_wild_err_arm() {
169     let x: Result<i32, &str> = Ok(3);
170
171     match x {
172         Ok(3) => println!("ok"),
173         Ok(_) => println!("ok"),
174         Err(_) => panic!("err")
175     }
176
177     match x {
178         Ok(3) => println!("ok"),
179         Ok(_) => println!("ok"),
180         Err(_) => {panic!()}
181     }
182
183     match x {
184         Ok(3) => println!("ok"),
185         Ok(_) => println!("ok"),
186         Err(_) => {panic!();}
187     }
188
189     // allowed when not with `panic!` block
190     match x {
191         Ok(3) => println!("ok"),
192         Ok(_) => println!("ok"),
193         Err(_) => println!("err")
194     }
195
196     // allowed when used with `unreachable!`
197     match x {
198         Ok(3) => println!("ok"),
199         Ok(_) => println!("ok"),
200         Err(_) => {unreachable!()}
201     }
202
203     match x {
204         Ok(3) => println!("ok"),
205         Ok(_) => println!("ok"),
206         Err(_) => unreachable!()
207     }
208
209     match x {
210         Ok(3) => println!("ok"),
211         Ok(_) => println!("ok"),
212         Err(_) => {unreachable!();}
213     }
214
215     // no warning because of the guard
216     match x {
217         Ok(x) if x*x == 64 => println!("ok"),
218         Ok(_) => println!("ok"),
219         Err(_) => println!("err")
220     }
221
222     // this used to be a false positive, see #1996
223     match x {
224         Ok(3) => println!("ok"),
225         Ok(x) if x*x == 64 => println!("ok 64"),
226         Ok(_) => println!("ok"),
227         Err(_) => println!("err")
228     }
229
230     match (x, Some(1i32)) {
231         (Ok(x), Some(_)) => println!("ok {}", x),
232         (Ok(_), Some(x)) => println!("ok {}", x),
233         _ => println!("err")
234     }
235
236     // no warning because of the different types for x
237     match (x, Some(1.0f64)) {
238         (Ok(x), Some(_)) => println!("ok {}", x),
239         (Ok(_), Some(x)) => println!("ok {}", x),
240         _ => println!("err")
241     }
242
243     // because of a bug, no warning was generated for this case before #2251
244     match x {
245         Ok(_tmp) => println!("ok"),
246         Ok(3) => println!("ok"),
247         Ok(_) => println!("ok"),
248         Err(_) => {unreachable!();}
249     }
250 }
251
252 fn match_as_ref() {
253     let owned: Option<()> = None;
254     let borrowed: Option<&()> = match owned {
255         None => None,
256         Some(ref v) => Some(v),
257     };
258
259     let mut mut_owned: Option<()> = None;
260     let borrow_mut: Option<&mut ()> = match mut_owned {
261         None => None,
262         Some(ref mut v) => Some(v),
263     };
264
265 }
266
267 fn main() {
268 }