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