1 #![allow(clippy::all)]
2 #![warn(clippy::cognitive_complexity)]
3 #![allow(unused, unused_crate_dependencies)]
4 
5 #[rustfmt::skip]
main()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 = "1"]
kaboom()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 
bloo()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 // Short circuiting operations don't increase the complexity of a function.
137 // Note that the minimum complexity of a function is 1.
138 #[clippy::cognitive_complexity = "1"]
lots_of_short_circuits() -> bool139 fn lots_of_short_circuits() -> bool {
140     true && false && true && false && true && false && true
141 }
142 
143 #[clippy::cognitive_complexity = "1"]
lots_of_short_circuits2() -> bool144 fn lots_of_short_circuits2() -> bool {
145     true || false || true || false || true || false || true
146 }
147 
148 #[clippy::cognitive_complexity = "1"]
baa()149 fn baa() {
150     let x = || match 99 {
151         0 => 0,
152         1 => 1,
153         2 => 2,
154         4 => 4,
155         6 => 6,
156         9 => 9,
157         _ => 42,
158     };
159     if x() == 42 {
160         println!("x");
161     } else {
162         println!("not x");
163     }
164 }
165 
166 #[clippy::cognitive_complexity = "1"]
bar()167 fn bar() {
168     match 99 {
169         0 => println!("hi"),
170         _ => println!("bye"),
171     }
172 }
173 
174 #[test]
175 #[clippy::cognitive_complexity = "1"]
176 /// Tests are usually complex but simple at the same time. `clippy::cognitive_complexity` used to
177 /// give lots of false-positives in tests.
dont_warn_on_tests()178 fn dont_warn_on_tests() {
179     match 99 {
180         0 => println!("hi"),
181         _ => println!("bye"),
182     }
183 }
184 
185 #[clippy::cognitive_complexity = "1"]
barr()186 fn barr() {
187     match 99 {
188         0 => println!("hi"),
189         1 => println!("bla"),
190         2 | 3 => println!("blub"),
191         _ => println!("bye"),
192     }
193 }
194 
195 #[clippy::cognitive_complexity = "1"]
barr2()196 fn barr2() {
197     match 99 {
198         0 => println!("hi"),
199         1 => println!("bla"),
200         2 | 3 => println!("blub"),
201         _ => println!("bye"),
202     }
203     match 99 {
204         0 => println!("hi"),
205         1 => println!("bla"),
206         2 | 3 => println!("blub"),
207         _ => println!("bye"),
208     }
209 }
210 
211 #[clippy::cognitive_complexity = "1"]
barrr()212 fn barrr() {
213     match 99 {
214         0 => println!("hi"),
215         1 => panic!("bla"),
216         2 | 3 => println!("blub"),
217         _ => println!("bye"),
218     }
219 }
220 
221 #[clippy::cognitive_complexity = "1"]
barrr2()222 fn barrr2() {
223     match 99 {
224         0 => println!("hi"),
225         1 => panic!("bla"),
226         2 | 3 => println!("blub"),
227         _ => println!("bye"),
228     }
229     match 99 {
230         0 => println!("hi"),
231         1 => panic!("bla"),
232         2 | 3 => println!("blub"),
233         _ => println!("bye"),
234     }
235 }
236 
237 #[clippy::cognitive_complexity = "1"]
barrrr()238 fn barrrr() {
239     match 99 {
240         0 => println!("hi"),
241         1 => println!("bla"),
242         2 | 3 => panic!("blub"),
243         _ => println!("bye"),
244     }
245 }
246 
247 #[clippy::cognitive_complexity = "1"]
barrrr2()248 fn barrrr2() {
249     match 99 {
250         0 => println!("hi"),
251         1 => println!("bla"),
252         2 | 3 => panic!("blub"),
253         _ => println!("bye"),
254     }
255     match 99 {
256         0 => println!("hi"),
257         1 => println!("bla"),
258         2 | 3 => panic!("blub"),
259         _ => println!("bye"),
260     }
261 }
262 
263 #[clippy::cognitive_complexity = "1"]
cake()264 fn cake() {
265     if 4 == 5 {
266         println!("yea");
267     } else {
268         panic!("meh");
269     }
270     println!("whee");
271 }
272 
273 #[clippy::cognitive_complexity = "1"]
read_file(input_path: &str) -> String274 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::cognitive_complexity = "1"]
void(void: Void)305 fn void(void: Void) {
306     if true {
307         match void {}
308     }
309 }
310 
311 #[clippy::cognitive_complexity = "1"]
mcarton_sees_all()312 fn mcarton_sees_all() {
313     panic!("meh");
314     panic!("möh");
315 }
316 
317 #[clippy::cognitive_complexity = "1"]
try_() -> Result<i32, &'static str>318 fn try_() -> Result<i32, &'static str> {
319     match 5 {
320         5 => Ok(5),
321         _ => return Err("bla"),
322     }
323 }
324 
325 #[clippy::cognitive_complexity = "1"]
try_again() -> Result<i32, &'static str>326 fn try_again() -> Result<i32, &'static str> {
327     let _ = Ok(42)?;
328     let _ = Ok(43)?;
329     let _ = Ok(44)?;
330     let _ = Ok(45)?;
331     let _ = Ok(46)?;
332     let _ = Ok(47)?;
333     let _ = Ok(48)?;
334     let _ = Ok(49)?;
335     match 5 {
336         5 => Ok(5),
337         _ => return Err("bla"),
338     }
339 }
340 
341 #[clippy::cognitive_complexity = "1"]
early() -> Result<i32, &'static str>342 fn early() -> Result<i32, &'static str> {
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     return Ok(5);
351     return Ok(5);
352 }
353 
354 #[rustfmt::skip]
355 #[clippy::cognitive_complexity = "1"]
early_ret() -> i32356 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 }
374 
375 #[clippy::cognitive_complexity = "1"]
closures()376 fn closures() {
377     let x = |a: i32, b: i32| -> i32 {
378         if true {
379             println!("moo");
380         }
381 
382         a + b
383     };
384 }
385 
386 struct Moo;
387 
388 #[clippy::cognitive_complexity = "1"]
389 impl Moo {
moo(&self)390     fn moo(&self) {
391         if true {
392             println!("moo");
393         }
394     }
395 }
396