]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/copies.rs
68756a57cc75ee21c5dd4e88555d87bb5dab7253
[rust.git] / tests / compile-fail / copies.rs
1 #![feature(plugin, inclusive_range_syntax)]
2 #![plugin(clippy)]
3
4 #![allow(dead_code, no_effect)]
5 #![allow(let_and_return)]
6 #![allow(needless_return)]
7 #![allow(unused_variables)]
8 #![allow(cyclomatic_complexity)]
9 #![allow(blacklisted_name)]
10
11 fn bar<T>(_: T) {}
12 fn foo() -> bool { unimplemented!() }
13
14 struct Foo {
15     bar: u8,
16 }
17
18 #[deny(if_same_then_else)]
19 #[deny(match_same_arms)]
20 fn if_same_then_else() -> Result<&'static str, ()> {
21     if true {
22         Foo { bar: 42 };
23         0..10;
24         ..;
25         0..;
26         ..10;
27         0...10;
28         foo();
29     }
30     else { //~ERROR this `if` has identical blocks
31         Foo { bar: 42 };
32         0..10;
33         ..;
34         0..;
35         ..10;
36         0...10;
37         foo();
38     }
39
40     if true {
41         Foo { bar: 42 };
42     }
43     else {
44         Foo { bar: 43 };
45     }
46
47     if true {
48         0..10;
49     }
50     else {
51         0...10;
52     }
53
54     if true {
55         foo();
56         foo();
57     }
58     else {
59         foo();
60     }
61
62     let _ = if true {
63         foo();
64         42
65     }
66     else { //~ERROR this `if` has identical blocks
67         foo();
68         42
69     };
70
71     if true {
72         foo();
73     }
74
75     let _ = if true {
76         42
77     }
78     else { //~ERROR this `if` has identical blocks
79         42
80     };
81
82     if true {
83         let bar = if true {
84             42
85         }
86         else {
87             43
88         };
89
90         while foo() { break; }
91         bar + 1;
92     }
93     else { //~ERROR this `if` has identical blocks
94         let bar = if true {
95             42
96         }
97         else {
98             43
99         };
100
101         while foo() { break; }
102         bar + 1;
103     }
104
105     if true {
106         let _ = match 42 {
107             42 => 1,
108             a if a > 0 => 2,
109             10...15 => 3,
110             _ => 4,
111         };
112     }
113     else if false {
114         foo();
115     }
116     else if foo() { //~ERROR this `if` has identical blocks
117         let _ = match 42 {
118             42 => 1,
119             a if a > 0 => 2,
120             10...15 => 3,
121             _ => 4,
122         };
123     }
124
125     if true {
126         if let Some(a) = Some(42) {}
127     }
128     else { //~ERROR this `if` has identical blocks
129         if let Some(a) = Some(42) {}
130     }
131
132     if true {
133         if let Some(a) = Some(42) {}
134     }
135     else {
136         if let Some(a) = Some(43) {}
137     }
138
139     let _ = match 42 {
140         42 => foo(),
141         51 => foo(), //~ERROR this `match` has identical arm bodies
142         _ => true,
143     };
144
145     let _ = match Some(42) {
146         Some(_) => 24,
147         None => 24,
148     };
149
150     let _ = match Some(42) {
151         Some(42) => 24,
152         Some(a) => 24, // bindings are different
153         None => 0,
154     };
155
156     let _ = match Some(42) {
157         Some(a) if a > 0 => 24,
158         Some(a) => 24, // one arm has a guard
159         None => 0,
160     };
161
162     match (Some(42), Some(42)) {
163         (Some(a), None) => bar(a),
164         (None, Some(a)) => bar(a), //~ERROR this `match` has identical arm bodies
165         _ => (),
166     }
167
168     match (Some(42), Some("")) {
169         (Some(a), None) => bar(a),
170         (None, Some(a)) => bar(a), // bindings have different types
171         _ => (),
172     }
173
174     if true {
175         try!(Ok("foo"));
176     }
177     else { //~ERROR this `if` has identical blocks
178         try!(Ok("foo"));
179     }
180
181     if true {
182         let foo = "";
183         return Ok(&foo[0..]);
184     }
185     else if false {
186         let foo = "bar";
187         return Ok(&foo[0..]);
188     }
189     else { //~ERROR this `if` has identical blocks
190         let foo = "";
191         return Ok(&foo[0..]);
192     }
193 }
194
195 #[deny(ifs_same_cond)]
196 #[allow(if_same_then_else)] // all empty blocks
197 fn ifs_same_cond() {
198     let a = 0;
199     let b = false;
200
201     if b {
202     }
203     else if b { //~ERROR this `if` has the same condition as a previous if
204     }
205
206     if a == 1 {
207     }
208     else if a == 1 { //~ERROR this `if` has the same condition as a previous if
209     }
210
211     if 2*a == 1 {
212     }
213     else if 2*a == 2 {
214     }
215     else if 2*a == 1 { //~ERROR this `if` has the same condition as a previous if
216     }
217     else if a == 1 {
218     }
219
220     // See #659
221     if cfg!(feature = "feature1-659") {
222         1
223     } else if cfg!(feature = "feature2-659") {
224         2
225     } else {
226         3
227     };
228
229     let mut v = vec![1];
230     if v.pop() == None { // ok, functions
231     }
232     else if v.pop() == None {
233     }
234
235     if v.len() == 42 { // ok, functions
236     }
237     else if v.len() == 42 {
238     }
239 }
240
241 fn main() {}