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