]> git.lizzy.rs Git - rust.git/blob - tests/ui/cyclomatic_complexity.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / cyclomatic_complexity.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![allow(clippy::all)]
11 #![warn(clippy::cyclomatic_complexity)]
12 #![allow(unused)]
13
14 #[rustfmt::skip]
15 fn main() {
16     if true {
17         println!("a");
18     }
19     if true {
20         println!("a");
21     }
22     if true {
23         println!("a");
24     }
25     if true {
26         println!("a");
27     }
28     if true {
29         println!("a");
30     }
31     if true {
32         println!("a");
33     }
34     if true {
35         println!("a");
36     }
37     if true {
38         println!("a");
39     }
40     if true {
41         println!("a");
42     }
43     if true {
44         println!("a");
45     }
46     if true {
47         println!("a");
48     }
49     if true {
50         println!("a");
51     }
52     if true {
53         println!("a");
54     }
55     if true {
56         println!("a");
57     }
58     if true {
59         println!("a");
60     }
61     if true {
62         println!("a");
63     }
64     if true {
65         println!("a");
66     }
67     if true {
68         println!("a");
69     }
70     if true {
71         println!("a");
72     }
73     if true {
74         println!("a");
75     }
76     if true {
77         println!("a");
78     }
79     if true {
80         println!("a");
81     }
82     if true {
83         println!("a");
84     }
85     if true {
86         println!("a");
87     }
88     if true {
89         println!("a");
90     }
91     if true {
92         println!("a");
93     }
94     if true {
95         println!("a");
96     }
97 }
98
99 #[clippy::cyclomatic_complexity = "0"]
100 fn kaboom() {
101     let n = 0;
102     'a: for i in 0..20 {
103         'b: for j in i..20 {
104             for k in j..20 {
105                 if k == 5 {
106                     break 'b;
107                 }
108                 if j == 3 && k == 6 {
109                     continue 'a;
110                 }
111                 if k == j {
112                     continue;
113                 }
114                 println!("bake");
115             }
116         }
117         println!("cake");
118     }
119 }
120
121 fn bloo() {
122     match 42 {
123         0 => println!("hi"),
124         1 => println!("hai"),
125         2 => println!("hey"),
126         3 => println!("hallo"),
127         4 => println!("hello"),
128         5 => println!("salut"),
129         6 => println!("good morning"),
130         7 => println!("good evening"),
131         8 => println!("good afternoon"),
132         9 => println!("good night"),
133         10 => println!("bonjour"),
134         11 => println!("hej"),
135         12 => println!("hej hej"),
136         13 => println!("greetings earthling"),
137         14 => println!("take us to you leader"),
138         15 | 17 | 19 | 21 | 23 | 25 | 27 | 29 | 31 | 33 => println!("take us to you leader"),
139         35 | 37 | 39 | 41 | 43 | 45 | 47 | 49 | 51 | 53 => println!("there is no undefined behavior"),
140         55 | 57 | 59 | 61 | 63 | 65 | 67 | 69 | 71 | 73 => println!("I know borrow-fu"),
141         _ => println!("bye"),
142     }
143 }
144
145 #[clippy::cyclomatic_complexity = "0"]
146 fn lots_of_short_circuits() -> bool {
147     true && false && true && false && true && false && true
148 }
149
150 #[clippy::cyclomatic_complexity = "0"]
151 fn lots_of_short_circuits2() -> bool {
152     true || false || true || false || true || false || true
153 }
154
155 #[clippy::cyclomatic_complexity = "0"]
156 fn baa() {
157     let x = || match 99 {
158         0 => 0,
159         1 => 1,
160         2 => 2,
161         4 => 4,
162         6 => 6,
163         9 => 9,
164         _ => 42,
165     };
166     if x() == 42 {
167         println!("x");
168     } else {
169         println!("not x");
170     }
171 }
172
173 #[clippy::cyclomatic_complexity = "0"]
174 fn bar() {
175     match 99 {
176         0 => println!("hi"),
177         _ => println!("bye"),
178     }
179 }
180
181 #[test]
182 #[clippy::cyclomatic_complexity = "0"]
183 /// Tests are usually complex but simple at the same time. `clippy::cyclomatic_complexity` used to
184 /// give lots of false-positives in tests.
185 fn dont_warn_on_tests() {
186     match 99 {
187         0 => println!("hi"),
188         _ => println!("bye"),
189     }
190 }
191
192 #[clippy::cyclomatic_complexity = "0"]
193 fn barr() {
194     match 99 {
195         0 => println!("hi"),
196         1 => println!("bla"),
197         2 | 3 => println!("blub"),
198         _ => println!("bye"),
199     }
200 }
201
202 #[clippy::cyclomatic_complexity = "0"]
203 fn barr2() {
204     match 99 {
205         0 => println!("hi"),
206         1 => println!("bla"),
207         2 | 3 => println!("blub"),
208         _ => println!("bye"),
209     }
210     match 99 {
211         0 => println!("hi"),
212         1 => println!("bla"),
213         2 | 3 => println!("blub"),
214         _ => println!("bye"),
215     }
216 }
217
218 #[clippy::cyclomatic_complexity = "0"]
219 fn barrr() {
220     match 99 {
221         0 => println!("hi"),
222         1 => panic!("bla"),
223         2 | 3 => println!("blub"),
224         _ => println!("bye"),
225     }
226 }
227
228 #[clippy::cyclomatic_complexity = "0"]
229 fn barrr2() {
230     match 99 {
231         0 => println!("hi"),
232         1 => panic!("bla"),
233         2 | 3 => println!("blub"),
234         _ => println!("bye"),
235     }
236     match 99 {
237         0 => println!("hi"),
238         1 => panic!("bla"),
239         2 | 3 => println!("blub"),
240         _ => println!("bye"),
241     }
242 }
243
244 #[clippy::cyclomatic_complexity = "0"]
245 fn barrrr() {
246     match 99 {
247         0 => println!("hi"),
248         1 => println!("bla"),
249         2 | 3 => panic!("blub"),
250         _ => println!("bye"),
251     }
252 }
253
254 #[clippy::cyclomatic_complexity = "0"]
255 fn barrrr2() {
256     match 99 {
257         0 => println!("hi"),
258         1 => println!("bla"),
259         2 | 3 => panic!("blub"),
260         _ => println!("bye"),
261     }
262     match 99 {
263         0 => println!("hi"),
264         1 => println!("bla"),
265         2 | 3 => panic!("blub"),
266         _ => println!("bye"),
267     }
268 }
269
270 #[clippy::cyclomatic_complexity = "0"]
271 fn cake() {
272     if 4 == 5 {
273         println!("yea");
274     } else {
275         panic!("meh");
276     }
277     println!("whee");
278 }
279
280 #[clippy::cyclomatic_complexity = "0"]
281 pub fn read_file(input_path: &str) -> String {
282     use std::fs::File;
283     use std::io::{Read, Write};
284     use std::path::Path;
285     let mut file = match File::open(&Path::new(input_path)) {
286         Ok(f) => f,
287         Err(err) => {
288             panic!("Can't open {}: {}", input_path, err);
289         },
290     };
291
292     let mut bytes = Vec::new();
293
294     match file.read_to_end(&mut bytes) {
295         Ok(..) => {},
296         Err(_) => {
297             panic!("Can't read {}", input_path);
298         },
299     };
300
301     match String::from_utf8(bytes) {
302         Ok(contents) => contents,
303         Err(_) => {
304             panic!("{} is not UTF-8 encoded", input_path);
305         },
306     }
307 }
308
309 enum Void {}
310
311 #[clippy::cyclomatic_complexity = "0"]
312 fn void(void: Void) {
313     if true {
314         match void {}
315     }
316 }
317
318 #[clippy::cyclomatic_complexity = "0"]
319 fn mcarton_sees_all() {
320     panic!("meh");
321     panic!("möh");
322 }
323
324 #[clippy::cyclomatic_complexity = "0"]
325 fn try() -> Result<i32, &'static str> {
326     match 5 {
327         5 => Ok(5),
328         _ => return Err("bla"),
329     }
330 }
331
332 #[clippy::cyclomatic_complexity = "0"]
333 fn try_again() -> Result<i32, &'static str> {
334     let _ = try!(Ok(42));
335     let _ = try!(Ok(43));
336     let _ = try!(Ok(44));
337     let _ = try!(Ok(45));
338     let _ = try!(Ok(46));
339     let _ = try!(Ok(47));
340     let _ = try!(Ok(48));
341     let _ = try!(Ok(49));
342     match 5 {
343         5 => Ok(5),
344         _ => return Err("bla"),
345     }
346 }
347
348 #[clippy::cyclomatic_complexity = "0"]
349 fn early() -> Result<i32, &'static str> {
350     return Ok(5);
351     return Ok(5);
352     return Ok(5);
353     return Ok(5);
354     return Ok(5);
355     return Ok(5);
356     return Ok(5);
357     return Ok(5);
358     return Ok(5);
359 }
360
361 #[rustfmt::skip]
362 #[clippy::cyclomatic_complexity = "0"]
363 fn early_ret() -> i32 {
364     let a = if true { 42 } else { return 0; };
365     let a = if a < 99 { 42 } else { return 0; };
366     let a = if a < 99 { 42 } else { return 0; };
367     let a = if a < 99 { 42 } else { return 0; };
368     let a = if a < 99 { 42 } else { return 0; };
369     let a = if a < 99 { 42 } else { return 0; };
370     let a = if a < 99 { 42 } else { return 0; };
371     let a = if a < 99 { 42 } else { return 0; };
372     let a = if a < 99 { 42 } else { return 0; };
373     let a = if a < 99 { 42 } else { return 0; };
374     let a = if a < 99 { 42 } else { return 0; };
375     let a = if a < 99 { 42 } else { return 0; };
376     match 5 {
377         5 => 5,
378         _ => return 6,
379     }
380 }