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