Skip to content

Commit b22341f

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. * `compile-test.rs` now allows `clippy::cognitive_complexity` in ui tests by default. This is because most ui tests don't use functions in a standard manner, and tend to have rather large ones. * Tests for CoC have been updated.
1 parent fbb3a47 commit b22341f

25 files changed

+1117
-423
lines changed

clippy_lints/src/cognitive_complexity.rs

+316-173
Large diffs are not rendered by default.

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);

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

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);

tests/compile-test.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,6 @@ fn config(mode: &str, dir: PathBuf) -> compiletest::Config {
6767
config
6868
}
6969

70-
fn run_mode(mode: &str, dir: PathBuf) {
71-
let cfg = config(mode, dir);
72-
// clean rmeta data, otherwise "cargo check; cargo test" fails (#2896)
73-
cfg.clean_rmeta();
74-
compiletest::run_tests(&cfg);
75-
}
76-
7770
#[allow(clippy::identity_conversion)]
7871
fn run_ui_toml_tests(config: &compiletest::Config, mut tests: Vec<test::TestDescAndFn>) -> Result<bool, io::Error> {
7972
let mut result = true;
@@ -131,9 +124,28 @@ fn prepare_env() {
131124
//set_var("RUST_BACKTRACE", "0");
132125
}
133126

127+
fn run_ui_tests() {
128+
let mut cfg = config("ui", "tests/ui".into());
129+
// clean rmeta data, otherwise "cargo check; cargo test" fails (#2896)
130+
cfg.clean_rmeta();
131+
132+
let default_flags = {
133+
if let Some(flags) = cfg.target_rustcflags {
134+
flags
135+
} else {
136+
"".to_string()
137+
}
138+
};
139+
let new_flags = format!("{} -A clippy::cognitive_complexity", default_flags);
140+
cfg.target_rustcflags = Some(new_flags);
141+
142+
compiletest::run_tests(&cfg);
143+
}
144+
134145
#[test]
135146
fn compile_test() {
136147
prepare_env();
137-
run_mode("ui", "tests/ui".into());
148+
run_ui_tests();
149+
// run_mode("ui", "tests/ui".into());
138150
run_ui_toml();
139151
}

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+
}

0 commit comments

Comments
 (0)