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