]> git.lizzy.rs Git - rust.git/blob - tests/ui/matches.rs
Merge pull request #3291 from JoshMcguigan/cmp_owned-3289
[rust.git] / tests / ui / matches.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10
11
12 #![feature(exclusive_range_pattern)]
13
14
15 #![warn(clippy::all)]
16 #![allow(unused, clippy::if_let_redundant_pattern_matching)]
17 #![warn(clippy::single_match_else, clippy::match_same_arms)]
18
19 enum ExprNode {
20     ExprAddrOf,
21     Butterflies,
22     Unicorns,
23 }
24
25 static NODE: ExprNode = ExprNode::Unicorns;
26
27 fn dummy() {
28 }
29
30 fn unwrap_addr() -> Option<&'static ExprNode> {
31     match ExprNode::Butterflies {
32         ExprNode::ExprAddrOf => Some(&NODE),
33         _ => { let x = 5; None },
34     }
35 }
36
37 fn ref_pats() {
38     {
39         let v = &Some(0);
40         match v {
41             &Some(v) => println!("{:?}", v),
42             &None => println!("none"),
43         }
44         match v {  // this doesn't trigger, we have a different pattern
45             &Some(v) => println!("some"),
46             other => println!("other"),
47         }
48     }
49     let tup =& (1, 2);
50     match tup {
51         &(v, 1) => println!("{}", v),
52         _ => println!("none"),
53     }
54     // special case: using & both in expr and pats
55     let w = Some(0);
56     match &w {
57         &Some(v) => println!("{:?}", v),
58         &None => println!("none"),
59     }
60     // false positive: only wildcard pattern
61     let w = Some(0);
62     match w {
63         _ => println!("none"),
64     }
65
66     let a = &Some(0);
67     if let &None = a {
68         println!("none");
69     }
70
71     let b = Some(0);
72     if let &None = &b {
73         println!("none");
74     }
75 }
76
77 fn overlapping() {
78     const FOO : u64 = 2;
79
80     match 42 {
81         0 ... 10 => println!("0 ... 10"),
82         0 ... 11 => println!("0 ... 11"),
83         _ => (),
84     }
85
86     match 42 {
87         0 ... 5 => println!("0 ... 5"),
88         6 ... 7 => println!("6 ... 7"),
89         FOO ... 11 => println!("0 ... 11"),
90         _ => (),
91     }
92
93     match 42 {
94         2 => println!("2"),
95         0 ... 5 => println!("0 ... 5"),
96         _ => (),
97     }
98
99     match 42 {
100         2 => println!("2"),
101         0 ... 2 => println!("0 ... 2"),
102         _ => (),
103     }
104
105     match 42 {
106         0 ... 10 => println!("0 ... 10"),
107         11 ... 50 => println!("11 ... 50"),
108         _ => (),
109     }
110
111     match 42 {
112         2 => println!("2"),
113         0 .. 2 => println!("0 .. 2"),
114         _ => (),
115     }
116
117     match 42 {
118         0 .. 10 => println!("0 .. 10"),
119         10 .. 50 => println!("10 .. 50"),
120         _ => (),
121     }
122
123     match 42 {
124         0 .. 11 => println!("0 .. 11"),
125         0 ... 11 => println!("0 ... 11"),
126         _ => (),
127     }
128
129     if let None = Some(42) {
130         // nothing
131     } else if let None = Some(42) {
132         // another nothing :-)
133     }
134 }
135
136 fn match_wild_err_arm() {
137     let x: Result<i32, &str> = Ok(3);
138
139     match x {
140         Ok(3) => println!("ok"),
141         Ok(_) => println!("ok"),
142         Err(_) => panic!("err")
143     }
144
145     match x {
146         Ok(3) => println!("ok"),
147         Ok(_) => println!("ok"),
148         Err(_) => {panic!()}
149     }
150
151     match x {
152         Ok(3) => println!("ok"),
153         Ok(_) => println!("ok"),
154         Err(_) => {panic!();}
155     }
156
157     // allowed when not with `panic!` block
158     match x {
159         Ok(3) => println!("ok"),
160         Ok(_) => println!("ok"),
161         Err(_) => println!("err")
162     }
163
164     // allowed when used with `unreachable!`
165     match x {
166         Ok(3) => println!("ok"),
167         Ok(_) => println!("ok"),
168         Err(_) => {unreachable!()}
169     }
170
171     match x {
172         Ok(3) => println!("ok"),
173         Ok(_) => println!("ok"),
174         Err(_) => unreachable!()
175     }
176
177     match x {
178         Ok(3) => println!("ok"),
179         Ok(_) => println!("ok"),
180         Err(_) => {unreachable!();}
181     }
182
183     // no warning because of the guard
184     match x {
185         Ok(x) if x*x == 64 => println!("ok"),
186         Ok(_) => println!("ok"),
187         Err(_) => println!("err")
188     }
189
190     // this used to be a false positive, see #1996
191     match x {
192         Ok(3) => println!("ok"),
193         Ok(x) if x*x == 64 => println!("ok 64"),
194         Ok(_) => println!("ok"),
195         Err(_) => println!("err")
196     }
197
198     match (x, Some(1i32)) {
199         (Ok(x), Some(_)) => println!("ok {}", x),
200         (Ok(_), Some(x)) => println!("ok {}", x),
201         _ => println!("err")
202     }
203
204     // no warning because of the different types for x
205     match (x, Some(1.0f64)) {
206         (Ok(x), Some(_)) => println!("ok {}", x),
207         (Ok(_), Some(x)) => println!("ok {}", x),
208         _ => println!("err")
209     }
210
211     // because of a bug, no warning was generated for this case before #2251
212     match x {
213         Ok(_tmp) => println!("ok"),
214         Ok(3) => println!("ok"),
215         Ok(_) => println!("ok"),
216         Err(_) => {unreachable!();}
217     }
218 }
219
220 fn match_as_ref() {
221     let owned: Option<()> = None;
222     let borrowed: Option<&()> = match owned {
223         None => None,
224         Some(ref v) => Some(v),
225     };
226
227     let mut mut_owned: Option<()> = None;
228     let borrow_mut: Option<&mut ()> = match mut_owned {
229         None => None,
230         Some(ref mut v) => Some(v),
231     };
232
233 }
234
235 fn main() {
236 }