]> git.lizzy.rs Git - rust.git/blob - tests/ui/copies.rs
Auto merge of #3635 - matthiaskrgr:revert_random_state_3603, r=xfix
[rust.git] / tests / ui / copies.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 #![allow(
11     clippy::blacklisted_name,
12     clippy::collapsible_if,
13     clippy::cyclomatic_complexity,
14     clippy::eq_op,
15     clippy::needless_continue,
16     clippy::needless_return,
17     clippy::never_loop,
18     clippy::no_effect,
19     clippy::zero_divided_by_zero,
20     clippy::unused_unit
21 )]
22
23 fn bar<T>(_: T) {}
24 fn foo() -> bool {
25     unimplemented!()
26 }
27
28 struct Foo {
29     bar: u8,
30 }
31
32 pub enum Abc {
33     A,
34     B,
35     C,
36 }
37
38 #[warn(clippy::if_same_then_else)]
39 #[warn(clippy::match_same_arms)]
40 #[allow(clippy::unused_unit)]
41 fn if_same_then_else() -> Result<&'static str, ()> {
42     if true {
43         Foo { bar: 42 };
44         0..10;
45         ..;
46         0..;
47         ..10;
48         0..=10;
49         foo();
50     } else {
51         //~ ERROR same body as `if` block
52         Foo { bar: 42 };
53         0..10;
54         ..;
55         0..;
56         ..10;
57         0..=10;
58         foo();
59     }
60
61     if true {
62         Foo { bar: 42 };
63     } else {
64         Foo { bar: 43 };
65     }
66
67     if true {
68         ();
69     } else {
70         ()
71     }
72
73     if true {
74         0..10;
75     } else {
76         0..=10;
77     }
78
79     if true {
80         foo();
81         foo();
82     } else {
83         foo();
84     }
85
86     let _ = match 42 {
87         42 => {
88             foo();
89             let mut a = 42 + [23].len() as i32;
90             if true {
91                 a += 7;
92             }
93             a = -31 - a;
94             a
95         },
96         _ => {
97             //~ ERROR match arms have same body
98             foo();
99             let mut a = 42 + [23].len() as i32;
100             if true {
101                 a += 7;
102             }
103             a = -31 - a;
104             a
105         },
106     };
107
108     let _ = match Abc::A {
109         Abc::A => 0,
110         Abc::B => 1,
111         _ => 0, //~ ERROR match arms have same body
112     };
113
114     if true {
115         foo();
116     }
117
118     let _ = if true {
119         42
120     } else {
121         //~ ERROR same body as `if` block
122         42
123     };
124
125     if true {
126         for _ in &[42] {
127             let foo: &Option<_> = &Some::<u8>(42);
128             if true {
129                 break;
130             } else {
131                 continue;
132             }
133         }
134     } else {
135         //~ ERROR same body as `if` block
136         for _ in &[42] {
137             let foo: &Option<_> = &Some::<u8>(42);
138             if true {
139                 break;
140             } else {
141                 continue;
142             }
143         }
144     }
145
146     if true {
147         let bar = if true { 42 } else { 43 };
148
149         while foo() {
150             break;
151         }
152         bar + 1;
153     } else {
154         //~ ERROR same body as `if` block
155         let bar = if true { 42 } else { 43 };
156
157         while foo() {
158             break;
159         }
160         bar + 1;
161     }
162
163     if true {
164         let _ = match 42 {
165             42 => 1,
166             a if a > 0 => 2,
167             10..=15 => 3,
168             _ => 4,
169         };
170     } else if false {
171         foo();
172     } else if foo() {
173         let _ = match 42 {
174             42 => 1,
175             a if a > 0 => 2,
176             10..=15 => 3,
177             _ => 4,
178         };
179     }
180
181     if true {
182         if let Some(a) = Some(42) {}
183     } else {
184         //~ ERROR same body as `if` block
185         if let Some(a) = Some(42) {}
186     }
187
188     if true {
189         if let (1, .., 3) = (1, 2, 3) {}
190     } else {
191         //~ ERROR same body as `if` block
192         if let (1, .., 3) = (1, 2, 3) {}
193     }
194
195     if true {
196         if let (1, .., 3) = (1, 2, 3) {}
197     } else {
198         if let (.., 3) = (1, 2, 3) {}
199     }
200
201     if true {
202         if let (1, .., 3) = (1, 2, 3) {}
203     } else {
204         if let (.., 4) = (1, 2, 3) {}
205     }
206
207     if true {
208         if let (1, .., 3) = (1, 2, 3) {}
209     } else {
210         if let (.., 1, 3) = (1, 2, 3) {}
211     }
212
213     if true {
214         if let Some(42) = None {}
215     } else {
216         if let Option::Some(42) = None {}
217     }
218
219     if true {
220         if let Some(42) = None::<u8> {}
221     } else {
222         if let Some(42) = None {}
223     }
224
225     if true {
226         if let Some(42) = None::<u8> {}
227     } else {
228         if let Some(42) = None::<u32> {}
229     }
230
231     if true {
232         if let Some(a) = Some(42) {}
233     } else {
234         if let Some(a) = Some(43) {}
235     }
236
237     let _ = match 42 {
238         42 => foo(),
239         51 => foo(), //~ ERROR match arms have same body
240         _ => true,
241     };
242
243     let _ = match Some(42) {
244         Some(_) => 24,
245         None => 24, //~ ERROR match arms have same body
246     };
247
248     let _ = match Some(42) {
249         Some(foo) => 24,
250         None => 24,
251     };
252
253     let _ = match Some(42) {
254         Some(42) => 24,
255         Some(a) => 24, // bindings are different
256         None => 0,
257     };
258
259     let _ = match Some(42) {
260         Some(a) if a > 0 => 24,
261         Some(a) => 24, // one arm has a guard
262         None => 0,
263     };
264
265     match (Some(42), Some(42)) {
266         (Some(a), None) => bar(a),
267         (None, Some(a)) => bar(a), //~ ERROR match arms have same body
268         _ => (),
269     }
270
271     match (Some(42), Some(42)) {
272         (Some(a), ..) => bar(a),
273         (.., Some(a)) => bar(a), //~ ERROR match arms have same body
274         _ => (),
275     }
276
277     match (1, 2, 3) {
278         (1, .., 3) => 42,
279         (.., 3) => 42, //~ ERROR match arms have same body
280         _ => 0,
281     };
282
283     let _ = if true {
284         0.0
285     } else {
286         //~ ERROR same body as `if` block
287         0.0
288     };
289
290     let _ = if true {
291         -0.0
292     } else {
293         //~ ERROR same body as `if` block
294         -0.0
295     };
296
297     let _ = if true { 0.0 } else { -0.0 };
298
299     // Different NaNs
300     let _ = if true { 0.0 / 0.0 } else { std::f32::NAN };
301
302     // Same NaNs
303     let _ = if true {
304         std::f32::NAN
305     } else {
306         //~ ERROR same body as `if` block
307         std::f32::NAN
308     };
309
310     let _ = match Some(()) {
311         Some(()) => 0.0,
312         None => -0.0,
313     };
314
315     match (Some(42), Some("")) {
316         (Some(a), None) => bar(a),
317         (None, Some(a)) => bar(a), // bindings have different types
318         _ => (),
319     }
320
321     if true {
322         try!(Ok("foo"));
323     } else {
324         //~ ERROR same body as `if` block
325         try!(Ok("foo"));
326     }
327
328     if true {
329         let foo = "";
330         return Ok(&foo[0..]);
331     } else if false {
332         let foo = "bar";
333         return Ok(&foo[0..]);
334     } else {
335         let foo = "";
336         return Ok(&foo[0..]);
337     }
338
339     // false positive if_same_then_else, let(x,y) vs let(y,x), see #3559
340     if true {
341         let foo = "";
342         let (x, y) = (1, 2);
343         return Ok(&foo[x..y]);
344     } else {
345         let foo = "";
346         let (y, x) = (1, 2);
347         return Ok(&foo[x..y]);
348     }
349 }
350
351 fn main() {}
352
353 // Issue #2423. This was causing an ICE
354 fn func() {
355     if true {
356         f(&[0; 62]);
357         f(&[0; 4]);
358         f(&[0; 3]);
359     } else {
360         f(&[0; 62]);
361         f(&[0; 6]);
362         f(&[0; 6]);
363     }
364 }
365
366 fn f(val: &[u8]) {}