Skip to content

Commit a2477f7

Browse files
committed
Remove use of the HIR CFG
1 parent f30bf69 commit a2477f7

File tree

3 files changed

+75
-240
lines changed

3 files changed

+75
-240
lines changed
Lines changed: 25 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
//! calculate cognitive complexity and warn about overly complex functions
22
3-
use rustc::cfg::CFG;
43
use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
54
use rustc::hir::*;
65
use rustc::lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass};
7-
use rustc::ty;
86
use rustc::{declare_tool_lint, impl_lint_pass};
97
use syntax::ast::Attribute;
108
use syntax::source_map::Span;
119

12-
use crate::utils::{is_allowed, match_type, paths, span_help_and_lint, LimitStack};
10+
use crate::utils::{match_type, paths, span_help_and_lint, LimitStack};
1311

1412
declare_clippy_lint! {
1513
/// **What it does:** Checks for methods with high cognitive complexity.
@@ -46,30 +44,11 @@ impl CognitiveComplexity {
4644
return;
4745
}
4846

49-
let cfg = CFG::new(cx.tcx, body);
5047
let expr = &body.value;
51-
let n = cfg.graph.len_nodes() as u64;
52-
let e = cfg.graph.len_edges() as u64;
53-
if e + 2 < n {
54-
// the function has unreachable code, other lints should catch this
55-
return;
56-
}
57-
let cc = e + 2 - n;
58-
let mut helper = CCHelper {
59-
match_arms: 0,
60-
divergence: 0,
61-
short_circuits: 0,
62-
returns: 0,
63-
cx,
64-
};
48+
49+
let mut helper = CCHelper { cc: 1, returns: 0 };
6550
helper.visit_expr(expr);
66-
let CCHelper {
67-
match_arms,
68-
divergence,
69-
short_circuits,
70-
returns,
71-
..
72-
} = helper;
51+
let CCHelper { cc, returns } = helper;
7352
let ret_ty = cx.tables.node_type(expr.hir_id);
7453
let ret_adjust = if match_type(cx, ret_ty, &paths::RESULT) {
7554
returns
@@ -78,36 +57,23 @@ impl CognitiveComplexity {
7857
(returns / 2)
7958
};
8059

81-
if cc + divergence < match_arms + short_circuits {
82-
report_cc_bug(
60+
let mut rust_cc = cc;
61+
// prevent degenerate cases where unreachable code contains `return` statements
62+
if rust_cc >= ret_adjust {
63+
rust_cc -= ret_adjust;
64+
}
65+
if rust_cc > self.limit.limit() {
66+
span_help_and_lint(
8367
cx,
84-
cc,
85-
match_arms,
86-
divergence,
87-
short_circuits,
88-
ret_adjust,
68+
COGNITIVE_COMPLEXITY,
8969
span,
90-
body.id().hir_id,
70+
&format!(
71+
"the function has a cognitive complexity of ({}/{})",
72+
rust_cc,
73+
self.limit.limit()
74+
),
75+
"you could split it up into multiple smaller functions",
9176
);
92-
} else {
93-
let mut rust_cc = cc + divergence - match_arms - short_circuits;
94-
// prevent degenerate cases where unreachable code contains `return` statements
95-
if rust_cc >= ret_adjust {
96-
rust_cc -= ret_adjust;
97-
}
98-
if rust_cc > self.limit.limit() {
99-
span_help_and_lint(
100-
cx,
101-
COGNITIVE_COMPLEXITY,
102-
span,
103-
&format!(
104-
"the function has a cognitive complexity of ({}/{})",
105-
rust_cc,
106-
self.limit.limit()
107-
),
108-
"you could split it up into multiple smaller functions",
109-
);
110-
}
11177
}
11278
}
11379
}
@@ -136,99 +102,27 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CognitiveComplexity {
136102
}
137103
}
138104

139-
struct CCHelper<'a, 'tcx> {
140-
match_arms: u64,
141-
divergence: u64,
105+
struct CCHelper {
106+
cc: u64,
142107
returns: u64,
143-
short_circuits: u64, // && and ||
144-
cx: &'a LateContext<'a, 'tcx>,
145108
}
146109

147-
impl<'a, 'tcx> Visitor<'tcx> for CCHelper<'a, 'tcx> {
110+
impl<'tcx> Visitor<'tcx> for CCHelper {
148111
fn visit_expr(&mut self, e: &'tcx Expr) {
112+
walk_expr(self, e);
149113
match e.node {
150114
ExprKind::Match(_, ref arms, _) => {
151-
walk_expr(self, e);
152115
let arms_n: u64 = arms.iter().map(|arm| arm.pats.len() as u64).sum();
153116
if arms_n > 1 {
154-
self.match_arms += arms_n - 2;
155-
}
156-
},
157-
ExprKind::Call(ref callee, _) => {
158-
walk_expr(self, e);
159-
let ty = self.cx.tables.node_type(callee.hir_id);
160-
match ty.sty {
161-
ty::FnDef(..) | ty::FnPtr(_) => {
162-
let sig = ty.fn_sig(self.cx.tcx);
163-
if sig.skip_binder().output().sty == ty::Never {
164-
self.divergence += 1;
165-
}
166-
},
167-
_ => (),
168-
}
169-
},
170-
ExprKind::Closure(.., _) => (),
171-
ExprKind::Binary(op, _, _) => {
172-
walk_expr(self, e);
173-
match op.node {
174-
BinOpKind::And | BinOpKind::Or => self.short_circuits += 1,
175-
_ => (),
117+
self.cc += 1;
176118
}
119+
self.cc += arms.iter().filter(|arm| arm.guard.is_some()).count() as u64;
177120
},
178121
ExprKind::Ret(_) => self.returns += 1,
179-
_ => walk_expr(self, e),
122+
_ => {},
180123
}
181124
}
182125
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
183126
NestedVisitorMap::None
184127
}
185128
}
186-
187-
#[cfg(feature = "debugging")]
188-
#[allow(clippy::too_many_arguments)]
189-
fn report_cc_bug(
190-
_: &LateContext<'_, '_>,
191-
cc: u64,
192-
narms: u64,
193-
div: u64,
194-
shorts: u64,
195-
returns: u64,
196-
span: Span,
197-
_: HirId,
198-
) {
199-
span_bug!(
200-
span,
201-
"Clippy encountered a bug calculating cognitive complexity: cc = {}, arms = {}, \
202-
div = {}, shorts = {}, returns = {}. Please file a bug report.",
203-
cc,
204-
narms,
205-
div,
206-
shorts,
207-
returns
208-
);
209-
}
210-
#[cfg(not(feature = "debugging"))]
211-
#[allow(clippy::too_many_arguments)]
212-
fn report_cc_bug(
213-
cx: &LateContext<'_, '_>,
214-
cc: u64,
215-
narms: u64,
216-
div: u64,
217-
shorts: u64,
218-
returns: u64,
219-
span: Span,
220-
id: HirId,
221-
) {
222-
if !is_allowed(cx, COGNITIVE_COMPLEXITY, id) {
223-
cx.sess().span_note_without_error(
224-
span,
225-
&format!(
226-
"Clippy encountered a bug calculating cognitive complexity \
227-
(hide this message with `#[allow(cognitive_complexity)]`): \
228-
cc = {}, arms = {}, div = {}, shorts = {}, returns = {}. \
229-
Please file a bug report.",
230-
cc, narms, div, shorts, returns
231-
),
232-
);
233-
}
234-
}

tests/ui/cognitive_complexity.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ fn main() {
8787
}
8888
}
8989

90-
#[clippy::cognitive_complexity = "0"]
90+
#[clippy::cognitive_complexity = "1"]
9191
fn kaboom() {
9292
let n = 0;
9393
'a: for i in 0..20 {
@@ -133,17 +133,19 @@ fn bloo() {
133133
}
134134
}
135135

136-
#[clippy::cognitive_complexity = "0"]
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"]
137139
fn lots_of_short_circuits() -> bool {
138140
true && false && true && false && true && false && true
139141
}
140142

141-
#[clippy::cognitive_complexity = "0"]
143+
#[clippy::cognitive_complexity = "1"]
142144
fn lots_of_short_circuits2() -> bool {
143145
true || false || true || false || true || false || true
144146
}
145147

146-
#[clippy::cognitive_complexity = "0"]
148+
#[clippy::cognitive_complexity = "1"]
147149
fn baa() {
148150
let x = || match 99 {
149151
0 => 0,
@@ -161,7 +163,7 @@ fn baa() {
161163
}
162164
}
163165

164-
#[clippy::cognitive_complexity = "0"]
166+
#[clippy::cognitive_complexity = "1"]
165167
fn bar() {
166168
match 99 {
167169
0 => println!("hi"),
@@ -170,7 +172,7 @@ fn bar() {
170172
}
171173

172174
#[test]
173-
#[clippy::cognitive_complexity = "0"]
175+
#[clippy::cognitive_complexity = "1"]
174176
/// Tests are usually complex but simple at the same time. `clippy::cognitive_complexity` used to
175177
/// give lots of false-positives in tests.
176178
fn dont_warn_on_tests() {
@@ -180,7 +182,7 @@ fn dont_warn_on_tests() {
180182
}
181183
}
182184

183-
#[clippy::cognitive_complexity = "0"]
185+
#[clippy::cognitive_complexity = "1"]
184186
fn barr() {
185187
match 99 {
186188
0 => println!("hi"),
@@ -190,7 +192,7 @@ fn barr() {
190192
}
191193
}
192194

193-
#[clippy::cognitive_complexity = "0"]
195+
#[clippy::cognitive_complexity = "1"]
194196
fn barr2() {
195197
match 99 {
196198
0 => println!("hi"),
@@ -206,7 +208,7 @@ fn barr2() {
206208
}
207209
}
208210

209-
#[clippy::cognitive_complexity = "0"]
211+
#[clippy::cognitive_complexity = "1"]
210212
fn barrr() {
211213
match 99 {
212214
0 => println!("hi"),
@@ -216,7 +218,7 @@ fn barrr() {
216218
}
217219
}
218220

219-
#[clippy::cognitive_complexity = "0"]
221+
#[clippy::cognitive_complexity = "1"]
220222
fn barrr2() {
221223
match 99 {
222224
0 => println!("hi"),
@@ -232,7 +234,7 @@ fn barrr2() {
232234
}
233235
}
234236

235-
#[clippy::cognitive_complexity = "0"]
237+
#[clippy::cognitive_complexity = "1"]
236238
fn barrrr() {
237239
match 99 {
238240
0 => println!("hi"),
@@ -242,7 +244,7 @@ fn barrrr() {
242244
}
243245
}
244246

245-
#[clippy::cognitive_complexity = "0"]
247+
#[clippy::cognitive_complexity = "1"]
246248
fn barrrr2() {
247249
match 99 {
248250
0 => println!("hi"),
@@ -258,7 +260,7 @@ fn barrrr2() {
258260
}
259261
}
260262

261-
#[clippy::cognitive_complexity = "0"]
263+
#[clippy::cognitive_complexity = "1"]
262264
fn cake() {
263265
if 4 == 5 {
264266
println!("yea");
@@ -268,7 +270,7 @@ fn cake() {
268270
println!("whee");
269271
}
270272

271-
#[clippy::cognitive_complexity = "0"]
273+
#[clippy::cognitive_complexity = "1"]
272274
pub fn read_file(input_path: &str) -> String {
273275
use std::fs::File;
274276
use std::io::{Read, Write};
@@ -299,28 +301,28 @@ pub fn read_file(input_path: &str) -> String {
299301

300302
enum Void {}
301303

302-
#[clippy::cognitive_complexity = "0"]
304+
#[clippy::cognitive_complexity = "1"]
303305
fn void(void: Void) {
304306
if true {
305307
match void {}
306308
}
307309
}
308310

309-
#[clippy::cognitive_complexity = "0"]
311+
#[clippy::cognitive_complexity = "1"]
310312
fn mcarton_sees_all() {
311313
panic!("meh");
312314
panic!("möh");
313315
}
314316

315-
#[clippy::cognitive_complexity = "0"]
317+
#[clippy::cognitive_complexity = "1"]
316318
fn try_() -> Result<i32, &'static str> {
317319
match 5 {
318320
5 => Ok(5),
319321
_ => return Err("bla"),
320322
}
321323
}
322324

323-
#[clippy::cognitive_complexity = "0"]
325+
#[clippy::cognitive_complexity = "1"]
324326
fn try_again() -> Result<i32, &'static str> {
325327
let _ = Ok(42)?;
326328
let _ = Ok(43)?;
@@ -336,7 +338,7 @@ fn try_again() -> Result<i32, &'static str> {
336338
}
337339
}
338340

339-
#[clippy::cognitive_complexity = "0"]
341+
#[clippy::cognitive_complexity = "1"]
340342
fn early() -> Result<i32, &'static str> {
341343
return Ok(5);
342344
return Ok(5);
@@ -350,7 +352,7 @@ fn early() -> Result<i32, &'static str> {
350352
}
351353

352354
#[rustfmt::skip]
353-
#[clippy::cognitive_complexity = "0"]
355+
#[clippy::cognitive_complexity = "1"]
354356
fn early_ret() -> i32 {
355357
let a = if true { 42 } else { return 0; };
356358
let a = if a < 99 { 42 } else { return 0; };

0 commit comments

Comments
 (0)