]>
git.proxmox.com Git - rustc.git/blob - src/tools/clippy/tests/ui/matches.rs
2 #![feature(exclusive_range_pattern)]
6 #![allow(unused, if_let_redundant_pattern_matching)]
7 #![warn(single_match_else)]
11 enum Foo { Bar, Baz(u8) }
20 static NODE
: ExprNode
= ExprNode
::Unicorns
;
25 fn unwrap_addr() -> Option
<&'
static ExprNode
> {
26 match ExprNode
::Butterflies
{
27 ExprNode
::ExprAddrOf
=> Some(&NODE
),
28 _
=> { let x = 5; None }
,
36 Some(y
) => { println!("{:?}
", y); }
42 (2...3, 7...9) => dummy(),
46 // Not linted (pattern guards used)
48 Some(y) if y == 0 => println!("{:?}
", y),
52 // Not linted (no block with statements in the single arm)
54 (2...3, 7...9) => println!("{:?}
", z),
55 _ => println!("nope
"),
59 fn single_match_know_enum() {
61 let y : Result<_, i8> = Ok(1i8);
73 let c = Cow::Borrowed("");
76 Cow::Borrowed(..) => dummy(),
83 Bar => println!("42"),
88 Baz(_) => println!("42"),
94 let test: bool = true;
109 false => { println!("Noooo!"); }
113 false => { println!("Noooo!"); }
118 false => { println!("Noooo!"); }
123 false => { println!("Noooo!"); }
124 true => { println!("Yes!"); }
139 &Some(v) => println!("{:?}
", v),
140 &None => println!("none
"),
142 match v { // this doesn't trigger, we have a different pattern
143 &Some(v) => println!("some
"),
144 other => println!("other
"),
149 &(v, 1) => println!("{}
", v),
150 _ => println!("none
"),
152 // special case: using & both in expr and pats
155 &Some(v) => println!("{:?}
", v),
156 &None => println!("none
"),
158 // false positive: only wildcard pattern
161 _ => println!("none
"),
179 0 ... 10 => println!("0 ... 10"),
180 0 ... 11 => println!("0 ... 11"),
185 0 ... 5 => println!("0 ... 5"),
186 6 ... 7 => println!("6 ... 7"),
187 FOO ... 11 => println!("0 ... 11"),
193 0 ... 5 => println!("0 ... 5"),
199 0 ... 2 => println!("0 ... 2"),
204 0 ... 10 => println!("0 ... 10"),
205 11 ... 50 => println!("11 ... 50"),
211 0 .. 2 => println!("0 .. 2"),
216 0 .. 10 => println!("0 .. 10"),
217 10 .. 50 => println!("10 .. 50"),
222 0 .. 11 => println!("0 .. 11"),
223 0 ... 11 => println!("0 ... 11"),
227 if let None = Some(42) {
229 } else if let None = Some(42) {
230 // another nothing :-)
234 fn match_wild_err_arm() {
235 let x: Result<i32, &str> = Ok(3);
238 Ok(3) => println!("ok
"),
239 Ok(_) => println!("ok
"),
240 Err(_) => panic!("err
")
244 Ok(3) => println!("ok
"),
245 Ok(_) => println!("ok
"),
250 Ok(3) => println!("ok
"),
251 Ok(_) => println!("ok
"),
252 Err(_) => {panic!();}
255 // allowed when not with `panic!` block
257 Ok(3) => println!("ok
"),
258 Ok(_) => println!("ok
"),
259 Err(_) => println!("err
")
262 // allowed when used with `unreachable!`
264 Ok(3) => println!("ok
"),
265 Ok(_) => println!("ok
"),
266 Err(_) => {unreachable!()}
270 Ok(3) => println!("ok
"),
271 Ok(_) => println!("ok
"),
272 Err(_) => unreachable!()
276 Ok(3) => println!("ok
"),
277 Ok(_) => println!("ok
"),
278 Err(_) => {unreachable!();}
281 // no warning because of the guard
283 Ok(x) if x*x == 64 => println!("ok
"),
284 Ok(_) => println!("ok
"),
285 Err(_) => println!("err
")
288 // this used to be a false positive, see #1996
290 Ok(3) => println!("ok
"),
291 Ok(x) if x*x == 64 => println!("ok
64"),
292 Ok(_) => println!("ok
"),
293 Err(_) => println!("err
")
296 match (x, Some(1i32)) {
297 (Ok(x), Some(_)) => println!("ok {}
", x),
298 (Ok(_), Some(x)) => println!("ok {}
", x),
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),
309 // because of a bug, no warning was generated for this case before #2251
311 Ok(_tmp) => println!("ok
"),
312 Ok(3) => println!("ok
"),
313 Ok(_) => println!("ok
"),
314 Err(_) => {unreachable!();}
319 let owned: Option<()> = None;
320 let borrowed: Option<&()> = match owned {
322 Some(ref v) => Some(v),
325 let mut mut_owned: Option<()> = None;
326 let borrow_mut: Option<&mut ()> = match mut_owned {
328 Some(ref mut v) => Some(v),