]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/copies.rs
Add a `BLACKLISTED_NAME` lint
[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(42) => 24,
147         Some(a) => 24, // bindings are different
148         None => 0,
149     };
150
151     match (Some(42), Some(42)) {
152         (Some(a), None) => bar(a),
153         (None, Some(a)) => bar(a), //~ERROR this `match` has identical arm bodies
154         _ => (),
155     }
156
157     match (Some(42), Some("")) {
158         (Some(a), None) => bar(a),
159         (None, Some(a)) => bar(a), // bindings have different types
160         _ => (),
161     }
162
163     if true {
164         try!(Ok("foo"));
165     }
166     else { //~ERROR this `if` has identical blocks
167         try!(Ok("foo"));
168     }
169
170     if true {
171         let foo = "";
172         return Ok(&foo[0..]);
173     }
174     else if false {
175         let foo = "bar";
176         return Ok(&foo[0..]);
177     }
178     else { //~ERROR this `if` has identical blocks
179         let foo = "";
180         return Ok(&foo[0..]);
181     }
182 }
183
184 #[deny(ifs_same_cond)]
185 #[allow(if_same_then_else)] // all empty blocks
186 fn ifs_same_cond() {
187     let a = 0;
188     let b = false;
189
190     if b {
191     }
192     else if b { //~ERROR this `if` has the same condition as a previous if
193     }
194
195     if a == 1 {
196     }
197     else if a == 1 { //~ERROR this `if` has the same condition as a previous if
198     }
199
200     if 2*a == 1 {
201     }
202     else if 2*a == 2 {
203     }
204     else if 2*a == 1 { //~ERROR this `if` has the same condition as a previous if
205     }
206     else if a == 1 {
207     }
208
209     // See #659
210     if cfg!(feature = "feature1-659") {
211         1
212     } else if cfg!(feature = "feature2-659") {
213         2
214     } else {
215         3
216     };
217
218     let mut v = vec![1];
219     if v.pop() == None { // ok, functions
220     }
221     else if v.pop() == None {
222     }
223
224     if v.len() == 42 { // ok, functions
225     }
226     else if v.len() == 42 {
227     }
228 }
229
230 fn main() {}