]> git.lizzy.rs Git - rust.git/blob - tests/ui/manual_map_option.rs
Auto merge of #8359 - flip1995:rustup, r=flip1995
[rust.git] / tests / ui / manual_map_option.rs
1 // run-rustfix
2
3 #![warn(clippy::manual_map)]
4 #![allow(
5     clippy::no_effect,
6     clippy::map_identity,
7     clippy::unit_arg,
8     clippy::match_ref_pats,
9     clippy::redundant_pattern_matching,
10     dead_code
11 )]
12
13 fn main() {
14     match Some(0) {
15         Some(_) => Some(2),
16         None::<u32> => None,
17     };
18
19     match Some(0) {
20         Some(x) => Some(x + 1),
21         _ => None,
22     };
23
24     match Some("") {
25         Some(x) => Some(x.is_empty()),
26         None => None,
27     };
28
29     if let Some(x) = Some(0) {
30         Some(!x)
31     } else {
32         None
33     };
34
35     #[rustfmt::skip]
36     match Some(0) {
37         Some(x) => { Some(std::convert::identity(x)) }
38         None => { None }
39     };
40
41     match Some(&String::new()) {
42         Some(x) => Some(str::len(x)),
43         None => None,
44     };
45
46     match Some(0) {
47         Some(x) if false => Some(x + 1),
48         _ => None,
49     };
50
51     match &Some([0, 1]) {
52         Some(x) => Some(x[0]),
53         &None => None,
54     };
55
56     match &Some(0) {
57         &Some(x) => Some(x * 2),
58         None => None,
59     };
60
61     match Some(String::new()) {
62         Some(ref x) => Some(x.is_empty()),
63         _ => None,
64     };
65
66     match &&Some(String::new()) {
67         Some(x) => Some(x.len()),
68         _ => None,
69     };
70
71     match &&Some(0) {
72         &&Some(x) => Some(x + x),
73         &&_ => None,
74     };
75
76     #[warn(clippy::option_map_unit_fn)]
77     match &mut Some(String::new()) {
78         Some(x) => Some(x.push_str("")),
79         None => None,
80     };
81
82     #[allow(clippy::option_map_unit_fn)]
83     {
84         match &mut Some(String::new()) {
85             Some(x) => Some(x.push_str("")),
86             None => None,
87         };
88     }
89
90     match &mut Some(String::new()) {
91         Some(ref x) => Some(x.len()),
92         None => None,
93     };
94
95     match &mut &Some(String::new()) {
96         Some(x) => Some(x.is_empty()),
97         &mut _ => None,
98     };
99
100     match Some((0, 1, 2)) {
101         Some((x, y, z)) => Some(x + y + z),
102         None => None,
103     };
104
105     match Some([1, 2, 3]) {
106         Some([first, ..]) => Some(first),
107         None => None,
108     };
109
110     match &Some((String::new(), "test")) {
111         Some((x, y)) => Some((y, x)),
112         None => None,
113     };
114
115     match Some((String::new(), 0)) {
116         Some((ref x, y)) => Some((y, x)),
117         None => None,
118     };
119
120     match Some(Some(0)) {
121         Some(Some(_)) | Some(None) => Some(0),
122         None => None,
123     };
124
125     match Some(Some((0, 1))) {
126         Some(Some((x, 1))) => Some(x),
127         _ => None,
128     };
129
130     // #6795
131     fn f1() -> Result<(), ()> {
132         let _ = match Some(Ok(())) {
133             Some(x) => Some(x?),
134             None => None,
135         };
136         Ok(())
137     }
138
139     for &x in Some(Some(true)).iter() {
140         let _ = match x {
141             Some(x) => Some(if x { continue } else { x }),
142             None => None,
143         };
144     }
145
146     // #6797
147     let x1 = (Some(String::new()), 0);
148     let x2 = x1.0;
149     match x2 {
150         Some(x) => Some((x, x1.1)),
151         None => None,
152     };
153
154     struct S1 {
155         x: Option<String>,
156         y: u32,
157     }
158     impl S1 {
159         fn f(self) -> Option<(String, u32)> {
160             match self.x {
161                 Some(x) => Some((x, self.y)),
162                 None => None,
163             }
164         }
165     }
166
167     // #6811
168     match Some(0) {
169         Some(x) => Some(vec![x]),
170         None => None,
171     };
172
173     match option_env!("") {
174         Some(x) => Some(String::from(x)),
175         None => None,
176     };
177
178     // #6819
179     async fn f2(x: u32) -> u32 {
180         x
181     }
182
183     async fn f3() {
184         match Some(0) {
185             Some(x) => Some(f2(x).await),
186             None => None,
187         };
188     }
189
190     // #6847
191     if let Some(_) = Some(0) {
192         Some(0)
193     } else if let Some(x) = Some(0) {
194         Some(x + 1)
195     } else {
196         None
197     };
198
199     if true {
200         Some(0)
201     } else if let Some(x) = Some(0) {
202         Some(x + 1)
203     } else {
204         None
205     };
206
207     // #6967
208     const fn f4() {
209         match Some(0) {
210             Some(x) => Some(x + 1),
211             None => None,
212         };
213     }
214
215     // #7077
216     let s = &String::new();
217     let _: Option<&str> = match Some(s) {
218         Some(s) => Some(s),
219         None => None,
220     };
221 }