2018-07-28 10:34:52 -05:00
|
|
|
#![allow(clippy::all)]
|
2019-02-22 19:19:50 -06:00
|
|
|
#![warn(clippy::cognitive_complexity)]
|
2020-05-28 08:45:24 -05:00
|
|
|
#![allow(unused, unused_crate_dependencies)]
|
2015-11-18 05:35:18 -06:00
|
|
|
|
2018-12-08 11:56:59 -06:00
|
|
|
#[rustfmt::skip]
|
2017-02-08 07:58:07 -06:00
|
|
|
fn main() {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: the function has a cognitive complexity of (28/25)
|
2015-11-18 05:35:18 -06:00
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
if true {
|
|
|
|
println!("a");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 05:39:42 -05:00
|
|
|
#[clippy::cognitive_complexity = "1"]
|
2017-02-08 07:58:07 -06:00
|
|
|
fn kaboom() {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: the function has a cognitive complexity of (7/1)
|
2015-11-18 05:35:18 -06:00
|
|
|
let n = 0;
|
|
|
|
'a: for i in 0..20 {
|
|
|
|
'b: for j in i..20 {
|
|
|
|
for k in j..20 {
|
|
|
|
if k == 5 {
|
|
|
|
break 'b;
|
|
|
|
}
|
|
|
|
if j == 3 && k == 6 {
|
|
|
|
continue 'a;
|
|
|
|
}
|
|
|
|
if k == j {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
println!("bake");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println!("cake");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bloo() {
|
|
|
|
match 42 {
|
|
|
|
0 => println!("hi"),
|
|
|
|
1 => println!("hai"),
|
|
|
|
2 => println!("hey"),
|
|
|
|
3 => println!("hallo"),
|
|
|
|
4 => println!("hello"),
|
|
|
|
5 => println!("salut"),
|
|
|
|
6 => println!("good morning"),
|
|
|
|
7 => println!("good evening"),
|
|
|
|
8 => println!("good afternoon"),
|
|
|
|
9 => println!("good night"),
|
|
|
|
10 => println!("bonjour"),
|
|
|
|
11 => println!("hej"),
|
|
|
|
12 => println!("hej hej"),
|
|
|
|
13 => println!("greetings earthling"),
|
|
|
|
14 => println!("take us to you leader"),
|
|
|
|
15 | 17 | 19 | 21 | 23 | 25 | 27 | 29 | 31 | 33 => println!("take us to you leader"),
|
|
|
|
35 | 37 | 39 | 41 | 43 | 45 | 47 | 49 | 51 | 53 => println!("there is no undefined behavior"),
|
|
|
|
55 | 57 | 59 | 61 | 63 | 65 | 67 | 69 | 71 | 73 => println!("I know borrow-fu"),
|
|
|
|
_ => println!("bye"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 05:39:42 -05:00
|
|
|
// Short circuiting operations don't increase the complexity of a function.
|
|
|
|
// Note that the minimum complexity of a function is 1.
|
|
|
|
#[clippy::cognitive_complexity = "1"]
|
2017-02-08 07:58:07 -06:00
|
|
|
fn lots_of_short_circuits() -> bool {
|
2016-03-08 08:10:02 -06:00
|
|
|
true && false && true && false && true && false && true
|
|
|
|
}
|
|
|
|
|
2019-09-08 05:39:42 -05:00
|
|
|
#[clippy::cognitive_complexity = "1"]
|
2017-02-08 07:58:07 -06:00
|
|
|
fn lots_of_short_circuits2() -> bool {
|
2016-03-08 08:10:02 -06:00
|
|
|
true || false || true || false || true || false || true
|
|
|
|
}
|
|
|
|
|
2019-09-08 05:39:42 -05:00
|
|
|
#[clippy::cognitive_complexity = "1"]
|
2017-02-08 07:58:07 -06:00
|
|
|
fn baa() {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: the function has a cognitive complexity of (2/1)
|
2017-02-08 07:58:07 -06:00
|
|
|
let x = || match 99 {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: the function has a cognitive complexity of (2/1)
|
2016-02-09 17:38:53 -06:00
|
|
|
0 => 0,
|
|
|
|
1 => 1,
|
|
|
|
2 => 2,
|
|
|
|
4 => 4,
|
|
|
|
6 => 6,
|
|
|
|
9 => 9,
|
|
|
|
_ => 42,
|
2015-11-18 05:35:18 -06:00
|
|
|
};
|
2016-02-09 17:38:53 -06:00
|
|
|
if x() == 42 {
|
2015-11-18 05:35:18 -06:00
|
|
|
println!("x");
|
|
|
|
} else {
|
|
|
|
println!("not x");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 05:39:42 -05:00
|
|
|
#[clippy::cognitive_complexity = "1"]
|
2017-02-08 07:58:07 -06:00
|
|
|
fn bar() {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: the function has a cognitive complexity of (2/1)
|
2015-11-18 05:35:18 -06:00
|
|
|
match 99 {
|
|
|
|
0 => println!("hi"),
|
|
|
|
_ => println!("bye"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-05 10:29:14 -05:00
|
|
|
#[test]
|
2019-09-08 05:39:42 -05:00
|
|
|
#[clippy::cognitive_complexity = "1"]
|
2019-02-22 19:19:50 -06:00
|
|
|
/// Tests are usually complex but simple at the same time. `clippy::cognitive_complexity` used to
|
2018-12-09 16:26:16 -06:00
|
|
|
/// give lots of false-positives in tests.
|
2016-04-05 10:29:14 -05:00
|
|
|
fn dont_warn_on_tests() {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: the function has a cognitive complexity of (2/1)
|
2016-04-05 10:29:14 -05:00
|
|
|
match 99 {
|
|
|
|
0 => println!("hi"),
|
|
|
|
_ => println!("bye"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 05:39:42 -05:00
|
|
|
#[clippy::cognitive_complexity = "1"]
|
2017-02-08 07:58:07 -06:00
|
|
|
fn barr() {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: the function has a cognitive complexity of (2/1)
|
2015-11-18 05:35:18 -06:00
|
|
|
match 99 {
|
|
|
|
0 => println!("hi"),
|
|
|
|
1 => println!("bla"),
|
|
|
|
2 | 3 => println!("blub"),
|
|
|
|
_ => println!("bye"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 05:39:42 -05:00
|
|
|
#[clippy::cognitive_complexity = "1"]
|
2017-02-08 07:58:07 -06:00
|
|
|
fn barr2() {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: the function has a cognitive complexity of (3/1)
|
2015-12-14 07:29:20 -06:00
|
|
|
match 99 {
|
|
|
|
0 => println!("hi"),
|
|
|
|
1 => println!("bla"),
|
|
|
|
2 | 3 => println!("blub"),
|
|
|
|
_ => println!("bye"),
|
|
|
|
}
|
|
|
|
match 99 {
|
|
|
|
0 => println!("hi"),
|
|
|
|
1 => println!("bla"),
|
|
|
|
2 | 3 => println!("blub"),
|
|
|
|
_ => println!("bye"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 05:39:42 -05:00
|
|
|
#[clippy::cognitive_complexity = "1"]
|
2017-02-08 07:58:07 -06:00
|
|
|
fn barrr() {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: the function has a cognitive complexity of (2/1)
|
2015-12-14 07:29:20 -06:00
|
|
|
match 99 {
|
|
|
|
0 => println!("hi"),
|
|
|
|
1 => panic!("bla"),
|
|
|
|
2 | 3 => println!("blub"),
|
|
|
|
_ => println!("bye"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 05:39:42 -05:00
|
|
|
#[clippy::cognitive_complexity = "1"]
|
2017-02-08 07:58:07 -06:00
|
|
|
fn barrr2() {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: the function has a cognitive complexity of (3/1)
|
2015-12-14 07:29:20 -06:00
|
|
|
match 99 {
|
|
|
|
0 => println!("hi"),
|
|
|
|
1 => panic!("bla"),
|
|
|
|
2 | 3 => println!("blub"),
|
|
|
|
_ => println!("bye"),
|
|
|
|
}
|
|
|
|
match 99 {
|
|
|
|
0 => println!("hi"),
|
|
|
|
1 => panic!("bla"),
|
|
|
|
2 | 3 => println!("blub"),
|
|
|
|
_ => println!("bye"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 05:39:42 -05:00
|
|
|
#[clippy::cognitive_complexity = "1"]
|
2017-02-08 07:58:07 -06:00
|
|
|
fn barrrr() {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: the function has a cognitive complexity of (2/1)
|
2015-12-14 07:29:20 -06:00
|
|
|
match 99 {
|
|
|
|
0 => println!("hi"),
|
|
|
|
1 => println!("bla"),
|
|
|
|
2 | 3 => panic!("blub"),
|
|
|
|
_ => println!("bye"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 05:39:42 -05:00
|
|
|
#[clippy::cognitive_complexity = "1"]
|
2017-02-08 07:58:07 -06:00
|
|
|
fn barrrr2() {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: the function has a cognitive complexity of (3/1)
|
2015-12-14 07:29:20 -06:00
|
|
|
match 99 {
|
|
|
|
0 => println!("hi"),
|
|
|
|
1 => println!("bla"),
|
|
|
|
2 | 3 => panic!("blub"),
|
|
|
|
_ => println!("bye"),
|
|
|
|
}
|
|
|
|
match 99 {
|
|
|
|
0 => println!("hi"),
|
|
|
|
1 => println!("bla"),
|
|
|
|
2 | 3 => panic!("blub"),
|
|
|
|
_ => println!("bye"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 05:39:42 -05:00
|
|
|
#[clippy::cognitive_complexity = "1"]
|
2017-02-08 07:58:07 -06:00
|
|
|
fn cake() {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: the function has a cognitive complexity of (2/1)
|
2015-12-14 07:29:20 -06:00
|
|
|
if 4 == 5 {
|
|
|
|
println!("yea");
|
|
|
|
} else {
|
|
|
|
panic!("meh");
|
|
|
|
}
|
|
|
|
println!("whee");
|
|
|
|
}
|
|
|
|
|
2019-09-08 05:39:42 -05:00
|
|
|
#[clippy::cognitive_complexity = "1"]
|
2017-02-08 07:58:07 -06:00
|
|
|
pub fn read_file(input_path: &str) -> String {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: the function has a cognitive complexity of (4/1)
|
2015-12-14 07:29:20 -06:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{Read, Write};
|
|
|
|
use std::path::Path;
|
|
|
|
let mut file = match File::open(&Path::new(input_path)) {
|
|
|
|
Ok(f) => f,
|
|
|
|
Err(err) => {
|
|
|
|
panic!("Can't open {}: {}", input_path, err);
|
2018-12-09 16:26:16 -06:00
|
|
|
},
|
2015-12-14 07:29:20 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut bytes = Vec::new();
|
|
|
|
|
|
|
|
match file.read_to_end(&mut bytes) {
|
|
|
|
Ok(..) => {},
|
|
|
|
Err(_) => {
|
|
|
|
panic!("Can't read {}", input_path);
|
2018-12-09 16:26:16 -06:00
|
|
|
},
|
2015-12-14 07:29:20 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
match String::from_utf8(bytes) {
|
|
|
|
Ok(contents) => contents,
|
|
|
|
Err(_) => {
|
|
|
|
panic!("{} is not UTF-8 encoded", input_path);
|
2018-12-09 16:26:16 -06:00
|
|
|
},
|
2015-12-14 07:29:20 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-18 05:35:18 -06:00
|
|
|
enum Void {}
|
|
|
|
|
2019-09-08 05:39:42 -05:00
|
|
|
#[clippy::cognitive_complexity = "1"]
|
2017-02-08 07:58:07 -06:00
|
|
|
fn void(void: Void) {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: the function has a cognitive complexity of (2/1)
|
2015-11-18 05:35:18 -06:00
|
|
|
if true {
|
2018-12-09 16:26:16 -06:00
|
|
|
match void {}
|
2015-11-18 05:35:18 -06:00
|
|
|
}
|
|
|
|
}
|
2016-03-14 11:24:55 -05:00
|
|
|
|
2019-09-08 05:39:42 -05:00
|
|
|
#[clippy::cognitive_complexity = "1"]
|
2016-03-14 11:24:55 -05:00
|
|
|
fn mcarton_sees_all() {
|
|
|
|
panic!("meh");
|
|
|
|
panic!("möh");
|
|
|
|
}
|
2016-04-23 07:30:05 -05:00
|
|
|
|
2019-09-08 05:39:42 -05:00
|
|
|
#[clippy::cognitive_complexity = "1"]
|
2019-07-22 14:59:09 -05:00
|
|
|
fn try_() -> Result<i32, &'static str> {
|
2016-04-23 07:30:05 -05:00
|
|
|
match 5 {
|
|
|
|
5 => Ok(5),
|
|
|
|
_ => return Err("bla"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 05:39:42 -05:00
|
|
|
#[clippy::cognitive_complexity = "1"]
|
2017-02-08 07:58:07 -06:00
|
|
|
fn try_again() -> Result<i32, &'static str> {
|
2019-08-15 03:04:47 -05:00
|
|
|
let _ = Ok(42)?;
|
|
|
|
let _ = Ok(43)?;
|
|
|
|
let _ = Ok(44)?;
|
|
|
|
let _ = Ok(45)?;
|
|
|
|
let _ = Ok(46)?;
|
|
|
|
let _ = Ok(47)?;
|
|
|
|
let _ = Ok(48)?;
|
|
|
|
let _ = Ok(49)?;
|
2016-04-23 07:30:05 -05:00
|
|
|
match 5 {
|
|
|
|
5 => Ok(5),
|
|
|
|
_ => return Err("bla"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 05:39:42 -05:00
|
|
|
#[clippy::cognitive_complexity = "1"]
|
2017-02-08 07:58:07 -06:00
|
|
|
fn early() -> Result<i32, &'static str> {
|
2016-04-23 07:30:05 -05:00
|
|
|
return Ok(5);
|
|
|
|
return Ok(5);
|
|
|
|
return Ok(5);
|
|
|
|
return Ok(5);
|
|
|
|
return Ok(5);
|
|
|
|
return Ok(5);
|
|
|
|
return Ok(5);
|
|
|
|
return Ok(5);
|
|
|
|
return Ok(5);
|
|
|
|
}
|
|
|
|
|
2018-12-08 11:56:59 -06:00
|
|
|
#[rustfmt::skip]
|
2019-09-08 05:39:42 -05:00
|
|
|
#[clippy::cognitive_complexity = "1"]
|
2017-02-08 07:58:07 -06:00
|
|
|
fn early_ret() -> i32 {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: the function has a cognitive complexity of (8/1)
|
2016-04-23 07:30:05 -05:00
|
|
|
let a = if true { 42 } else { return 0; };
|
|
|
|
let a = if a < 99 { 42 } else { return 0; };
|
|
|
|
let a = if a < 99 { 42 } else { return 0; };
|
|
|
|
let a = if a < 99 { 42 } else { return 0; };
|
|
|
|
let a = if a < 99 { 42 } else { return 0; };
|
|
|
|
let a = if a < 99 { 42 } else { return 0; };
|
|
|
|
let a = if a < 99 { 42 } else { return 0; };
|
|
|
|
let a = if a < 99 { 42 } else { return 0; };
|
|
|
|
let a = if a < 99 { 42 } else { return 0; };
|
|
|
|
let a = if a < 99 { 42 } else { return 0; };
|
|
|
|
let a = if a < 99 { 42 } else { return 0; };
|
|
|
|
let a = if a < 99 { 42 } else { return 0; };
|
|
|
|
match 5 {
|
|
|
|
5 => 5,
|
|
|
|
_ => return 6,
|
|
|
|
}
|
|
|
|
}
|
2019-12-21 20:07:53 -06:00
|
|
|
|
|
|
|
#[clippy::cognitive_complexity = "1"]
|
|
|
|
fn closures() {
|
|
|
|
let x = |a: i32, b: i32| -> i32 {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: the function has a cognitive complexity of (2/1)
|
2019-12-21 20:07:53 -06:00
|
|
|
if true {
|
|
|
|
println!("moo");
|
|
|
|
}
|
|
|
|
|
|
|
|
a + b
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Moo;
|
|
|
|
|
|
|
|
#[clippy::cognitive_complexity = "1"]
|
|
|
|
impl Moo {
|
|
|
|
fn moo(&self) {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: the function has a cognitive complexity of (2/1)
|
2019-12-21 20:07:53 -06:00
|
|
|
if true {
|
|
|
|
println!("moo");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-21 13:34:47 -06:00
|
|
|
|
|
|
|
#[clippy::cognitive_complexity = "1"]
|
|
|
|
mod issue9300 {
|
|
|
|
async fn a() {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: the function has a cognitive complexity of (2/1)
|
2022-11-21 13:34:47 -06:00
|
|
|
let a = 0;
|
|
|
|
if a == 0 {}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct S;
|
|
|
|
impl S {
|
|
|
|
pub async fn async_method() {
|
2023-08-24 14:32:12 -05:00
|
|
|
//~^ ERROR: the function has a cognitive complexity of (2/1)
|
2022-11-21 13:34:47 -06:00
|
|
|
let a = 0;
|
|
|
|
if a == 0 {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|