Skip to content

Commit

Permalink
Changed Cognitive Complexity as per issue 3793
Browse files Browse the repository at this point in the history
* CoC is now a pre-expansion Early pass.
* The full draft of issue 3793 has been implemented.
* Affected tests have been updated.
* Tests for CoC have been updated.
  • Loading branch information
felix91gr committed Apr 15, 2019
1 parent fbb3a47 commit 84a4778
Show file tree
Hide file tree
Showing 29 changed files with 535 additions and 428 deletions.
414 changes: 243 additions & 171 deletions clippy_lints/src/cognitive_complexity.rs

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ pub fn register_pre_expansion_lints(
);
store.register_pre_expansion_pass(Some(session), true, false, box attrs::CfgAttrPass);
store.register_pre_expansion_pass(Some(session), true, false, box dbg_macro::Pass);

store.register_pre_expansion_pass(
Some(session),
true,
false,
box cognitive_complexity::CognitiveComplexity::new(conf.cognitive_complexity_threshold),
)
}

#[doc(hidden)]
Expand Down Expand Up @@ -370,6 +377,7 @@ pub fn read_conf(reg: &rustc_plugin::Registry<'_>) -> Conf {
///
/// Used in `./src/driver.rs`.
#[allow(clippy::too_many_lines)]
#[allow(clippy::cognitive_complexity)]
#[rustfmt::skip]
pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
let mut store = reg.sess.lint_store.borrow_mut();
Expand Down Expand Up @@ -478,9 +486,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
reg.register_late_lint_pass(box no_effect::Pass);
reg.register_late_lint_pass(box temporary_assignment::Pass);
reg.register_late_lint_pass(box transmute::Transmute);
reg.register_late_lint_pass(
box cognitive_complexity::CognitiveComplexity::new(conf.cognitive_complexity_threshold)
);
reg.register_late_lint_pass(box escape::Pass{too_large_for_stack: conf.too_large_for_stack});
reg.register_early_lint_pass(box misc_early::MiscEarly);
reg.register_late_lint_pass(box panic_unimplemented::Pass);
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/utils/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ define_Conf! {
/// Lint: BLACKLISTED_NAME. The list of blacklisted names to lint about
(blacklisted_names, "blacklisted_names", ["foo", "bar", "baz", "quux"] => Vec<String>),
/// Lint: COGNITIVE_COMPLEXITY. The maximum cognitive complexity a function can have
(cognitive_complexity_threshold, "cognitive_complexity_threshold", 25 => u64),
(cognitive_complexity_threshold, "cognitive_complexity_threshold", 50 => u64),
/// DEPRECATED LINT: CYCLOMATIC_COMPLEXITY. Use the Cognitive Complexity lint instead.
(cyclomatic_complexity_threshold, "cyclomatic_complexity_threshold", None => Option<u64>),
/// Lint: DOC_MARKDOWN. The list of words this lint should not consider as identifiers needing ticks
Expand Down
2 changes: 2 additions & 0 deletions clippy_lints/src/utils/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ fn has_attr(sess: &Session, attrs: &[Attribute]) -> bool {
get_attr(sess, attrs, "dump").count() > 0
}

#[allow(clippy::cognitive_complexity)]
#[allow(clippy::similar_names)]
fn print_expr(cx: &LateContext<'_, '_>, expr: &hir::Expr, indent: usize) {
let ind = " ".repeat(indent);
Expand Down Expand Up @@ -415,6 +416,7 @@ fn print_item(cx: &LateContext<'_, '_>, item: &hir::Item) {
}
}

#[allow(clippy::cognitive_complexity)]
#[allow(clippy::similar_names)]
fn print_pat(cx: &LateContext<'_, '_>, pat: &hir::Pat, indent: usize) {
let ind = " ".repeat(indent);
Expand Down
27 changes: 17 additions & 10 deletions tests/ui/cognitive_complexity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,6 @@ fn bloo() {
}
}

#[clippy::cognitive_complexity = "0"]
fn lots_of_short_circuits() -> bool {
true && false && true && false && true && false && true
}

#[clippy::cognitive_complexity = "0"]
fn lots_of_short_circuits2() -> bool {
true || false || true || false || true || false || true
}

#[clippy::cognitive_complexity = "0"]
fn baa() {
let x = || match 99 {
Expand Down Expand Up @@ -369,3 +359,20 @@ fn early_ret() -> i32 {
_ => return 6,
}
}

#[clippy::cognitive_complexity = "0"]
fn osscilating_logical_chain_1() -> bool {
true && false || true && false
}

// This tests that the only thing that matters
// is the change in operator
#[clippy::cognitive_complexity = "0"]
fn osscilating_logical_chain_2() -> bool {
true && false && true && false || true && false && true && false
}

#[clippy::cognitive_complexity = "0"]
fn osscilating_logical_chain_3() -> bool {
(true && false) || (true && false)
}
146 changes: 69 additions & 77 deletions tests/ui/cognitive_complexity.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: the function has a cognitive complexity of 28
error: the function has a cognitive complexity of 54
--> $DIR/cognitive_complexity.rs:6:1
|
LL | / fn main() {
Expand All @@ -13,7 +13,7 @@ LL | | }
= note: `-D clippy::cognitive-complexity` implied by `-D warnings`
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 7
error: the function has a cognitive complexity of 11
--> $DIR/cognitive_complexity.rs:91:1
|
LL | / fn kaboom() {
Expand All @@ -27,29 +27,9 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 1
error: the function has a cognitive complexity of 5
--> $DIR/cognitive_complexity.rs:137:1
|
LL | / fn lots_of_short_circuits() -> bool {
LL | | true && false && true && false && true && false && true
LL | | }
| |_^
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 1
--> $DIR/cognitive_complexity.rs:142:1
|
LL | / fn lots_of_short_circuits2() -> bool {
LL | | true || false || true || false || true || false || true
LL | | }
| |_^
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 2
--> $DIR/cognitive_complexity.rs:147:1
|
LL | / fn baa() {
LL | | let x = || match 99 {
LL | | 0 => 0,
Expand All @@ -61,23 +41,8 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 2
--> $DIR/cognitive_complexity.rs:148:13
|
LL | let x = || match 99 {
| _____________^
LL | | 0 => 0,
LL | | 1 => 1,
LL | | 2 => 2,
... |
LL | | _ => 42,
LL | | };
| |_____^
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 2
--> $DIR/cognitive_complexity.rs:165:1
error: the function has a cognitive complexity of 3
--> $DIR/cognitive_complexity.rs:155:1
|
LL | / fn bar() {
LL | | match 99 {
Expand All @@ -89,8 +54,8 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 2
--> $DIR/cognitive_complexity.rs:184:1
error: the function has a cognitive complexity of 5
--> $DIR/cognitive_complexity.rs:174:1
|
LL | / fn barr() {
LL | | match 99 {
Expand All @@ -103,8 +68,8 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 3
--> $DIR/cognitive_complexity.rs:194:1
error: the function has a cognitive complexity of 10
--> $DIR/cognitive_complexity.rs:184:1
|
LL | / fn barr2() {
LL | | match 99 {
Expand All @@ -117,8 +82,8 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 2
--> $DIR/cognitive_complexity.rs:210:1
error: the function has a cognitive complexity of 5
--> $DIR/cognitive_complexity.rs:200:1
|
LL | / fn barrr() {
LL | | match 99 {
Expand All @@ -131,8 +96,8 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 3
--> $DIR/cognitive_complexity.rs:220:1
error: the function has a cognitive complexity of 10
--> $DIR/cognitive_complexity.rs:210:1
|
LL | / fn barrr2() {
LL | | match 99 {
Expand All @@ -145,8 +110,8 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 2
--> $DIR/cognitive_complexity.rs:236:1
error: the function has a cognitive complexity of 5
--> $DIR/cognitive_complexity.rs:226:1
|
LL | / fn barrrr() {
LL | | match 99 {
Expand All @@ -159,8 +124,8 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 3
--> $DIR/cognitive_complexity.rs:246:1
error: the function has a cognitive complexity of 10
--> $DIR/cognitive_complexity.rs:236:1
|
LL | / fn barrrr2() {
LL | | match 99 {
Expand All @@ -173,8 +138,8 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 2
--> $DIR/cognitive_complexity.rs:262:1
error: the function has a cognitive complexity of 4
--> $DIR/cognitive_complexity.rs:252:1
|
LL | / fn cake() {
LL | | if 4 == 5 {
Expand All @@ -187,8 +152,8 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 4
--> $DIR/cognitive_complexity.rs:272:1
error: the function has a cognitive complexity of 10
--> $DIR/cognitive_complexity.rs:262:1
|
LL | / pub fn read_file(input_path: &str) -> String {
LL | | use std::fs::File;
Expand All @@ -201,8 +166,8 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 1
--> $DIR/cognitive_complexity.rs:303:1
error: the function has a cognitive complexity of 2
--> $DIR/cognitive_complexity.rs:293:1
|
LL | / fn void(void: Void) {
LL | | if true {
Expand All @@ -213,8 +178,19 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 1
--> $DIR/cognitive_complexity.rs:316:1
error: the function has a cognitive complexity of 2
--> $DIR/cognitive_complexity.rs:300:1
|
LL | / fn mcarton_sees_all() {
LL | | panic!("meh");
LL | | panic!("möh");
LL | | }
| |_^
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 2
--> $DIR/cognitive_complexity.rs:306:1
|
LL | / fn try() -> Result<i32, &'static str> {
LL | | match 5 {
Expand All @@ -226,8 +202,8 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 1
--> $DIR/cognitive_complexity.rs:324:1
error: the function has a cognitive complexity of 10
--> $DIR/cognitive_complexity.rs:314:1
|
LL | / fn try_again() -> Result<i32, &'static str> {
LL | | let _ = try!(Ok(42));
Expand All @@ -240,29 +216,45 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 1
--> $DIR/cognitive_complexity.rs:340:1
error: the function has a cognitive complexity of 13
--> $DIR/cognitive_complexity.rs:344:1
|
LL | / fn early() -> Result<i32, &'static str> {
LL | | return Ok(5);
LL | | return Ok(5);
LL | | return Ok(5);
LL | / fn early_ret() -> i32 {
LL | | let a = if true { 42 } else { return 0; };
LL | | let a = if a < 99 { 42 } else { return 0; };
LL | | let a = if a < 99 { 42 } else { return 0; };
... |
LL | | return Ok(5);
LL | | }
LL | | }
| |_^
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 8
--> $DIR/cognitive_complexity.rs:354:1
error: the function has a cognitive complexity of 2
--> $DIR/cognitive_complexity.rs:364:1
|
LL | / fn early_ret() -> i32 {
LL | | let a = if true { 42 } else { return 0; };
LL | | let a = if a < 99 { 42 } else { return 0; };
LL | | let a = if a < 99 { 42 } else { return 0; };
... |
LL | | }
LL | / fn osscilating_logical_chain_1() -> bool {
LL | | true && false || true && false
LL | | }
| |_^
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 2
--> $DIR/cognitive_complexity.rs:371:1
|
LL | / fn osscilating_logical_chain_2() -> bool {
LL | | true && false && true && false || true && false && true && false
LL | | }
| |_^
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 2
--> $DIR/cognitive_complexity.rs:376:1
|
LL | / fn osscilating_logical_chain_3() -> bool {
LL | | (true && false) || (true && false)
LL | | }
| |_^
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/cognitive_complexity_attr_used.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: the function has a cognitive complexity of 3
error: the function has a cognitive complexity of 4
--> $DIR/cognitive_complexity_attr_used.rs:9:1
|
LL | / fn kaboom() {
Expand Down
5 changes: 3 additions & 2 deletions tests/ui/eta.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
clippy::many_single_char_names,
clippy::needless_pass_by_value,
clippy::option_map_unit_fn,
clippy::trivially_copy_pass_by_ref
)]
clippy::trivially_copy_pass_by_ref,
clippy::cognitive_complexity
)]
#![warn(clippy::redundant_closure, clippy::needless_borrow)]

use std::path::PathBuf;
Expand Down
5 changes: 3 additions & 2 deletions tests/ui/eta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
clippy::many_single_char_names,
clippy::needless_pass_by_value,
clippy::option_map_unit_fn,
clippy::trivially_copy_pass_by_ref
)]
clippy::trivially_copy_pass_by_ref,
clippy::cognitive_complexity
)]
#![warn(clippy::redundant_closure, clippy::needless_borrow)]

use std::path::PathBuf;
Expand Down
Loading

0 comments on commit 84a4778

Please sign in to comment.