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