Skip to content

Commit

Permalink
Changed Cognitive Complexity as per issue 3793
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
felix91gr committed Apr 18, 2019
1 parent fbb3a47 commit b22341f
Show file tree
Hide file tree
Showing 25 changed files with 1,117 additions and 423 deletions.
489 changes: 316 additions & 173 deletions clippy_lints/src/cognitive_complexity.rs

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ pub fn register_pre_expansion_lints(
);
store.register_pre_expansion_pass(Some(session), true, false, box attrs::CfgAttrPass);
store.register_pre_expansion_pass(Some(session), true, false, box dbg_macro::Pass);

store.register_pre_expansion_pass(
Some(session),
true,
false,
box cognitive_complexity::CognitiveComplexity::new(conf.cognitive_complexity_threshold),
)
}

#[doc(hidden)]
Expand Down Expand Up @@ -370,6 +377,7 @@ pub fn read_conf(reg: &rustc_plugin::Registry<'_>) -> Conf {
///
/// Used in `./src/driver.rs`.
#[allow(clippy::too_many_lines)]
#[allow(clippy::cognitive_complexity)]
#[rustfmt::skip]
pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
let mut store = reg.sess.lint_store.borrow_mut();
Expand Down Expand Up @@ -478,9 +486,6 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
reg.register_late_lint_pass(box no_effect::Pass);
reg.register_late_lint_pass(box temporary_assignment::Pass);
reg.register_late_lint_pass(box transmute::Transmute);
reg.register_late_lint_pass(
box cognitive_complexity::CognitiveComplexity::new(conf.cognitive_complexity_threshold)
);
reg.register_late_lint_pass(box escape::Pass{too_large_for_stack: conf.too_large_for_stack});
reg.register_early_lint_pass(box misc_early::MiscEarly);
reg.register_late_lint_pass(box panic_unimplemented::Pass);
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/utils/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ define_Conf! {
/// Lint: BLACKLISTED_NAME. The list of blacklisted names to lint about
(blacklisted_names, "blacklisted_names", ["foo", "bar", "baz", "quux"] => Vec<String>),
/// Lint: COGNITIVE_COMPLEXITY. The maximum cognitive complexity a function can have
(cognitive_complexity_threshold, "cognitive_complexity_threshold", 25 => u64),
(cognitive_complexity_threshold, "cognitive_complexity_threshold", 50 => u64),
/// DEPRECATED LINT: CYCLOMATIC_COMPLEXITY. Use the Cognitive Complexity lint instead.
(cyclomatic_complexity_threshold, "cyclomatic_complexity_threshold", None => Option<u64>),
/// Lint: DOC_MARKDOWN. The list of words this lint should not consider as identifiers needing ticks
Expand Down
2 changes: 2 additions & 0 deletions clippy_lints/src/utils/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ fn has_attr(sess: &Session, attrs: &[Attribute]) -> bool {
get_attr(sess, attrs, "dump").count() > 0
}

#[allow(clippy::cognitive_complexity)]
#[allow(clippy::similar_names)]
fn print_expr(cx: &LateContext<'_, '_>, expr: &hir::Expr, indent: usize) {
let ind = " ".repeat(indent);
Expand Down Expand Up @@ -415,6 +416,7 @@ fn print_item(cx: &LateContext<'_, '_>, item: &hir::Item) {
}
}

#[allow(clippy::cognitive_complexity)]
#[allow(clippy::similar_names)]
fn print_pat(cx: &LateContext<'_, '_>, pat: &hir::Pat, indent: usize) {
let ind = " ".repeat(indent);
Expand Down
28 changes: 20 additions & 8 deletions tests/compile-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,6 @@ fn config(mode: &str, dir: PathBuf) -> compiletest::Config {
config
}

fn run_mode(mode: &str, dir: PathBuf) {
let cfg = config(mode, dir);
// clean rmeta data, otherwise "cargo check; cargo test" fails (#2896)
cfg.clean_rmeta();
compiletest::run_tests(&cfg);
}

#[allow(clippy::identity_conversion)]
fn run_ui_toml_tests(config: &compiletest::Config, mut tests: Vec<test::TestDescAndFn>) -> Result<bool, io::Error> {
let mut result = true;
Expand Down Expand Up @@ -131,9 +124,28 @@ fn prepare_env() {
//set_var("RUST_BACKTRACE", "0");
}

fn run_ui_tests() {
let mut cfg = config("ui", "tests/ui".into());
// clean rmeta data, otherwise "cargo check; cargo test" fails (#2896)
cfg.clean_rmeta();

let default_flags = {
if let Some(flags) = cfg.target_rustcflags {
flags
} else {
"".to_string()
}
};
let new_flags = format!("{} -A clippy::cognitive_complexity", default_flags);
cfg.target_rustcflags = Some(new_flags);

compiletest::run_tests(&cfg);
}

#[test]
fn compile_test() {
prepare_env();
run_mode("ui", "tests/ui".into());
run_ui_tests();
// run_mode("ui", "tests/ui".into());
run_ui_toml();
}
39 changes: 29 additions & 10 deletions tests/ui/cognitive_complexity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,6 @@ fn bloo() {
}
}

#[clippy::cognitive_complexity = "0"]
fn lots_of_short_circuits() -> bool {
true && false && true && false && true && false && true
}

#[clippy::cognitive_complexity = "0"]
fn lots_of_short_circuits2() -> bool {
true || false || true || false || true || false || true
}

#[clippy::cognitive_complexity = "0"]
fn baa() {
let x = || match 99 {
Expand Down Expand Up @@ -369,3 +359,32 @@ fn early_ret() -> i32 {
_ => return 6,
}
}

#[clippy::cognitive_complexity = "0"]
fn osscilating_logical_chain_1() -> bool {
true && false || true && false
}

// This tests that the only thing that matters
// is the change in operator
#[clippy::cognitive_complexity = "0"]
fn osscilating_logical_chain_2() -> bool {
true && false && true && false || true && false && true && false
}

#[clippy::cognitive_complexity = "0"]
fn osscilating_logical_chain_3() -> bool {
(true && false) || (true && false)
}

#[clippy::cognitive_complexity = "1"]
fn nested_functions_are_counted(n: usize) -> usize {
fn lucas_number(n: usize) -> usize {
match n {
0 => 2,
1 => 1,
_ => lucas_number(n - 1) + lucas_number(n - 2),
}
}
lucas_number(n)
}
Loading

0 comments on commit b22341f

Please sign in to comment.