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