Skip to content

Cognitive Complexity PR, re-gitted #4516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
617 changes: 441 additions & 176 deletions clippy_lints/src/cognitive_complexity.rs

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
reg.register_late_lint_pass(box no_effect::NoEffect);
reg.register_late_lint_pass(box temporary_assignment::TemporaryAssignment);
reg.register_late_lint_pass(box transmute::Transmute);
reg.register_late_lint_pass(
reg.register_early_lint_pass(
box cognitive_complexity::CognitiveComplexity::new(conf.cognitive_complexity_threshold)
);
reg.register_late_lint_pass(box escape::BoxedLocal{too_large_for_stack: conf.too_large_for_stack});
Expand Down Expand Up @@ -703,7 +703,6 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
booleans::LOGIC_BUG,
booleans::NONMINIMAL_BOOL,
bytecount::NAIVE_BYTECOUNT,
cognitive_complexity::COGNITIVE_COMPLEXITY,
collapsible_if::COLLAPSIBLE_IF,
copies::IFS_SAME_COND,
copies::IF_SAME_THEN_ELSE,
Expand Down Expand Up @@ -1012,7 +1011,6 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con
assign_ops::MISREFACTORED_ASSIGN_OP,
attrs::DEPRECATED_CFG_ATTR,
booleans::NONMINIMAL_BOOL,
cognitive_complexity::COGNITIVE_COMPLEXITY,
double_comparison::DOUBLE_COMPARISONS,
double_parens::DOUBLE_PARENS,
duration_subsec::DURATION_SUBSEC,
Expand Down Expand Up @@ -1172,6 +1170,7 @@ pub fn register_plugins(reg: &mut rustc_driver::plugin::Registry<'_>, conf: &Con

reg.register_lint_group("clippy::nursery", Some("clippy_nursery"), vec![
attrs::EMPTY_LINE_AFTER_OUTER_ATTR,
cognitive_complexity::COGNITIVE_COMPLEXITY,
fallible_impl_from::FALLIBLE_IMPL_FROM,
missing_const_for_fn::MISSING_CONST_FOR_FN,
mutex_atomic::MUTEX_INTEGER,
Expand Down
2 changes: 1 addition & 1 deletion src/lintlist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ pub const ALL_LINTS: [Lint; 313] = [
},
Lint {
name: "cognitive_complexity",
group: "complexity",
group: "nursery",
desc: "functions that should be split up into multiple functions",
deprecation: None,
module: "cognitive_complexity",
Expand Down
80 changes: 45 additions & 35 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/25)
error: the function has a cognitive complexity of 1350
--> $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/0)
error: the function has a cognitive complexity of 180
--> $DIR/cognitive_complexity.rs:91:1
|
LL | / fn kaboom() {
Expand All @@ -27,7 +27,21 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of (1/0)
error: the function has a cognitive complexity of 770
--> $DIR/cognitive_complexity.rs:112:1
|
LL | / fn bloo() {
LL | | match 42 {
LL | | 0 => println!("hi"),
LL | | 1 => println!("hai"),
... |
LL | | }
LL | | }
| |_^
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of 10
--> $DIR/cognitive_complexity.rs:137:1
|
LL | / fn lots_of_short_circuits() -> bool {
Expand All @@ -37,7 +51,7 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of (1/0)
error: the function has a cognitive complexity of 10
--> $DIR/cognitive_complexity.rs:142:1
|
LL | / fn lots_of_short_circuits2() -> bool {
Expand All @@ -47,7 +61,7 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

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

error: the function has a cognitive complexity of (2/0)
--> $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/0)
error: the function has a cognitive complexity of 90
--> $DIR/cognitive_complexity.rs:165:1
|
LL | / fn bar() {
Expand All @@ -89,7 +88,7 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

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

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

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

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

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

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

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

error: the function has a cognitive complexity of (4/0)
error: the function has a cognitive complexity of 270
--> $DIR/cognitive_complexity.rs:272:1
|
LL | / pub fn read_file(input_path: &str) -> String {
Expand All @@ -201,7 +200,7 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of (1/0)
error: the function has a cognitive complexity of 20
--> $DIR/cognitive_complexity.rs:303:1
|
LL | / fn void(void: Void) {
Expand All @@ -213,7 +212,18 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of (1/0)
error: the function has a cognitive complexity of 20
--> $DIR/cognitive_complexity.rs:310: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 30
--> $DIR/cognitive_complexity.rs:316:1
|
LL | / fn try_() -> Result<i32, &'static str> {
Expand All @@ -226,7 +236,7 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of (1/0)
error: the function has a cognitive complexity of 110
--> $DIR/cognitive_complexity.rs:324:1
|
LL | / fn try_again() -> Result<i32, &'static str> {
Expand All @@ -240,7 +250,7 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of (1/0)
error: the function has a cognitive complexity of 90
--> $DIR/cognitive_complexity.rs:340:1
|
LL | / fn early() -> Result<i32, &'static str> {
Expand All @@ -254,7 +264,7 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: the function has a cognitive complexity of (8/0)
error: the function has a cognitive complexity of 130
--> $DIR/cognitive_complexity.rs:354:1
|
LL | / fn early_ret() -> i32 {
Expand All @@ -268,5 +278,5 @@ LL | | }
|
= help: you could split it up into multiple smaller functions

error: aborting due to 20 previous errors
error: aborting due to 21 previous errors

Loading