1
- //! calculate cyclomatic complexity and warn about overly complex functions
1
+ //! calculate cognitive complexity and warn about overly complex functions
2
2
3
3
use rustc:: cfg:: CFG ;
4
4
use rustc:: hir:: intravisit:: { walk_expr, NestedVisitorMap , Visitor } ;
@@ -12,43 +12,43 @@ use syntax::source_map::Span;
12
12
use crate :: utils:: { in_macro, is_allowed, match_type, paths, span_help_and_lint, LimitStack } ;
13
13
14
14
declare_clippy_lint ! {
15
- /// **What it does:** Checks for methods with high cyclomatic complexity.
15
+ /// **What it does:** Checks for methods with high cognitive complexity.
16
16
///
17
- /// **Why is this bad?** Methods of high cyclomatic complexity tend to be badly
18
- /// readable . Also LLVM will usually optimize small methods better.
17
+ /// **Why is this bad?** Methods of high cognitive complexity tend to be hard to
18
+ /// both read and maintain . Also LLVM will tend to optimize small methods better.
19
19
///
20
20
/// **Known problems:** Sometimes it's hard to find a way to reduce the
21
21
/// complexity.
22
22
///
23
23
/// **Example:** No. You'll see it when you get the warning.
24
- pub CYCLOMATIC_COMPLEXITY ,
24
+ pub COGNITIVE_COMPLEXITY ,
25
25
complexity,
26
26
"functions that should be split up into multiple functions"
27
27
}
28
28
29
- pub struct CyclomaticComplexity {
29
+ pub struct CognitiveComplexity {
30
30
limit : LimitStack ,
31
31
}
32
32
33
- impl CyclomaticComplexity {
33
+ impl CognitiveComplexity {
34
34
pub fn new ( limit : u64 ) -> Self {
35
35
Self {
36
36
limit : LimitStack :: new ( limit) ,
37
37
}
38
38
}
39
39
}
40
40
41
- impl LintPass for CyclomaticComplexity {
41
+ impl LintPass for CognitiveComplexity {
42
42
fn get_lints ( & self ) -> LintArray {
43
- lint_array ! ( CYCLOMATIC_COMPLEXITY )
43
+ lint_array ! ( COGNITIVE_COMPLEXITY )
44
44
}
45
45
46
46
fn name ( & self ) -> & ' static str {
47
- "CyclomaticComplexity "
47
+ "CognitiveComplexity "
48
48
}
49
49
}
50
50
51
- impl CyclomaticComplexity {
51
+ impl CognitiveComplexity {
52
52
fn check < ' a , ' tcx : ' a > ( & mut self , cx : & ' a LateContext < ' a , ' tcx > , body : & ' tcx Body , span : Span ) {
53
53
if in_macro ( span) {
54
54
return ;
@@ -105,17 +105,17 @@ impl CyclomaticComplexity {
105
105
if rust_cc > self . limit . limit ( ) {
106
106
span_help_and_lint (
107
107
cx,
108
- CYCLOMATIC_COMPLEXITY ,
108
+ COGNITIVE_COMPLEXITY ,
109
109
span,
110
- & format ! ( "the function has a cyclomatic complexity of {}" , rust_cc) ,
110
+ & format ! ( "the function has a cognitive complexity of {}" , rust_cc) ,
111
111
"you could split it up into multiple smaller functions" ,
112
112
) ;
113
113
}
114
114
}
115
115
}
116
116
}
117
117
118
- impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for CyclomaticComplexity {
118
+ impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for CognitiveComplexity {
119
119
fn check_fn (
120
120
& mut self ,
121
121
cx : & LateContext < ' a , ' tcx > ,
@@ -132,10 +132,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CyclomaticComplexity {
132
132
}
133
133
134
134
fn enter_lint_attrs ( & mut self , cx : & LateContext < ' a , ' tcx > , attrs : & ' tcx [ Attribute ] ) {
135
- self . limit . push_attrs ( cx. sess ( ) , attrs, "cyclomatic_complexity " ) ;
135
+ self . limit . push_attrs ( cx. sess ( ) , attrs, "cognitive_complexity " ) ;
136
136
}
137
137
fn exit_lint_attrs ( & mut self , cx : & LateContext < ' a , ' tcx > , attrs : & ' tcx [ Attribute ] ) {
138
- self . limit . pop_attrs ( cx. sess ( ) , attrs, "cyclomatic_complexity " ) ;
138
+ self . limit . pop_attrs ( cx. sess ( ) , attrs, "cognitive_complexity " ) ;
139
139
}
140
140
}
141
141
@@ -201,7 +201,7 @@ fn report_cc_bug(
201
201
) {
202
202
span_bug ! (
203
203
span,
204
- "Clippy encountered a bug calculating cyclomatic complexity: cc = {}, arms = {}, \
204
+ "Clippy encountered a bug calculating cognitive complexity: cc = {}, arms = {}, \
205
205
div = {}, shorts = {}, returns = {}. Please file a bug report.",
206
206
cc,
207
207
narms,
@@ -222,12 +222,12 @@ fn report_cc_bug(
222
222
span : Span ,
223
223
id : HirId ,
224
224
) {
225
- if !is_allowed ( cx, CYCLOMATIC_COMPLEXITY , id) {
225
+ if !is_allowed ( cx, COGNITIVE_COMPLEXITY , id) {
226
226
cx. sess ( ) . span_note_without_error (
227
227
span,
228
228
& format ! (
229
- "Clippy encountered a bug calculating cyclomatic complexity \
230
- (hide this message with `#[allow(cyclomatic_complexity )]`): \
229
+ "Clippy encountered a bug calculating cognitive complexity \
230
+ (hide this message with `#[allow(cognitive_complexity )]`): \
231
231
cc = {}, arms = {}, div = {}, shorts = {}, returns = {}. \
232
232
Please file a bug report.",
233
233
cc, narms, div, shorts, returns
0 commit comments