Skip to content

Commit ddc7180

Browse files
committed
Renamed: Cyclomatic Complexity -> Cognitive Complexity
* Ran automatic naming update * Formalized rename of `cyclomatic_complexity` to `cognitive_complexity` ** Added the rename to `lib.rs` ** Added rename test * Added warning for deprecated key `cyclomatic_complexity_threshold` and tests for it * Added deprecation status for Clippy's builtin attribute * Updated tests for new builtin attribute renaming
1 parent 15d1731 commit ddc7180

29 files changed

+175
-120
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -798,11 +798,11 @@ All notable changes to this project will be documented in this file.
798798
[`cmp_nan`]: https://rust-lang.github.io/rust-clippy/master/index.html#cmp_nan
799799
[`cmp_null`]: https://rust-lang.github.io/rust-clippy/master/index.html#cmp_null
800800
[`cmp_owned`]: https://rust-lang.github.io/rust-clippy/master/index.html#cmp_owned
801+
[`cognitive_complexity`]: https://rust-lang.github.io/rust-clippy/master/index.html#cognitive_complexity
801802
[`collapsible_if`]: https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_if
802803
[`const_static_lifetime`]: https://rust-lang.github.io/rust-clippy/master/index.html#const_static_lifetime
803804
[`copy_iterator`]: https://rust-lang.github.io/rust-clippy/master/index.html#copy_iterator
804805
[`crosspointer_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#crosspointer_transmute
805-
[`cyclomatic_complexity`]: https://rust-lang.github.io/rust-clippy/master/index.html#cyclomatic_complexity
806806
[`dbg_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#dbg_macro
807807
[`decimal_literal_representation`]: https://rust-lang.github.io/rust-clippy/master/index.html#decimal_literal_representation
808808
[`declare_interior_mutable_const`]: https://rust-lang.github.io/rust-clippy/master/index.html#declare_interior_mutable_const

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ Some lints can be configured in a TOML file named `clippy.toml` or `.clippy.toml
131131

132132
```toml
133133
blacklisted-names = ["toto", "tata", "titi"]
134-
cyclomatic-complexity-threshold = 30
134+
cognitive-complexity-threshold = 30
135135
```
136136

137137
See the [list of lints](https://rust-lang.github.io/rust-clippy/master/index.html) for more information about which lints can be configured and the

clippy_lints/src/assign_ops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
119119
},
120120
hir::ExprKind::Assign(assignee, e) => {
121121
if let hir::ExprKind::Binary(op, l, r) = &e.node {
122-
#[allow(clippy::cyclomatic_complexity)]
122+
#[allow(clippy::cognitive_complexity)]
123123
let lint = |assignee: &hir::Expr, rhs: &hir::Expr| {
124124
let ty = cx.tables.expr_ty(assignee);
125125
let rty = cx.tables.expr_ty(rhs);

clippy_lints/src/cyclomatic_complexity.rs renamed to clippy_lints/src/cognitive_complexity.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! calculate cyclomatic complexity and warn about overly complex functions
1+
//! calculate cognitive complexity and warn about overly complex functions
22
33
use rustc::cfg::CFG;
44
use rustc::hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
@@ -12,43 +12,43 @@ use syntax::source_map::Span;
1212
use crate::utils::{in_macro, is_allowed, match_type, paths, span_help_and_lint, LimitStack};
1313

1414
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.
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

clippy_lints/src/lib.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ pub mod block_in_if_condition;
152152
pub mod booleans;
153153
pub mod bytecount;
154154
pub mod cargo_common_metadata;
155+
pub mod cognitive_complexity;
155156
pub mod collapsible_if;
156157
pub mod const_static_lifetime;
157158
pub mod copies;
158159
pub mod copy_iterator;
159-
pub mod cyclomatic_complexity;
160160
pub mod dbg_macro;
161161
pub mod default_trait_access;
162162
pub mod derive;
@@ -478,7 +478,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
478478
reg.register_late_lint_pass(box temporary_assignment::Pass);
479479
reg.register_late_lint_pass(box transmute::Transmute);
480480
reg.register_late_lint_pass(
481-
box cyclomatic_complexity::CyclomaticComplexity::new(conf.cyclomatic_complexity_threshold)
481+
box cognitive_complexity::CognitiveComplexity::new(conf.cognitive_complexity_threshold)
482482
);
483483
reg.register_late_lint_pass(box escape::Pass{too_large_for_stack: conf.too_large_for_stack});
484484
reg.register_early_lint_pass(box misc_early::MiscEarly);
@@ -666,11 +666,11 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
666666
booleans::LOGIC_BUG,
667667
booleans::NONMINIMAL_BOOL,
668668
bytecount::NAIVE_BYTECOUNT,
669+
cognitive_complexity::COGNITIVE_COMPLEXITY,
669670
collapsible_if::COLLAPSIBLE_IF,
670671
const_static_lifetime::CONST_STATIC_LIFETIME,
671672
copies::IFS_SAME_COND,
672673
copies::IF_SAME_THEN_ELSE,
673-
cyclomatic_complexity::CYCLOMATIC_COMPLEXITY,
674674
derive::DERIVE_HASH_XOR_EQ,
675675
double_comparison::DOUBLE_COMPARISONS,
676676
double_parens::DOUBLE_PARENS,
@@ -962,7 +962,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
962962
assign_ops::MISREFACTORED_ASSIGN_OP,
963963
attrs::DEPRECATED_CFG_ATTR,
964964
booleans::NONMINIMAL_BOOL,
965-
cyclomatic_complexity::CYCLOMATIC_COMPLEXITY,
965+
cognitive_complexity::COGNITIVE_COMPLEXITY,
966966
double_comparison::DOUBLE_COMPARISONS,
967967
double_parens::DOUBLE_PARENS,
968968
duration_subsec::DURATION_SUBSEC,
@@ -1131,6 +1131,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
11311131
pub fn register_renamed(ls: &mut rustc::lint::LintStore) {
11321132
ls.register_renamed("clippy::stutter", "clippy::module_name_repetitions");
11331133
ls.register_renamed("clippy::new_without_default_derive", "clippy::new_without_default");
1134+
ls.register_renamed("clippy::cyclomatic_complexity", "clippy::cognitive_complexity");
11341135
}
11351136

11361137
// only exists to let the dogfood integration test works.

clippy_lints/src/methods/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ impl LintPass for Pass {
821821
}
822822

823823
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
824-
#[allow(clippy::cyclomatic_complexity)]
824+
#[allow(clippy::cognitive_complexity)]
825825
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
826826
if in_macro(expr.span) {
827827
return;

clippy_lints/src/utils/attrs.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ pub enum DeprecationStatus {
1515

1616
pub const BUILTIN_ATTRIBUTES: &[(&str, DeprecationStatus)] = &[
1717
("author", DeprecationStatus::None),
18-
("cyclomatic_complexity", DeprecationStatus::None),
18+
("cognitive_complexity", DeprecationStatus::None),
19+
(
20+
"cyclomatic_complexity",
21+
DeprecationStatus::Replaced("cognitive_complexity"),
22+
),
1923
("dump", DeprecationStatus::None),
2024
];
2125

clippy_lints/src/utils/conf.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ macro_rules! define_Conf {
110110
define_Conf! {
111111
/// Lint: BLACKLISTED_NAME. The list of blacklisted names to lint about
112112
(blacklisted_names, "blacklisted_names", ["foo", "bar", "baz", "quux"] => Vec<String>),
113-
/// Lint: CYCLOMATIC_COMPLEXITY. The maximum cyclomatic complexity a function can have
114-
(cyclomatic_complexity_threshold, "cyclomatic_complexity_threshold", 25 => u64),
113+
/// Lint: COGNITIVE_COMPLEXITY. The maximum cognitive complexity a function can have
114+
(cognitive_complexity_threshold, "cognitive_complexity_threshold", 25 => u64),
115+
/// DEPRECATED LINT: CYCLOMATIC_COMPLEXITY. Use the Cognitive Complexity lint instead.
116+
(cyclomatic_complexity_threshold, "cyclomatic_complexity_threshold", None => Option<u64>),
115117
/// Lint: DOC_MARKDOWN. The list of words this lint should not consider as identifiers needing ticks
116118
(doc_valid_idents, "doc_valid_idents", [
117119
"KiB", "MiB", "GiB", "TiB", "PiB", "EiB",
@@ -227,13 +229,24 @@ pub fn read(path: Option<&path::Path>) -> (Conf, Vec<Error>) {
227229

228230
assert!(ERRORS.lock().expect("no threading -> mutex always safe").is_empty());
229231
match toml::from_str(&file) {
230-
Ok(toml) => (
231-
toml,
232-
ERRORS.lock().expect("no threading -> mutex always safe").split_off(0),
233-
),
232+
Ok(toml) => {
233+
let mut errors = ERRORS.lock().expect("no threading -> mutex always safe").split_off(0);
234+
235+
let toml_ref: &Conf = &toml;
236+
237+
let cyc_field: Option<u64> = toml_ref.cyclomatic_complexity_threshold;
238+
239+
if cyc_field.is_some() {
240+
let cyc_err = "found deprecated field `cyclomatic-complexity-threshold`. Please use `cognitive-complexity-threshold` instead.".to_string();
241+
errors.push(Error::Toml(cyc_err));
242+
}
243+
244+
(toml, errors)
245+
},
234246
Err(e) => {
235247
let mut errors = ERRORS.lock().expect("no threading -> mutex always safe").split_off(0);
236248
errors.push(Error::Toml(e.to_string()));
249+
237250
default(errors)
238251
},
239252
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# that one is an error
2+
cyclomatic-complexity-threshold = 42
3+
4+
# that one is white-listed
5+
[third-party]
6+
clippy-feature = "nightly"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// error-pattern: error reading Clippy's configuration file: found deprecated field
2+
// `cyclomatic-complexity-threshold`. Please use `cognitive-complexity-threshold` instead.
3+
4+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
error: error reading Clippy's configuration file `$DIR/clippy.toml`: found deprecated field `cyclomatic-complexity-threshold`. Please use `cognitive-complexity-threshold` instead.
2+
3+
error: aborting due to previous error
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# that one is white-listed
2+
[third-party]
3+
clippy-feature = "nightly"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// error-pattern: should give absolutely no error
2+
3+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `blacklisted-names`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `too-many-lines-threshold`, `third-party`
1+
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `too-many-lines-threshold`, `third-party`
22

33
error: aborting due to previous error
44

0 commit comments

Comments
 (0)