1- //! calculate cyclomatic complexity and warn about overly complex functions
1+ //! calculate cognitive complexity and warn about overly complex functions
22
33use rustc:: cfg:: CFG ;
44use rustc:: hir:: intravisit:: { walk_expr, NestedVisitorMap , Visitor } ;
@@ -12,43 +12,43 @@ use syntax::source_map::Span;
1212use crate :: utils:: { in_macro, is_allowed, match_type, paths, span_help_and_lint, LimitStack } ;
1313
1414declare_clippy_lint ! {
15- /// **What it does:** Checks for methods with high cyclomatic complexity.
15+ /// **What it does:** Checks for methods with high cognitive complexity.
1616 ///
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.
1919 ///
2020 /// **Known problems:** Sometimes it's hard to find a way to reduce the
2121 /// complexity.
2222 ///
2323 /// **Example:** No. You'll see it when you get the warning.
24- pub CYCLOMATIC_COMPLEXITY ,
24+ pub COGNITIVE_COMPLEXITY ,
2525 complexity,
2626 "functions that should be split up into multiple functions"
2727}
2828
29- pub struct CyclomaticComplexity {
29+ pub struct CognitiveComplexity {
3030 limit : LimitStack ,
3131}
3232
33- impl CyclomaticComplexity {
33+ impl CognitiveComplexity {
3434 pub fn new ( limit : u64 ) -> Self {
3535 Self {
3636 limit : LimitStack :: new ( limit) ,
3737 }
3838 }
3939}
4040
41- impl LintPass for CyclomaticComplexity {
41+ impl LintPass for CognitiveComplexity {
4242 fn get_lints ( & self ) -> LintArray {
43- lint_array ! ( CYCLOMATIC_COMPLEXITY )
43+ lint_array ! ( COGNITIVE_COMPLEXITY )
4444 }
4545
4646 fn name ( & self ) -> & ' static str {
47- "CyclomaticComplexity "
47+ "CognitiveComplexity "
4848 }
4949}
5050
51- impl CyclomaticComplexity {
51+ impl CognitiveComplexity {
5252 fn check < ' a , ' tcx : ' a > ( & mut self , cx : & ' a LateContext < ' a , ' tcx > , body : & ' tcx Body , span : Span ) {
5353 if in_macro ( span) {
5454 return ;
@@ -105,17 +105,17 @@ impl CyclomaticComplexity {
105105 if rust_cc > self . limit . limit ( ) {
106106 span_help_and_lint (
107107 cx,
108- CYCLOMATIC_COMPLEXITY ,
108+ COGNITIVE_COMPLEXITY ,
109109 span,
110- & format ! ( "the function has a cyclomatic complexity of {}" , rust_cc) ,
110+ & format ! ( "the function has a cognitive complexity of {}" , rust_cc) ,
111111 "you could split it up into multiple smaller functions" ,
112112 ) ;
113113 }
114114 }
115115 }
116116}
117117
118- impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for CyclomaticComplexity {
118+ impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for CognitiveComplexity {
119119 fn check_fn (
120120 & mut self ,
121121 cx : & LateContext < ' a , ' tcx > ,
@@ -132,10 +132,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CyclomaticComplexity {
132132 }
133133
134134 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 " ) ;
136136 }
137137 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 " ) ;
139139 }
140140}
141141
@@ -201,7 +201,7 @@ fn report_cc_bug(
201201) {
202202 span_bug ! (
203203 span,
204- "Clippy encountered a bug calculating cyclomatic complexity: cc = {}, arms = {}, \
204+ "Clippy encountered a bug calculating cognitive complexity: cc = {}, arms = {}, \
205205 div = {}, shorts = {}, returns = {}. Please file a bug report.",
206206 cc,
207207 narms,
@@ -222,12 +222,12 @@ fn report_cc_bug(
222222 span : Span ,
223223 id : HirId ,
224224) {
225- if !is_allowed ( cx, CYCLOMATIC_COMPLEXITY , id) {
225+ if !is_allowed ( cx, COGNITIVE_COMPLEXITY , id) {
226226 cx. sess ( ) . span_note_without_error (
227227 span,
228228 & 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 )]`): \
231231 cc = {}, arms = {}, div = {}, shorts = {}, returns = {}. \
232232 Please file a bug report.",
233233 cc, narms, div, shorts, returns
0 commit comments