]> git.lizzy.rs Git - rust.git/blob - tests/ui/matches.rs
Auto merge of #3635 - matthiaskrgr:revert_random_state_3603, r=xfix
[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 #![feature(exclusive_range_pattern)]
11 #![warn(clippy::all)]
12 #![allow(unused, clippy::redundant_pattern_matching)]
13 #![warn(clippy::match_same_arms)]
14
15 fn dummy() {}
16
17 fn ref_pats() {
18     {
19         let v = &Some(0);
20         match v {
21             &Some(v) => println!("{:?}", v),
22             &None => println!("none"),
23         }
24         match v {
25             // this doesn't trigger, we have a different pattern
26             &Some(v) => println!("some"),
27             other => println!("other"),
28         }
29     }
30     let tup = &(1, 2);
31     match tup {
32         &(v, 1) => println!("{}", v),
33         _ => println!("none"),
34     }
35     // special case: using & both in expr and pats
36     let w = Some(0);
37     match &w {
38         &Some(v) => println!("{:?}", v),
39         &None => println!("none"),
40     }
41     // false positive: only wildcard pattern
42     let w = Some(0);
43     match w {
44         _ => println!("none"),
45     }
46
47     let a = &Some(0);
48     if let &None = a {
49         println!("none");
50     }
51
52     let b = Some(0);
53     if let &None = &b {
54         println!("none");
55     }
56 }
57
58 fn match_wild_err_arm() {
59     let x: Result<i32, &str> = Ok(3);
60
61     match x {
62         Ok(3) => println!("ok"),
63         Ok(_) => println!("ok"),
64         Err(_) => panic!("err"),
65     }
66
67     match x {
68         Ok(3) => println!("ok"),
69         Ok(_) => println!("ok"),
70         Err(_) => panic!(),
71     }
72
73     match x {
74         Ok(3) => println!("ok"),
75         Ok(_) => println!("ok"),
76         Err(_) => {
77             panic!();
78         },
79     }
80
81     // allowed when not with `panic!` block
82     match x {
83         Ok(3) => println!("ok"),
84         Ok(_) => println!("ok"),
85         Err(_) => println!("err"),
86     }
87
88     // allowed when used with `unreachable!`
89     match x {
90         Ok(3) => println!("ok"),
91         Ok(_) => println!("ok"),
92         Err(_) => unreachable!(),
93     }
94
95     match x {
96         Ok(3) => println!("ok"),
97         Ok(_) => println!("ok"),
98         Err(_) => unreachable!(),
99     }
100
101     match x {
102         Ok(3) => println!("ok"),
103         Ok(_) => println!("ok"),
104         Err(_) => {
105             unreachable!();
106         },
107     }
108
109     // no warning because of the guard
110     match x {
111         Ok(x) if x * x == 64 => println!("ok"),
112         Ok(_) => println!("ok"),
113         Err(_) => println!("err"),
114     }
115
116     // this used to be a false positive, see #1996
117     match x {
118         Ok(3) => println!("ok"),
119         Ok(x) if x * x == 64 => println!("ok 64"),
120         Ok(_) => println!("ok"),
121         Err(_) => println!("err"),
122     }
123
124     match (x, Some(1i32)) {
125         (Ok(x), Some(_)) => println!("ok {}", x),
126         (Ok(_), Some(x)) => println!("ok {}", x),
127         _ => println!("err"),
128     }
129
130     // no warning because of the different types for x
131     match (x, Some(1.0f64)) {
132         (Ok(x), Some(_)) => println!("ok {}", x),
133         (Ok(_), Some(x)) => println!("ok {}", x),
134         _ => println!("err"),
135     }
136
137     // because of a bug, no warning was generated for this case before #2251
138     match x {
139         Ok(_tmp) => println!("ok"),
140         Ok(3) => println!("ok"),
141         Ok(_) => println!("ok"),
142         Err(_) => {
143             unreachable!();
144         },
145     }
146 }
147
148 fn match_as_ref() {
149     let owned: Option<()> = None;
150     let borrowed: Option<&()> = match owned {
151         None => None,
152         Some(ref v) => Some(v),
153     };
154
155     let mut mut_owned: Option<()> = None;
156     let borrow_mut: Option<&mut ()> = match mut_owned {
157         None => None,
158         Some(ref mut v) => Some(v),
159     };
160 }
161
162 fn main() {}