Skip to content

Commit a74d060

Browse files
committed
Changed Cognitive Complexity as per issue 3793
* CoC is now a pre-expansion Early pass. * The full draft of issue 3793 has been implemented. * Affected tests have been updated. ** In particular, ui/eta.rs has been reordered s.t. it's easier to read. Its cognitive complexity doesn't seem high at the time of writing, so it was tagged as `allow(clippy::cognitive_complexity)` after being reordered. * Tests for CoC have been updated.
1 parent fbb3a47 commit a74d060

37 files changed

+1115
-480
lines changed

Diff for: clippy_lints/src/cognitive_complexity.rs

+314-171
Large diffs are not rendered by default.

Diff for: clippy_lints/src/lib.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,13 @@ pub fn register_pre_expansion_lints(
306306
);
307307
store.register_pre_expansion_pass(Some(session), true, false, box attrs::CfgAttrPass);
308308
store.register_pre_expansion_pass(Some(session), true, false, box dbg_macro::Pass);
309+
310+
store.register_pre_expansion_pass(
311+
Some(session),
312+
true,
313+
false,
314+
box cognitive_complexity::CognitiveComplexity::new(conf.cognitive_complexity_threshold),
315+
)
309316
}
310317

311318
#[doc(hidden)]
@@ -370,6 +377,7 @@ pub fn read_conf(reg: &rustc_plugin::Registry<'_>) -> Conf {
370377
///
371378
/// Used in `./src/driver.rs`.
372379
#[allow(clippy::too_many_lines)]
380+
#[allow(clippy::cognitive_complexity)]
373381
#[rustfmt::skip]
374382
pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
375383
let mut store = reg.sess.lint_store.borrow_mut();
@@ -478,9 +486,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
478486
reg.register_late_lint_pass(box no_effect::Pass);
479487
reg.register_late_lint_pass(box temporary_assignment::Pass);
480488
reg.register_late_lint_pass(box transmute::Transmute);
481-
reg.register_late_lint_pass(
482-
box cognitive_complexity::CognitiveComplexity::new(conf.cognitive_complexity_threshold)
483-
);
484489
reg.register_late_lint_pass(box escape::Pass{too_large_for_stack: conf.too_large_for_stack});
485490
reg.register_early_lint_pass(box misc_early::MiscEarly);
486491
reg.register_late_lint_pass(box panic_unimplemented::Pass);

Diff for: clippy_lints/src/utils/conf.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ define_Conf! {
109109
/// Lint: BLACKLISTED_NAME. The list of blacklisted names to lint about
110110
(blacklisted_names, "blacklisted_names", ["foo", "bar", "baz", "quux"] => Vec<String>),
111111
/// Lint: COGNITIVE_COMPLEXITY. The maximum cognitive complexity a function can have
112-
(cognitive_complexity_threshold, "cognitive_complexity_threshold", 25 => u64),
112+
(cognitive_complexity_threshold, "cognitive_complexity_threshold", 50 => u64),
113113
/// DEPRECATED LINT: CYCLOMATIC_COMPLEXITY. Use the Cognitive Complexity lint instead.
114114
(cyclomatic_complexity_threshold, "cyclomatic_complexity_threshold", None => Option<u64>),
115115
/// Lint: DOC_MARKDOWN. The list of words this lint should not consider as identifiers needing ticks

Diff for: clippy_lints/src/utils/inspector.rs

+2
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ fn has_attr(sess: &Session, attrs: &[Attribute]) -> bool {
153153
get_attr(sess, attrs, "dump").count() > 0
154154
}
155155

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

419+
#[allow(clippy::cognitive_complexity)]
418420
#[allow(clippy::similar_names)]
419421
fn print_pat(cx: &LateContext<'_, '_>, pat: &hir::Pat, indent: usize) {
420422
let ind = " ".repeat(indent);

Diff for: tests/ui/cognitive_complexity.rs

+29-10
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,6 @@ fn bloo() {
133133
}
134134
}
135135

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-
146136
#[clippy::cognitive_complexity = "0"]
147137
fn baa() {
148138
let x = || match 99 {
@@ -369,3 +359,32 @@ fn early_ret() -> i32 {
369359
_ => return 6,
370360
}
371361
}
362+
363+
#[clippy::cognitive_complexity = "0"]
364+
fn osscilating_logical_chain_1() -> bool {
365+
true && false || true && false
366+
}
367+
368+
// This tests that the only thing that matters
369+
// is the change in operator
370+
#[clippy::cognitive_complexity = "0"]
371+
fn osscilating_logical_chain_2() -> bool {
372+
true && false && true && false || true && false && true && false
373+
}
374+
375+
#[clippy::cognitive_complexity = "0"]
376+
fn osscilating_logical_chain_3() -> bool {
377+
(true && false) || (true && false)
378+
}
379+
380+
#[clippy::cognitive_complexity = "1"]
381+
fn nested_functions_are_counted(n: usize) -> usize {
382+
fn lucas_number(n: usize) -> usize {
383+
match n {
384+
0 => 2,
385+
1 => 1,
386+
_ => lucas_number(n - 1) + lucas_number(n - 2),
387+
}
388+
}
389+
lucas_number(n)
390+
}

Diff for: tests/ui/cognitive_complexity.stderr

+101-67
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: the function has a cognitive complexity of 28
1+
error: the function has a cognitive complexity of 54
22
--> $DIR/cognitive_complexity.rs:6:1
33
|
44
LL | / fn main() {
@@ -13,7 +13,7 @@ LL | | }
1313
= note: `-D clippy::cognitive-complexity` implied by `-D warnings`
1414
= help: you could split it up into multiple smaller functions
1515

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

30-
error: the function has a cognitive complexity of 1
30+
error: the function has a cognitive complexity of 5
3131
--> $DIR/cognitive_complexity.rs:137:1
3232
|
33-
LL | / fn lots_of_short_circuits() -> bool {
34-
LL | | true && false && true && false && true && false && true
35-
LL | | }
36-
| |_^
37-
|
38-
= help: you could split it up into multiple smaller functions
39-
40-
error: the function has a cognitive complexity of 1
41-
--> $DIR/cognitive_complexity.rs:142:1
42-
|
43-
LL | / fn lots_of_short_circuits2() -> bool {
44-
LL | | true || false || true || false || true || false || true
45-
LL | | }
46-
| |_^
47-
|
48-
= help: you could split it up into multiple smaller functions
49-
50-
error: the function has a cognitive complexity of 2
51-
--> $DIR/cognitive_complexity.rs:147:1
52-
|
5333
LL | / fn baa() {
5434
LL | | let x = || match 99 {
5535
LL | | 0 => 0,
@@ -61,23 +41,8 @@ LL | | }
6141
|
6242
= help: you could split it up into multiple smaller functions
6343

64-
error: the function has a cognitive complexity of 2
65-
--> $DIR/cognitive_complexity.rs:148:13
66-
|
67-
LL | let x = || match 99 {
68-
| _____________^
69-
LL | | 0 => 0,
70-
LL | | 1 => 1,
71-
LL | | 2 => 2,
72-
... |
73-
LL | | _ => 42,
74-
LL | | };
75-
| |_____^
76-
|
77-
= help: you could split it up into multiple smaller functions
78-
79-
error: the function has a cognitive complexity of 2
80-
--> $DIR/cognitive_complexity.rs:165:1
44+
error: the function has a cognitive complexity of 3
45+
--> $DIR/cognitive_complexity.rs:155:1
8146
|
8247
LL | / fn bar() {
8348
LL | | match 99 {
@@ -89,8 +54,8 @@ LL | | }
8954
|
9055
= help: you could split it up into multiple smaller functions
9156

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

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

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

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

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

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

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

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

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

216-
error: the function has a cognitive complexity of 1
217-
--> $DIR/cognitive_complexity.rs:316:1
181+
error: the function has a cognitive complexity of 2
182+
--> $DIR/cognitive_complexity.rs:300:1
183+
|
184+
LL | / fn mcarton_sees_all() {
185+
LL | | panic!("meh");
186+
LL | | panic!("möh");
187+
LL | | }
188+
| |_^
189+
|
190+
= help: you could split it up into multiple smaller functions
191+
192+
error: the function has a cognitive complexity of 3
193+
--> $DIR/cognitive_complexity.rs:306:1
218194
|
219195
LL | / fn try() -> Result<i32, &'static str> {
220196
LL | | match 5 {
@@ -226,8 +202,8 @@ LL | | }
226202
|
227203
= help: you could split it up into multiple smaller functions
228204

229-
error: the function has a cognitive complexity of 1
230-
--> $DIR/cognitive_complexity.rs:324:1
205+
error: the function has a cognitive complexity of 11
206+
--> $DIR/cognitive_complexity.rs:314:1
231207
|
232208
LL | / fn try_again() -> Result<i32, &'static str> {
233209
LL | | let _ = try!(Ok(42));
@@ -240,8 +216,8 @@ LL | | }
240216
|
241217
= help: you could split it up into multiple smaller functions
242218

243-
error: the function has a cognitive complexity of 1
244-
--> $DIR/cognitive_complexity.rs:340:1
219+
error: the function has a cognitive complexity of 9
220+
--> $DIR/cognitive_complexity.rs:330:1
245221
|
246222
LL | / fn early() -> Result<i32, &'static str> {
247223
LL | | return Ok(5);
@@ -254,8 +230,8 @@ LL | | }
254230
|
255231
= help: you could split it up into multiple smaller functions
256232

257-
error: the function has a cognitive complexity of 8
258-
--> $DIR/cognitive_complexity.rs:354:1
233+
error: the function has a cognitive complexity of 13
234+
--> $DIR/cognitive_complexity.rs:344:1
259235
|
260236
LL | / fn early_ret() -> i32 {
261237
LL | | let a = if true { 42 } else { return 0; };
@@ -268,5 +244,63 @@ LL | | }
268244
|
269245
= help: you could split it up into multiple smaller functions
270246

271-
error: aborting due to 20 previous errors
247+
error: the function has a cognitive complexity of 2
248+
--> $DIR/cognitive_complexity.rs:364:1
249+
|
250+
LL | / fn osscilating_logical_chain_1() -> bool {
251+
LL | | true && false || true && false
252+
LL | | }
253+
| |_^
254+
|
255+
= help: you could split it up into multiple smaller functions
256+
257+
error: the function has a cognitive complexity of 2
258+
--> $DIR/cognitive_complexity.rs:371:1
259+
|
260+
LL | / fn osscilating_logical_chain_2() -> bool {
261+
LL | | true && false && true && false || true && false && true && false
262+
LL | | }
263+
| |_^
264+
|
265+
= help: you could split it up into multiple smaller functions
266+
267+
error: the function has a cognitive complexity of 2
268+
--> $DIR/cognitive_complexity.rs:376:1
269+
|
270+
LL | / fn osscilating_logical_chain_3() -> bool {
271+
LL | | (true && false) || (true && false)
272+
LL | | }
273+
| |_^
274+
|
275+
= help: you could split it up into multiple smaller functions
276+
277+
error: the function has a cognitive complexity of 4
278+
--> $DIR/cognitive_complexity.rs:381:1
279+
|
280+
LL | / fn nested_functions_are_counted(n: usize) -> usize {
281+
LL | | fn lucas_number(n: usize) -> usize {
282+
LL | | match n {
283+
LL | | 0 => 2,
284+
... |
285+
LL | | lucas_number(n)
286+
LL | | }
287+
| |_^
288+
|
289+
= help: you could split it up into multiple smaller functions
290+
291+
error: the function has a cognitive complexity of 3
292+
--> $DIR/cognitive_complexity.rs:382:5
293+
|
294+
LL | / fn lucas_number(n: usize) -> usize {
295+
LL | | match n {
296+
LL | | 0 => 2,
297+
LL | | 1 => 1,
298+
LL | | _ => lucas_number(n - 1) + lucas_number(n - 2),
299+
LL | | }
300+
LL | | }
301+
| |_____^
302+
|
303+
= help: you could split it up into multiple smaller functions
304+
305+
error: aborting due to 23 previous errors
272306

0 commit comments

Comments
 (0)