From 97c10075eca950c34958f2f32bf9956b3945ff42 Mon Sep 17 00:00:00 2001 From: Centri3 <114838443+Centri3@users.noreply.github.com> Date: Thu, 20 Apr 2023 01:14:50 -0500 Subject: [PATCH 01/10] add the `excessive_*` style lints --- CHANGELOG.md | 2 + clippy_lints/src/declared_lints.rs | 2 + clippy_lints/src/excessive_width.rs | 82 +++++++++++++++++++ clippy_lints/src/lib.rs | 11 +++ clippy_lints/src/utils/conf.rs | 12 +++ tests/ui-toml/excessive_width/clippy.toml | 3 + .../excessive_width/excessive_width.rs | 26 ++++++ .../excessive_width/excessive_width.stderr | 24 ++++++ tests/ui/excessive_width.rs | 44 ++++++++++ tests/ui/excessive_width.stderr | 11 +++ 10 files changed, 217 insertions(+) create mode 100644 clippy_lints/src/excessive_width.rs create mode 100644 tests/ui-toml/excessive_width/clippy.toml create mode 100644 tests/ui-toml/excessive_width/excessive_width.rs create mode 100644 tests/ui-toml/excessive_width/excessive_width.stderr create mode 100644 tests/ui/excessive_width.rs create mode 100644 tests/ui/excessive_width.stderr diff --git a/CHANGELOG.md b/CHANGELOG.md index a65ac4d46deb..76fd81a25c66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4758,7 +4758,9 @@ Released 2018-09-13 [`erasing_op`]: https://rust-lang.github.io/rust-clippy/master/index.html#erasing_op [`err_expect`]: https://rust-lang.github.io/rust-clippy/master/index.html#err_expect [`eval_order_dependence`]: https://rust-lang.github.io/rust-clippy/master/index.html#eval_order_dependence +[`excessive_indentation`]: https://rust-lang.github.io/rust-clippy/master/index.html#excessive_indentation [`excessive_precision`]: https://rust-lang.github.io/rust-clippy/master/index.html#excessive_precision +[`excessive_width`]: https://rust-lang.github.io/rust-clippy/master/index.html#excessive_width [`exhaustive_enums`]: https://rust-lang.github.io/rust-clippy/master/index.html#exhaustive_enums [`exhaustive_structs`]: https://rust-lang.github.io/rust-clippy/master/index.html#exhaustive_structs [`exit`]: https://rust-lang.github.io/rust-clippy/master/index.html#exit diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index d014534cee91..209113486908 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -159,6 +159,8 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::eta_reduction::REDUNDANT_CLOSURE_FOR_METHOD_CALLS_INFO, crate::excessive_bools::FN_PARAMS_EXCESSIVE_BOOLS_INFO, crate::excessive_bools::STRUCT_EXCESSIVE_BOOLS_INFO, + crate::excessive_width::EXCESSIVE_INDENTATION_INFO, + crate::excessive_width::EXCESSIVE_WIDTH_INFO, crate::exhaustive_items::EXHAUSTIVE_ENUMS_INFO, crate::exhaustive_items::EXHAUSTIVE_STRUCTS_INFO, crate::exit::EXIT_INFO, diff --git a/clippy_lints/src/excessive_width.rs b/clippy_lints/src/excessive_width.rs new file mode 100644 index 000000000000..84ae54f401e5 --- /dev/null +++ b/clippy_lints/src/excessive_width.rs @@ -0,0 +1,82 @@ +use clippy_utils::diagnostics::span_lint_and_help; +use rustc_hir::*; +use rustc_lint::{LateContext, LateLintPass, LintContext}; +use rustc_middle::lint::in_external_macro; +use rustc_session::{declare_tool_lint, impl_lint_pass}; +use rustc_span::Pos; + +// TODO: This still needs to be implemented. +declare_clippy_lint! { + /// ### What it does + /// + /// Checks for lines which are indented beyond a certain threshold. + /// + /// ### Why is this bad? + /// + /// It can severely hinder readability. The default is very generous; if you + /// exceed this, it's a sign you should refactor. + /// + /// ### Example + /// TODO + /// Use instead: + /// TODO + #[clippy::version = "1.70.0"] + pub EXCESSIVE_INDENTATION, + style, + "check for lines intended beyond a certain threshold" +} +declare_clippy_lint! { + /// ### What it does + /// + /// Checks for lines which are longer than a certain threshold. + /// + /// ### Why is this bad? + /// + /// It can severely hinder readability. Almost always, running rustfmt will get this + /// below this threshold (or whatever you have set as max_width), but if it fails, + /// it's probably a sign you should refactor. + /// + /// ### Example + /// TODO + /// Use instead: + /// TODO + #[clippy::version = "1.70.0"] + pub EXCESSIVE_WIDTH, + style, + "check for lines longer than a certain threshold" +} +impl_lint_pass!(ExcessiveWidth => [EXCESSIVE_INDENTATION, EXCESSIVE_WIDTH]); + +#[derive(Clone, Copy)] +pub struct ExcessiveWidth { + pub excessive_width_threshold: u64, + pub excessive_width_ignore_indentation: bool, + pub excessive_indentation_threshold: u64, +} + +impl LateLintPass<'_> for ExcessiveWidth { + fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) { + if in_external_macro(cx.sess(), stmt.span) { + return; + } + + if let Ok(lines) = cx.sess().source_map().span_to_lines(stmt.span).map(|info| info.lines) { + for line in &lines { + // TODO: yeah, no. + if (line.end_col.to_usize() + - line.start_col.to_usize() * self.excessive_width_ignore_indentation as usize) + > self.excessive_width_threshold as usize + { + span_lint_and_help( + cx, + EXCESSIVE_WIDTH, + stmt.span, + "this line is too long", + None, + "consider running rustfmt or refactoring this", + ); + } + } + } + } +} diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index d53c6de54514..8b08861bc1d5 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -122,6 +122,7 @@ mod equatable_if_let; mod escape; mod eta_reduction; mod excessive_bools; +mod excessive_width; mod exhaustive_items; mod exit; mod explicit_write; @@ -1007,6 +1008,16 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(tests_outside_test_module::TestsOutsideTestModule)); store.register_late_pass(|_| Box::new(manual_slice_size_calculation::ManualSliceSizeCalculation)); store.register_early_pass(|| Box::new(suspicious_doc_comments::SuspiciousDocComments)); + let excessive_width_threshold = conf.excessive_width_threshold; + let excessive_width_ignore_indentation = conf.excessive_width_ignore_indentation; + let excessive_indentation_threshold = conf.excessive_indentation_threshold; + store.register_late_pass(move |_| { + Box::new(excessive_width::ExcessiveWidth { + excessive_width_threshold, + excessive_width_ignore_indentation, + excessive_indentation_threshold, + }) + }); store.register_late_pass(|_| Box::new(items_after_test_module::ItemsAfterTestModule)); store.register_early_pass(|| Box::new(ref_patterns::RefPatterns)); store.register_late_pass(|_| Box::new(default_constructed_unit_structs::DefaultConstructedUnitStructs)); diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index 35f40830681d..1bdb5e424183 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -305,6 +305,18 @@ define_Conf! { /// /// The maximum cognitive complexity a function can have (cognitive_complexity_threshold: u64 = 25), + /// Lint: EXCESSIVE_WIDTH. + /// + /// The maximum width a statement can have + (excessive_width_threshold: u64 = 100), + /// Lint: EXCESSIVE_WIDTH. + /// + /// Whether to ignore the line's indentation + (excessive_width_ignore_indentation: bool = true), + /// Lint: EXCESSIVE_INDENTATION. + /// + /// The maximum indentation a statement can have + (excessive_indentation_threshold: u64 = 10), /// DEPRECATED LINT: CYCLOMATIC_COMPLEXITY. /// /// Use the Cognitive Complexity lint instead. diff --git a/tests/ui-toml/excessive_width/clippy.toml b/tests/ui-toml/excessive_width/clippy.toml new file mode 100644 index 000000000000..1824c8a3127d --- /dev/null +++ b/tests/ui-toml/excessive_width/clippy.toml @@ -0,0 +1,3 @@ +excessive-width-threshold = 20 +excessive-width-ignore-indentation = false +excessive-indentation-threshold = 3 diff --git a/tests/ui-toml/excessive_width/excessive_width.rs b/tests/ui-toml/excessive_width/excessive_width.rs new file mode 100644 index 000000000000..261b937e72e9 --- /dev/null +++ b/tests/ui-toml/excessive_width/excessive_width.rs @@ -0,0 +1,26 @@ +#![allow(unused)] +#![allow(clippy::identity_op)] +#![allow(clippy::no_effect)] +#![warn(clippy::excessive_width)] + +static mut C: u32 = 2u32; + +#[rustfmt::skip] +fn main() { + let x = 2 * unsafe { C }; + + { + { + // this too, even though it's only 15 characters! + (); + } + } + + { + { + { + println!("this will now emit a warning, how neat!") + } + } + } +} diff --git a/tests/ui-toml/excessive_width/excessive_width.stderr b/tests/ui-toml/excessive_width/excessive_width.stderr new file mode 100644 index 000000000000..00dce391be0b --- /dev/null +++ b/tests/ui-toml/excessive_width/excessive_width.stderr @@ -0,0 +1,24 @@ +error: this line is too long + --> $DIR/excessive_width.rs:10:5 + | +LL | let x = 2 * unsafe { C }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: consider running rustfmt or refactoring this + = note: `-D clippy::excessive-width` implied by `-D warnings` + +error: this line is too long + --> $DIR/excessive_width.rs:12:5 + | +LL | / { +LL | | { +LL | | // this too, even though it's only 15 characters! +LL | | (); +LL | | } +LL | | } + | |_____^ + | + = help: consider running rustfmt or refactoring this + +error: aborting due to 2 previous errors + diff --git a/tests/ui/excessive_width.rs b/tests/ui/excessive_width.rs new file mode 100644 index 000000000000..218950f9684c --- /dev/null +++ b/tests/ui/excessive_width.rs @@ -0,0 +1,44 @@ +#![allow(unused)] +#![allow(clippy::identity_op)] +#![warn(clippy::excessive_width)] + +#[rustfmt::skip] +fn main() { + let x = 1; + + let really_long_binding_name_because_this_needs_to_be_over_90_characters_long = 1usize * 200 / 2 * 500 / 1; + + { + { + { + { + { + { + { + { + { + { + { + { + { + { + { + { + println!("highly indented lines do not cause a warning (by default)!") + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } +} diff --git a/tests/ui/excessive_width.stderr b/tests/ui/excessive_width.stderr new file mode 100644 index 000000000000..707a3796b561 --- /dev/null +++ b/tests/ui/excessive_width.stderr @@ -0,0 +1,11 @@ +error: this line is too long + --> $DIR/excessive_width.rs:9:5 + | +LL | let really_long_binding_name_because_this_needs_to_be_over_90_characters_long = 1usize * 200 / 2 * 500 / 1; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: consider running rustfmt or refactoring this + = note: `-D clippy::excessive-width` implied by `-D warnings` + +error: aborting due to previous error + From 4af3ac1cd8cf907b142c5cd813f1733289c29063 Mon Sep 17 00:00:00 2001 From: Centri3 <114838443+Centri3@users.noreply.github.com> Date: Thu, 20 Apr 2023 01:19:26 -0500 Subject: [PATCH 02/10] change it to nursery category --- clippy_lints/src/excessive_width.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clippy_lints/src/excessive_width.rs b/clippy_lints/src/excessive_width.rs index 84ae54f401e5..0e23b39a14e1 100644 --- a/clippy_lints/src/excessive_width.rs +++ b/clippy_lints/src/excessive_width.rs @@ -22,7 +22,7 @@ declare_clippy_lint! { /// TODO #[clippy::version = "1.70.0"] pub EXCESSIVE_INDENTATION, - style, + nursery, "check for lines intended beyond a certain threshold" } declare_clippy_lint! { @@ -42,7 +42,7 @@ declare_clippy_lint! { /// TODO #[clippy::version = "1.70.0"] pub EXCESSIVE_WIDTH, - style, + nursery, "check for lines longer than a certain threshold" } impl_lint_pass!(ExcessiveWidth => [EXCESSIVE_INDENTATION, EXCESSIVE_WIDTH]); From e68dbc3308397e68d672f9f70742af41897b06be Mon Sep 17 00:00:00 2001 From: Centri3 <114838443+Centri3@users.noreply.github.com> Date: Thu, 20 Apr 2023 08:03:25 -0500 Subject: [PATCH 03/10] add `excessive_nesting` Close code block in example --- CHANGELOG.md | 3 +- clippy_lints/src/declared_lints.rs | 3 +- clippy_lints/src/excessive_nesting.rs | 325 ++++++++++++++++ clippy_lints/src/excessive_width.rs | 82 ---- clippy_lints/src/lib.rs | 14 +- clippy_lints/src/utils/conf.rs | 14 +- .../auxiliary/macro_rules.rs | 7 + tests/ui-toml/excessive_nesting/clippy.toml | 1 + .../excessive_nesting/excessive_nesting.rs | 191 ++++++++++ .../excessive_nesting.stderr | 358 ++++++++++++++++++ tests/ui-toml/excessive_width/clippy.toml | 3 - .../excessive_width/excessive_width.rs | 26 -- .../excessive_width/excessive_width.stderr | 24 -- .../toml_unknown_key/conf_unknown_key.stderr | 1 + tests/ui/auxiliary/macro_rules.rs | 1 - tests/ui/excessive_width.rs | 44 --- tests/ui/excessive_width.stderr | 11 - 17 files changed, 893 insertions(+), 215 deletions(-) create mode 100644 clippy_lints/src/excessive_nesting.rs delete mode 100644 clippy_lints/src/excessive_width.rs create mode 100644 tests/ui-toml/excessive_nesting/auxiliary/macro_rules.rs create mode 100644 tests/ui-toml/excessive_nesting/clippy.toml create mode 100644 tests/ui-toml/excessive_nesting/excessive_nesting.rs create mode 100644 tests/ui-toml/excessive_nesting/excessive_nesting.stderr delete mode 100644 tests/ui-toml/excessive_width/clippy.toml delete mode 100644 tests/ui-toml/excessive_width/excessive_width.rs delete mode 100644 tests/ui-toml/excessive_width/excessive_width.stderr delete mode 100644 tests/ui/excessive_width.rs delete mode 100644 tests/ui/excessive_width.stderr diff --git a/CHANGELOG.md b/CHANGELOG.md index 76fd81a25c66..69ac2c893d9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4758,9 +4758,8 @@ Released 2018-09-13 [`erasing_op`]: https://rust-lang.github.io/rust-clippy/master/index.html#erasing_op [`err_expect`]: https://rust-lang.github.io/rust-clippy/master/index.html#err_expect [`eval_order_dependence`]: https://rust-lang.github.io/rust-clippy/master/index.html#eval_order_dependence -[`excessive_indentation`]: https://rust-lang.github.io/rust-clippy/master/index.html#excessive_indentation +[`excessive_nesting`]: https://rust-lang.github.io/rust-clippy/master/index.html#excessive_nesting [`excessive_precision`]: https://rust-lang.github.io/rust-clippy/master/index.html#excessive_precision -[`excessive_width`]: https://rust-lang.github.io/rust-clippy/master/index.html#excessive_width [`exhaustive_enums`]: https://rust-lang.github.io/rust-clippy/master/index.html#exhaustive_enums [`exhaustive_structs`]: https://rust-lang.github.io/rust-clippy/master/index.html#exhaustive_structs [`exit`]: https://rust-lang.github.io/rust-clippy/master/index.html#exit diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index 209113486908..0c51996b6253 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -159,8 +159,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::eta_reduction::REDUNDANT_CLOSURE_FOR_METHOD_CALLS_INFO, crate::excessive_bools::FN_PARAMS_EXCESSIVE_BOOLS_INFO, crate::excessive_bools::STRUCT_EXCESSIVE_BOOLS_INFO, - crate::excessive_width::EXCESSIVE_INDENTATION_INFO, - crate::excessive_width::EXCESSIVE_WIDTH_INFO, + crate::excessive_nesting::EXCESSIVE_NESTING_INFO, crate::exhaustive_items::EXHAUSTIVE_ENUMS_INFO, crate::exhaustive_items::EXHAUSTIVE_STRUCTS_INFO, crate::exit::EXIT_INFO, diff --git a/clippy_lints/src/excessive_nesting.rs b/clippy_lints/src/excessive_nesting.rs new file mode 100644 index 000000000000..a60e2442fa29 --- /dev/null +++ b/clippy_lints/src/excessive_nesting.rs @@ -0,0 +1,325 @@ +use clippy_utils::diagnostics::span_lint_and_help; +use rustc_ast::{ + node_id::NodeId, + ptr::P, + visit::{FnKind, Visitor}, + Arm, AssocItemKind, Block, Expr, ExprKind, Inline, Item, ItemKind, Local, LocalKind, ModKind, ModSpans, Pat, + PatKind, Stmt, StmtKind, +}; +use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; +use rustc_middle::lint::in_external_macro; +use rustc_session::{declare_tool_lint, impl_lint_pass}; +use rustc_span::Span; +use thin_vec::ThinVec; + +declare_clippy_lint! { + /// ### What it does + /// + /// Checks for blocks which are indented beyond a certain threshold. + /// + /// ### Why is this bad? + /// + /// It can severely hinder readability. The default is very generous; if you + /// exceed this, it's a sign you should refactor. + /// + /// ### Known issues + /// + /// Nested inline modules will all be linted, rather than just the outermost one + /// that applies. This makes the output a bit verbose. + /// + /// ### Example + /// An example clippy.toml configuration: + /// ```toml + /// # clippy.toml + /// excessive-nesting-threshold = 3 + /// ``` + /// lib.rs: + /// ```rust,ignore + /// pub mod a { + /// pub struct X; + /// impl X { + /// pub fn run(&self) { + /// if true { + /// // etc... + /// } + /// } + /// } + /// } + /// Use instead: + /// a.rs: + /// ```rust,ignore + /// fn private_run(x: &X) { + /// if true { + /// // etc... + /// } + /// } + /// + /// pub struct X; + /// impl X { + /// pub fn run(&self) { + /// private_run(self); + /// } + /// } + /// ``` + #[clippy::version = "1.70.0"] + pub EXCESSIVE_NESTING, + restriction, + "checks for blocks nested beyond a certain threshold" +} +impl_lint_pass!(ExcessiveNesting => [EXCESSIVE_NESTING]); + +#[derive(Clone, Copy)] +pub struct ExcessiveNesting { + pub excessive_nesting_threshold: u64, +} + +impl EarlyLintPass for ExcessiveNesting { + fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { + let conf = self; + NestingVisitor { + conf, + cx, + nest_level: 0, + } + .visit_item(item); + } +} + +struct NestingVisitor<'conf, 'cx> { + conf: &'conf ExcessiveNesting, + cx: &'cx EarlyContext<'cx>, + nest_level: u64, +} + +impl<'conf, 'cx> Visitor<'_> for NestingVisitor<'conf, 'cx> { + fn visit_local(&mut self, local: &Local) { + self.visit_pat(&local.pat); + + match &local.kind { + LocalKind::Init(expr) => self.visit_expr(expr), + LocalKind::InitElse(expr, block) => { + self.visit_expr(expr); + self.visit_block(block); + }, + LocalKind::Decl => (), + } + } + + fn visit_block(&mut self, block: &Block) { + self.nest_level += 1; + + if !check_indent(self, block.span) { + for stmt in &block.stmts { + self.visit_stmt(stmt); + } + } + + self.nest_level -= 1; + } + + fn visit_stmt(&mut self, stmt: &Stmt) { + match &stmt.kind { + StmtKind::Local(local) => self.visit_local(local), + StmtKind::Item(item) => self.visit_item(item), + StmtKind::Expr(expr) | StmtKind::Semi(expr) => self.visit_expr(expr), + _ => (), + } + } + + fn visit_arm(&mut self, arm: &Arm) { + self.visit_pat(&arm.pat); + if let Some(expr) = &arm.guard { + self.visit_expr(expr); + } + self.visit_expr(&arm.body); + } + + // TODO: Is this necessary? + fn visit_pat(&mut self, pat: &Pat) { + match &pat.kind { + PatKind::Box(pat) | PatKind::Ref(pat, ..) | PatKind::Paren(pat) => self.visit_pat(pat), + PatKind::Lit(expr) => self.visit_expr(expr), + PatKind::Range(start, end, ..) => { + if let Some(expr) = start { + self.visit_expr(expr); + } + if let Some(expr) = end { + self.visit_expr(expr); + } + }, + PatKind::Ident(.., pat) if let Some(pat) = pat => { + self.visit_pat(pat); + }, + PatKind::Struct(.., pat_fields, _) => { + for pat_field in pat_fields { + self.visit_pat(&pat_field.pat); + } + }, + PatKind::TupleStruct(.., pats) | PatKind::Or(pats) | PatKind::Tuple(pats) | PatKind::Slice(pats) => { + for pat in pats { + self.visit_pat(pat); + } + }, + _ => (), + } + } + + fn visit_expr(&mut self, expr: &Expr) { + // This is a mess, but really all it does is extract every expression from every applicable variant + // of ExprKind until it finds a Block. + match &expr.kind { + ExprKind::ConstBlock(anon_const) => self.visit_expr(&anon_const.value), + ExprKind::Call(.., args) => { + for expr in args { + self.visit_expr(expr); + } + }, + ExprKind::MethodCall(method_call) => { + for expr in &method_call.args { + self.visit_expr(expr); + } + }, + ExprKind::Tup(exprs) | ExprKind::Array(exprs) => { + for expr in exprs { + self.visit_expr(expr); + } + }, + ExprKind::Binary(.., left, right) + | ExprKind::Assign(left, right, ..) + | ExprKind::AssignOp(.., left, right) + | ExprKind::Index(left, right) => { + self.visit_expr(left); + self.visit_expr(right); + }, + ExprKind::Let(pat, expr, ..) => { + self.visit_pat(pat); + self.visit_expr(expr); + }, + ExprKind::Unary(.., expr) + | ExprKind::Await(expr) + | ExprKind::Field(expr, ..) + | ExprKind::AddrOf(.., expr) + | ExprKind::Try(expr) => { + self.visit_expr(expr); + }, + ExprKind::Repeat(expr, anon_const) => { + self.visit_expr(expr); + self.visit_expr(&anon_const.value); + }, + ExprKind::If(expr, block, else_expr) => { + self.visit_expr(expr); + self.visit_block(block); + + if let Some(expr) = else_expr { + self.visit_expr(expr); + } + }, + ExprKind::While(expr, block, ..) => { + self.visit_expr(expr); + self.visit_block(block); + }, + ExprKind::ForLoop(pat, expr, block, ..) => { + self.visit_pat(pat); + self.visit_expr(expr); + self.visit_block(block); + }, + ExprKind::Loop(block, ..) + | ExprKind::Block(block, ..) + | ExprKind::Async(.., block) + | ExprKind::TryBlock(block) => { + self.visit_block(block); + }, + ExprKind::Match(expr, arms) => { + self.visit_expr(expr); + + for arm in arms { + self.visit_arm(arm); + } + }, + ExprKind::Closure(closure) => self.visit_expr(&closure.body), + ExprKind::Range(start, end, ..) => { + if let Some(expr) = start { + self.visit_expr(expr); + } + if let Some(expr) = end { + self.visit_expr(expr); + } + }, + ExprKind::Break(.., expr) | ExprKind::Ret(expr) | ExprKind::Yield(expr) | ExprKind::Yeet(expr) => { + if let Some(expr) = expr { + self.visit_expr(expr); + } + }, + ExprKind::Struct(struct_expr) => { + for field in &struct_expr.fields { + self.visit_expr(&field.expr); + } + }, + _ => (), + } + } + + fn visit_fn(&mut self, fk: FnKind<'_>, _: Span, _: NodeId) { + match fk { + FnKind::Fn(.., block) if let Some(block) = block => self.visit_block(block), + FnKind::Closure(.., expr) => self.visit_expr(expr), + // :/ + FnKind::Fn(..) => (), + } + } + + fn visit_item(&mut self, item: &Item) { + match &item.kind { + ItemKind::Static(static_item) if let Some(expr) = static_item.expr.as_ref() => self.visit_expr(expr), + ItemKind::Const(const_item) if let Some(expr) = const_item.expr.as_ref() => self.visit_expr(expr), + ItemKind::Fn(fk) if let Some(block) = fk.body.as_ref() => self.visit_block(block), + ItemKind::Mod(.., mod_kind) + if let ModKind::Loaded(items, Inline::Yes, ModSpans { inner_span, ..}) = mod_kind => + { + self.nest_level += 1; + + check_indent(self, *inner_span); + + self.nest_level -= 1; + } + ItemKind::Trait(trit) => check_trait_and_impl(self, item, &trit.items), + ItemKind::Impl(imp) => check_trait_and_impl(self, item, &imp.items), + _ => (), + } + } +} + +fn check_trait_and_impl(visitor: &mut NestingVisitor<'_, '_>, item: &Item, items: &ThinVec
>>) { + visitor.nest_level += 1; + + if !check_indent(visitor, item.span) { + for item in items { + match &item.kind { + AssocItemKind::Const(const_item) if let Some(expr) = const_item.expr.as_ref() => { + visitor.visit_expr(expr); + }, + AssocItemKind::Fn(fk) if let Some(block) = fk.body.as_ref() => visitor.visit_block(block), + _ => (), + } + } + } + + visitor.nest_level -= 1; +} + +fn check_indent(visitor: &NestingVisitor<'_, '_>, span: Span) -> bool { + if visitor.nest_level > visitor.conf.excessive_nesting_threshold && !in_external_macro(visitor.cx.sess(), span) { + span_lint_and_help( + visitor.cx, + EXCESSIVE_NESTING, + span, + "this block is too nested", + None, + "try refactoring your code, extraction is often both easier to read and less nested", + ); + + return true; + } + + false +} diff --git a/clippy_lints/src/excessive_width.rs b/clippy_lints/src/excessive_width.rs deleted file mode 100644 index 0e23b39a14e1..000000000000 --- a/clippy_lints/src/excessive_width.rs +++ /dev/null @@ -1,82 +0,0 @@ -use clippy_utils::diagnostics::span_lint_and_help; -use rustc_hir::*; -use rustc_lint::{LateContext, LateLintPass, LintContext}; -use rustc_middle::lint::in_external_macro; -use rustc_session::{declare_tool_lint, impl_lint_pass}; -use rustc_span::Pos; - -// TODO: This still needs to be implemented. -declare_clippy_lint! { - /// ### What it does - /// - /// Checks for lines which are indented beyond a certain threshold. - /// - /// ### Why is this bad? - /// - /// It can severely hinder readability. The default is very generous; if you - /// exceed this, it's a sign you should refactor. - /// - /// ### Example - /// TODO - /// Use instead: - /// TODO - #[clippy::version = "1.70.0"] - pub EXCESSIVE_INDENTATION, - nursery, - "check for lines intended beyond a certain threshold" -} -declare_clippy_lint! { - /// ### What it does - /// - /// Checks for lines which are longer than a certain threshold. - /// - /// ### Why is this bad? - /// - /// It can severely hinder readability. Almost always, running rustfmt will get this - /// below this threshold (or whatever you have set as max_width), but if it fails, - /// it's probably a sign you should refactor. - /// - /// ### Example - /// TODO - /// Use instead: - /// TODO - #[clippy::version = "1.70.0"] - pub EXCESSIVE_WIDTH, - nursery, - "check for lines longer than a certain threshold" -} -impl_lint_pass!(ExcessiveWidth => [EXCESSIVE_INDENTATION, EXCESSIVE_WIDTH]); - -#[derive(Clone, Copy)] -pub struct ExcessiveWidth { - pub excessive_width_threshold: u64, - pub excessive_width_ignore_indentation: bool, - pub excessive_indentation_threshold: u64, -} - -impl LateLintPass<'_> for ExcessiveWidth { - fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) { - if in_external_macro(cx.sess(), stmt.span) { - return; - } - - if let Ok(lines) = cx.sess().source_map().span_to_lines(stmt.span).map(|info| info.lines) { - for line in &lines { - // TODO: yeah, no. - if (line.end_col.to_usize() - - line.start_col.to_usize() * self.excessive_width_ignore_indentation as usize) - > self.excessive_width_threshold as usize - { - span_lint_and_help( - cx, - EXCESSIVE_WIDTH, - stmt.span, - "this line is too long", - None, - "consider running rustfmt or refactoring this", - ); - } - } - } - } -} diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 8b08861bc1d5..202b4709ad41 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -122,7 +122,7 @@ mod equatable_if_let; mod escape; mod eta_reduction; mod excessive_bools; -mod excessive_width; +mod excessive_nesting; mod exhaustive_items; mod exit; mod explicit_write; @@ -1008,14 +1008,10 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: store.register_late_pass(|_| Box::new(tests_outside_test_module::TestsOutsideTestModule)); store.register_late_pass(|_| Box::new(manual_slice_size_calculation::ManualSliceSizeCalculation)); store.register_early_pass(|| Box::new(suspicious_doc_comments::SuspiciousDocComments)); - let excessive_width_threshold = conf.excessive_width_threshold; - let excessive_width_ignore_indentation = conf.excessive_width_ignore_indentation; - let excessive_indentation_threshold = conf.excessive_indentation_threshold; - store.register_late_pass(move |_| { - Box::new(excessive_width::ExcessiveWidth { - excessive_width_threshold, - excessive_width_ignore_indentation, - excessive_indentation_threshold, + let excessive_nesting_threshold = conf.excessive_nesting_threshold; + store.register_early_pass(move || { + Box::new(excessive_nesting::ExcessiveNesting { + excessive_nesting_threshold, }) }); store.register_late_pass(|_| Box::new(items_after_test_module::ItemsAfterTestModule)); diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs index 1bdb5e424183..cf0da266dc97 100644 --- a/clippy_lints/src/utils/conf.rs +++ b/clippy_lints/src/utils/conf.rs @@ -305,18 +305,10 @@ define_Conf! { /// /// The maximum cognitive complexity a function can have (cognitive_complexity_threshold: u64 = 25), - /// Lint: EXCESSIVE_WIDTH. + /// Lint: EXCESSIVE_NESTING. /// - /// The maximum width a statement can have - (excessive_width_threshold: u64 = 100), - /// Lint: EXCESSIVE_WIDTH. - /// - /// Whether to ignore the line's indentation - (excessive_width_ignore_indentation: bool = true), - /// Lint: EXCESSIVE_INDENTATION. - /// - /// The maximum indentation a statement can have - (excessive_indentation_threshold: u64 = 10), + /// The maximum amount of nesting a block can reside in + (excessive_nesting_threshold: u64 = 10), /// DEPRECATED LINT: CYCLOMATIC_COMPLEXITY. /// /// Use the Cognitive Complexity lint instead. diff --git a/tests/ui-toml/excessive_nesting/auxiliary/macro_rules.rs b/tests/ui-toml/excessive_nesting/auxiliary/macro_rules.rs new file mode 100644 index 000000000000..86663db68f24 --- /dev/null +++ b/tests/ui-toml/excessive_nesting/auxiliary/macro_rules.rs @@ -0,0 +1,7 @@ +#[rustfmt::skip] +#[macro_export] +macro_rules! excessive_nesting { + () => {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ + println!("hi!!") + }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} +} diff --git a/tests/ui-toml/excessive_nesting/clippy.toml b/tests/ui-toml/excessive_nesting/clippy.toml new file mode 100644 index 000000000000..10be2751a867 --- /dev/null +++ b/tests/ui-toml/excessive_nesting/clippy.toml @@ -0,0 +1 @@ +excessive-nesting-threshold = 3 diff --git a/tests/ui-toml/excessive_nesting/excessive_nesting.rs b/tests/ui-toml/excessive_nesting/excessive_nesting.rs new file mode 100644 index 000000000000..66fc81663145 --- /dev/null +++ b/tests/ui-toml/excessive_nesting/excessive_nesting.rs @@ -0,0 +1,191 @@ +//@aux-build:macro_rules.rs +#![rustfmt::skip] +#![feature(custom_inner_attributes)] +#![allow(unused)] +#![allow(clippy::let_and_return)] +#![allow(clippy::redundant_closure_call)] +#![allow(clippy::no_effect)] +#![allow(clippy::unnecessary_operation)] +#![allow(clippy::never_loop)] +#![warn(clippy::excessive_nesting)] +#![allow(clippy::collapsible_if)] + +#[macro_use] +extern crate macro_rules; + +static X: u32 = { + let x = { + let y = { + let z = { + let w = { 3 }; + w + }; + z + }; + y + }; + x +}; + +macro_rules! xx { + () => {{ + { + { + { + { + { + { + { + { + { + { + println!("ehe"); + } + } + } + } + } + } + } + } + } + } + }}; +} + +struct A; + +impl A { + pub fn a(&self, v: u32) { + struct B; + + impl B { + pub fn b() { + struct C; + + impl C { + pub fn c() {} + } + } + } + } +} + +struct D { d: u32 } + +trait Lol { + fn lmao() { + fn bb() { + fn cc() { + let x = { 1 }; // not a warning + } + + let x = { 1 }; // warning + } + } +} + +use a::{b::{c::{d::{e::{f::{}}}}}}; // should not lint + +pub mod a { + pub mod b { + pub mod c { + pub mod d { + pub mod e { + pub mod f {} + } + } + } + } +} + +fn a_but_not(v: u32) {} + +fn main() { + let a = A; + + a_but_not({{{{{{{{0}}}}}}}}); + a.a({{{{{{{{{0}}}}}}}}}); + (0, {{{{{{{1}}}}}}}); + + if true { + if true { + if true { + if true { + if true { + + } + } + } + } + } + + let y = (|| { + let x = (|| { + let y = (|| { + let z = (|| { + let w = { 3 }; + w + })(); + z + })(); + y + })(); + x + })(); + + excessive_nesting!(); // ensure this isn't linted in external macros + xx!(); + let boo = true; + !{boo as u32 + !{boo as u32 + !{boo as u32}}}; + + let mut y = 1; + y += {{{{{5}}}}}; + let z = y + {{{{{{{{{5}}}}}}}}}; + [0, {{{{{{{{{{0}}}}}}}}}}]; + let mut xx = [0; {{{{{{{{100}}}}}}}}]; + xx[{{{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}}}]; + &mut {{{{{{{{{{y}}}}}}}}}}; + + for i in {{{{xx}}}} {{{{{{{{}}}}}}}} + + while let Some(i) = {{{{{{Some(1)}}}}}} {{{{{{{}}}}}}} + + while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}} + + let d = D { d: {{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}} }; + + {{{{1;}}}}..{{{{{{3}}}}}}; + {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}}; + ..{{{{{{{5}}}}}}}; + ..={{{{{3}}}}}; + {{{{{1;}}}}}..; + + loop { break {{{{1}}}} }; + loop {{{{{{}}}}}} + + match {{{{{{true}}}}}} { + true => {{{{}}}}, + false => {{{{}}}}, + } + + { + { + { + { + println!("warning! :)"); + } + } + } + } +} + +async fn b() -> u32 { + async fn c() -> u32 {{{{{{{0}}}}}}} + + c().await +} + +async fn a() { + {{{{b().await}}}}; +} diff --git a/tests/ui-toml/excessive_nesting/excessive_nesting.stderr b/tests/ui-toml/excessive_nesting/excessive_nesting.stderr new file mode 100644 index 000000000000..0bd43da21e24 --- /dev/null +++ b/tests/ui-toml/excessive_nesting/excessive_nesting.stderr @@ -0,0 +1,358 @@ +error: this block is too nested + --> $DIR/excessive_nesting.rs:19:21 + | +LL | let z = { + | _____________________^ +LL | | let w = { 3 }; +LL | | w +LL | | }; + | |_____________^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + = note: `-D clippy::excessive-nesting` implied by `-D warnings` + +error: this block is too nested + --> $DIR/excessive_nesting.rs:63:24 + | +LL | pub fn b() { + | ________________________^ +LL | | struct C; +LL | | +LL | | impl C { +LL | | pub fn c() {} +LL | | } +LL | | } + | |_____________^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:67:32 + | +LL | pub fn c() {} + | ^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:79:21 + | +LL | fn cc() { + | _____________________^ +LL | | let x = { 1 }; // not a warning +LL | | } + | |_____________^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:83:21 + | +LL | let x = { 1 }; // warning + | ^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:107:17 + | +LL | a_but_not({{{{{{{{0}}}}}}}}); + | ^^^^^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:108:11 + | +LL | a.a({{{{{{{{{0}}}}}}}}}); + | ^^^^^^^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:109:11 + | +LL | (0, {{{{{{{1}}}}}}}); + | ^^^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:113:21 + | +LL | if true { + | _____________________^ +LL | | if true { +LL | | if true { +LL | | +LL | | } +LL | | } +LL | | } + | |_____________^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:33:13 + | +LL | / { +LL | | { +LL | | { +LL | | { +... | +LL | | } +LL | | } + | |_____________^ +... +LL | xx!(); + | ----- in this macro invocation + | + = help: try refactoring your code, extraction is often both easier to read and less nested + = note: this error originates in the macro `xx` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: this block is too nested + --> $DIR/excessive_nesting.rs:140:36 + | +LL | !{boo as u32 + !{boo as u32 + !{boo as u32}}}; + | ^^^^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:143:12 + | +LL | y += {{{{{5}}}}}; + | ^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:144:19 + | +LL | let z = y + {{{{{{{{{5}}}}}}}}}; + | ^^^^^^^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:145:11 + | +LL | [0, {{{{{{{{{{0}}}}}}}}}}]; + | ^^^^^^^^^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:146:24 + | +LL | let mut xx = [0; {{{{{{{{100}}}}}}}}]; + | ^^^^^^^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:147:10 + | +LL | xx[{{{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}}}]; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:148:12 + | +LL | &mut {{{{{{{{{{y}}}}}}}}}}; + | ^^^^^^^^^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:150:16 + | +LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}} + | ^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:150:27 + | +LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}} + | ^^^^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:152:27 + | +LL | while let Some(i) = {{{{{{Some(1)}}}}}} {{{{{{{}}}}}}} + | ^^^^^^^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:152:47 + | +LL | while let Some(i) = {{{{{{Some(1)}}}}}} {{{{{{{}}}}}}} + | ^^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:154:13 + | +LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}} + | ^^^^^^^^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:154:34 + | +LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}} + | ^^^^^^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:156:22 + | +LL | let d = D { d: {{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}} }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:158:7 + | +LL | {{{{1;}}}}..{{{{{{3}}}}}}; + | ^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:158:19 + | +LL | {{{{1;}}}}..{{{{{{3}}}}}}; + | ^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:159:7 + | +LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}}; + | ^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:159:20 + | +LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}}; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:160:9 + | +LL | ..{{{{{{{5}}}}}}}; + | ^^^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:161:10 + | +LL | ..={{{{{3}}}}}; + | ^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:162:7 + | +LL | {{{{{1;}}}}}..; + | ^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:164:19 + | +LL | loop { break {{{{1}}}} }; + | ^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:165:12 + | +LL | loop {{{{{{}}}}}} + | ^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:167:13 + | +LL | match {{{{{{true}}}}}} { + | ^^^^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:168:19 + | +LL | true => {{{{}}}}, + | ^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:169:20 + | +LL | false => {{{{}}}}, + | ^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:174:13 + | +LL | / { +LL | | { +LL | | println!("warning! :)"); +LL | | } +LL | | } + | |_____________^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:184:27 + | +LL | async fn c() -> u32 {{{{{{{0}}}}}}} + | ^^^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:184:28 + | +LL | async fn c() -> u32 {{{{{{{0}}}}}}} + | ^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: this block is too nested + --> $DIR/excessive_nesting.rs:190:7 + | +LL | {{{{b().await}}}}; + | ^^^^^^^^^^^^^ + | + = help: try refactoring your code, extraction is often both easier to read and less nested + +error: aborting due to 40 previous errors + diff --git a/tests/ui-toml/excessive_width/clippy.toml b/tests/ui-toml/excessive_width/clippy.toml deleted file mode 100644 index 1824c8a3127d..000000000000 --- a/tests/ui-toml/excessive_width/clippy.toml +++ /dev/null @@ -1,3 +0,0 @@ -excessive-width-threshold = 20 -excessive-width-ignore-indentation = false -excessive-indentation-threshold = 3 diff --git a/tests/ui-toml/excessive_width/excessive_width.rs b/tests/ui-toml/excessive_width/excessive_width.rs deleted file mode 100644 index 261b937e72e9..000000000000 --- a/tests/ui-toml/excessive_width/excessive_width.rs +++ /dev/null @@ -1,26 +0,0 @@ -#![allow(unused)] -#![allow(clippy::identity_op)] -#![allow(clippy::no_effect)] -#![warn(clippy::excessive_width)] - -static mut C: u32 = 2u32; - -#[rustfmt::skip] -fn main() { - let x = 2 * unsafe { C }; - - { - { - // this too, even though it's only 15 characters! - (); - } - } - - { - { - { - println!("this will now emit a warning, how neat!") - } - } - } -} diff --git a/tests/ui-toml/excessive_width/excessive_width.stderr b/tests/ui-toml/excessive_width/excessive_width.stderr deleted file mode 100644 index 00dce391be0b..000000000000 --- a/tests/ui-toml/excessive_width/excessive_width.stderr +++ /dev/null @@ -1,24 +0,0 @@ -error: this line is too long - --> $DIR/excessive_width.rs:10:5 - | -LL | let x = 2 * unsafe { C }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: consider running rustfmt or refactoring this - = note: `-D clippy::excessive-width` implied by `-D warnings` - -error: this line is too long - --> $DIR/excessive_width.rs:12:5 - | -LL | / { -LL | | { -LL | | // this too, even though it's only 15 characters! -LL | | (); -LL | | } -LL | | } - | |_____^ - | - = help: consider running rustfmt or refactoring this - -error: aborting due to 2 previous errors - diff --git a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr index b6038f031f3c..28b6e7aa8248 100644 --- a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr +++ b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr @@ -24,6 +24,7 @@ error: error reading Clippy's configuration file: unknown field `foobar`, expect enforced-import-renames enum-variant-name-threshold enum-variant-size-threshold + excessive-nesting-threshold future-size-threshold ignore-interior-mutability large-error-threshold diff --git a/tests/ui/auxiliary/macro_rules.rs b/tests/ui/auxiliary/macro_rules.rs index e5bb906663c5..d9a1e76c077a 100644 --- a/tests/ui/auxiliary/macro_rules.rs +++ b/tests/ui/auxiliary/macro_rules.rs @@ -1,7 +1,6 @@ #![allow(dead_code)] //! Used to test that certain lints don't trigger in imported external macros - #[macro_export] macro_rules! try_err { () => { diff --git a/tests/ui/excessive_width.rs b/tests/ui/excessive_width.rs deleted file mode 100644 index 218950f9684c..000000000000 --- a/tests/ui/excessive_width.rs +++ /dev/null @@ -1,44 +0,0 @@ -#![allow(unused)] -#![allow(clippy::identity_op)] -#![warn(clippy::excessive_width)] - -#[rustfmt::skip] -fn main() { - let x = 1; - - let really_long_binding_name_because_this_needs_to_be_over_90_characters_long = 1usize * 200 / 2 * 500 / 1; - - { - { - { - { - { - { - { - { - { - { - { - { - { - { - { - { - println!("highly indented lines do not cause a warning (by default)!") - } - } - } - } - } - } - } - } - } - } - } - } - } - } - } - } -} diff --git a/tests/ui/excessive_width.stderr b/tests/ui/excessive_width.stderr deleted file mode 100644 index 707a3796b561..000000000000 --- a/tests/ui/excessive_width.stderr +++ /dev/null @@ -1,11 +0,0 @@ -error: this line is too long - --> $DIR/excessive_width.rs:9:5 - | -LL | let really_long_binding_name_because_this_needs_to_be_over_90_characters_long = 1usize * 200 / 2 * 500 / 1; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: consider running rustfmt or refactoring this - = note: `-D clippy::excessive-width` implied by `-D warnings` - -error: aborting due to previous error - From a9da61b1153edd8a8b49226ffd433e0c6a402e38 Mon Sep 17 00:00:00 2001 From: Catherine <114838443+Centri3@users.noreply.github.com> Date: Fri, 21 Apr 2023 10:59:36 -0500 Subject: [PATCH 04/10] couple more notes --- clippy_lints/src/excessive_nesting.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/clippy_lints/src/excessive_nesting.rs b/clippy_lints/src/excessive_nesting.rs index a60e2442fa29..604448d2e9c9 100644 --- a/clippy_lints/src/excessive_nesting.rs +++ b/clippy_lints/src/excessive_nesting.rs @@ -106,6 +106,8 @@ impl<'conf, 'cx> Visitor<'_> for NestingVisitor<'conf, 'cx> { } fn visit_block(&mut self, block: &Block) { + // TODO: Can we use some RAII guard instead? Borrow checker seems to hate that + // idea but it would be a lot cleaner. self.nest_level += 1; if !check_indent(self, block.span) { @@ -167,6 +169,8 @@ impl<'conf, 'cx> Visitor<'_> for NestingVisitor<'conf, 'cx> { fn visit_expr(&mut self, expr: &Expr) { // This is a mess, but really all it does is extract every expression from every applicable variant // of ExprKind until it finds a Block. + // TODO: clippy_utils has the two functions for_each_expr and for_each_expr_with_closures, can those + // be used here or are they not applicable for this case? match &expr.kind { ExprKind::ConstBlock(anon_const) => self.visit_expr(&anon_const.value), ExprKind::Call(.., args) => { From 88143ac295ba0656b78af8f9413e4074529b42d9 Mon Sep 17 00:00:00 2001 From: Centri3 <114838443+Centri3@users.noreply.github.com> Date: Fri, 28 Apr 2023 14:22:24 -0500 Subject: [PATCH 05/10] decided against reinventing the wheel --- book/src/lint_configuration.md | 10 + clippy_lints/src/excessive_nesting.rs | 267 +++--------------- .../excessive_nesting/excessive_nesting.rs | 7 +- .../excessive_nesting.stderr | 177 ++++++------ 4 files changed, 145 insertions(+), 316 deletions(-) diff --git a/book/src/lint_configuration.md b/book/src/lint_configuration.md index 4c888b63793b..4fa6b81c0027 100644 --- a/book/src/lint_configuration.md +++ b/book/src/lint_configuration.md @@ -158,6 +158,16 @@ The maximum cognitive complexity a function can have * [`cognitive_complexity`](https://rust-lang.github.io/rust-clippy/master/index.html#cognitive_complexity) +## `excessive-nesting-threshold` +The maximum amount of nesting a block can reside in + +**Default Value:** `10` (`u64`) + +--- +**Affected lints:** +* [`excessive_nesting`](https://rust-lang.github.io/rust-clippy/master/index.html#excessive_nesting) + + ## `disallowed-names` The list of disallowed names to lint about. NB: `bar` is not here since it has legitimate uses. The value `".."` can be used as part of the list to indicate, that the configured values should be appended to the diff --git a/clippy_lints/src/excessive_nesting.rs b/clippy_lints/src/excessive_nesting.rs index 604448d2e9c9..a133bdc2edf0 100644 --- a/clippy_lints/src/excessive_nesting.rs +++ b/clippy_lints/src/excessive_nesting.rs @@ -1,16 +1,12 @@ use clippy_utils::diagnostics::span_lint_and_help; use rustc_ast::{ - node_id::NodeId, - ptr::P, - visit::{FnKind, Visitor}, - Arm, AssocItemKind, Block, Expr, ExprKind, Inline, Item, ItemKind, Local, LocalKind, ModKind, ModSpans, Pat, - PatKind, Stmt, StmtKind, + visit::{walk_block, walk_item, Visitor}, + Block, Crate, Inline, Item, ItemKind, ModKind, }; use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; use rustc_middle::lint::in_external_macro; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::Span; -use thin_vec::ThinVec; declare_clippy_lint! { /// ### What it does @@ -22,11 +18,6 @@ declare_clippy_lint! { /// It can severely hinder readability. The default is very generous; if you /// exceed this, it's a sign you should refactor. /// - /// ### Known issues - /// - /// Nested inline modules will all be linted, rather than just the outermost one - /// that applies. This makes the output a bit verbose. - /// /// ### Example /// An example clippy.toml configuration: /// ```toml @@ -74,14 +65,17 @@ pub struct ExcessiveNesting { } impl EarlyLintPass for ExcessiveNesting { - fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) { + fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) { let conf = self; - NestingVisitor { + let mut visitor = NestingVisitor { conf, cx, nest_level: 0, + }; + + for item in &krate.items { + visitor.visit_item(item); } - .visit_item(item); } } @@ -91,239 +85,52 @@ struct NestingVisitor<'conf, 'cx> { nest_level: u64, } -impl<'conf, 'cx> Visitor<'_> for NestingVisitor<'conf, 'cx> { - fn visit_local(&mut self, local: &Local) { - self.visit_pat(&local.pat); - - match &local.kind { - LocalKind::Init(expr) => self.visit_expr(expr), - LocalKind::InitElse(expr, block) => { - self.visit_expr(expr); - self.visit_block(block); - }, - LocalKind::Decl => (), +impl NestingVisitor<'_, '_> { + fn check_indent(&self, span: Span) -> bool { + if self.nest_level > self.conf.excessive_nesting_threshold && !in_external_macro(self.cx.sess(), span) { + span_lint_and_help( + self.cx, + EXCESSIVE_NESTING, + span, + "this block is too nested", + None, + "try refactoring your code to minimize nesting", + ); + + return true; } + + false } +} +impl<'conf, 'cx> Visitor<'_> for NestingVisitor<'conf, 'cx> { fn visit_block(&mut self, block: &Block) { - // TODO: Can we use some RAII guard instead? Borrow checker seems to hate that - // idea but it would be a lot cleaner. self.nest_level += 1; - if !check_indent(self, block.span) { - for stmt in &block.stmts { - self.visit_stmt(stmt); - } + if !self.check_indent(block.span) { + walk_block(self, block); } self.nest_level -= 1; } - fn visit_stmt(&mut self, stmt: &Stmt) { - match &stmt.kind { - StmtKind::Local(local) => self.visit_local(local), - StmtKind::Item(item) => self.visit_item(item), - StmtKind::Expr(expr) | StmtKind::Semi(expr) => self.visit_expr(expr), - _ => (), - } - } - - fn visit_arm(&mut self, arm: &Arm) { - self.visit_pat(&arm.pat); - if let Some(expr) = &arm.guard { - self.visit_expr(expr); - } - self.visit_expr(&arm.body); - } - - // TODO: Is this necessary? - fn visit_pat(&mut self, pat: &Pat) { - match &pat.kind { - PatKind::Box(pat) | PatKind::Ref(pat, ..) | PatKind::Paren(pat) => self.visit_pat(pat), - PatKind::Lit(expr) => self.visit_expr(expr), - PatKind::Range(start, end, ..) => { - if let Some(expr) = start { - self.visit_expr(expr); - } - if let Some(expr) = end { - self.visit_expr(expr); - } - }, - PatKind::Ident(.., pat) if let Some(pat) = pat => { - self.visit_pat(pat); - }, - PatKind::Struct(.., pat_fields, _) => { - for pat_field in pat_fields { - self.visit_pat(&pat_field.pat); - } - }, - PatKind::TupleStruct(.., pats) | PatKind::Or(pats) | PatKind::Tuple(pats) | PatKind::Slice(pats) => { - for pat in pats { - self.visit_pat(pat); - } - }, - _ => (), - } - } - - fn visit_expr(&mut self, expr: &Expr) { - // This is a mess, but really all it does is extract every expression from every applicable variant - // of ExprKind until it finds a Block. - // TODO: clippy_utils has the two functions for_each_expr and for_each_expr_with_closures, can those - // be used here or are they not applicable for this case? - match &expr.kind { - ExprKind::ConstBlock(anon_const) => self.visit_expr(&anon_const.value), - ExprKind::Call(.., args) => { - for expr in args { - self.visit_expr(expr); - } - }, - ExprKind::MethodCall(method_call) => { - for expr in &method_call.args { - self.visit_expr(expr); - } - }, - ExprKind::Tup(exprs) | ExprKind::Array(exprs) => { - for expr in exprs { - self.visit_expr(expr); - } - }, - ExprKind::Binary(.., left, right) - | ExprKind::Assign(left, right, ..) - | ExprKind::AssignOp(.., left, right) - | ExprKind::Index(left, right) => { - self.visit_expr(left); - self.visit_expr(right); - }, - ExprKind::Let(pat, expr, ..) => { - self.visit_pat(pat); - self.visit_expr(expr); - }, - ExprKind::Unary(.., expr) - | ExprKind::Await(expr) - | ExprKind::Field(expr, ..) - | ExprKind::AddrOf(.., expr) - | ExprKind::Try(expr) => { - self.visit_expr(expr); - }, - ExprKind::Repeat(expr, anon_const) => { - self.visit_expr(expr); - self.visit_expr(&anon_const.value); - }, - ExprKind::If(expr, block, else_expr) => { - self.visit_expr(expr); - self.visit_block(block); - - if let Some(expr) = else_expr { - self.visit_expr(expr); - } - }, - ExprKind::While(expr, block, ..) => { - self.visit_expr(expr); - self.visit_block(block); - }, - ExprKind::ForLoop(pat, expr, block, ..) => { - self.visit_pat(pat); - self.visit_expr(expr); - self.visit_block(block); - }, - ExprKind::Loop(block, ..) - | ExprKind::Block(block, ..) - | ExprKind::Async(.., block) - | ExprKind::TryBlock(block) => { - self.visit_block(block); - }, - ExprKind::Match(expr, arms) => { - self.visit_expr(expr); - - for arm in arms { - self.visit_arm(arm); - } - }, - ExprKind::Closure(closure) => self.visit_expr(&closure.body), - ExprKind::Range(start, end, ..) => { - if let Some(expr) = start { - self.visit_expr(expr); - } - if let Some(expr) = end { - self.visit_expr(expr); - } - }, - ExprKind::Break(.., expr) | ExprKind::Ret(expr) | ExprKind::Yield(expr) | ExprKind::Yeet(expr) => { - if let Some(expr) = expr { - self.visit_expr(expr); - } - }, - ExprKind::Struct(struct_expr) => { - for field in &struct_expr.fields { - self.visit_expr(&field.expr); - } - }, - _ => (), - } - } - - fn visit_fn(&mut self, fk: FnKind<'_>, _: Span, _: NodeId) { - match fk { - FnKind::Fn(.., block) if let Some(block) = block => self.visit_block(block), - FnKind::Closure(.., expr) => self.visit_expr(expr), - // :/ - FnKind::Fn(..) => (), - } - } - fn visit_item(&mut self, item: &Item) { match &item.kind { - ItemKind::Static(static_item) if let Some(expr) = static_item.expr.as_ref() => self.visit_expr(expr), - ItemKind::Const(const_item) if let Some(expr) = const_item.expr.as_ref() => self.visit_expr(expr), - ItemKind::Fn(fk) if let Some(block) = fk.body.as_ref() => self.visit_block(block), - ItemKind::Mod(.., mod_kind) - if let ModKind::Loaded(items, Inline::Yes, ModSpans { inner_span, ..}) = mod_kind => - { + ItemKind::Trait(_) | ItemKind::Impl(_) | ItemKind::Mod(.., ModKind::Loaded(_, Inline::Yes, _)) => { self.nest_level += 1; - check_indent(self, *inner_span); + if !self.check_indent(item.span) { + walk_item(self, item); + } self.nest_level -= 1; - } - ItemKind::Trait(trit) => check_trait_and_impl(self, item, &trit.items), - ItemKind::Impl(imp) => check_trait_and_impl(self, item, &imp.items), - _ => (), - } - } -} - -fn check_trait_and_impl(visitor: &mut NestingVisitor<'_, '_>, item: &Item, items: &ThinVec
>>) {
- visitor.nest_level += 1;
-
- if !check_indent(visitor, item.span) {
- for item in items {
- match &item.kind {
- AssocItemKind::Const(const_item) if let Some(expr) = const_item.expr.as_ref() => {
- visitor.visit_expr(expr);
- },
- AssocItemKind::Fn(fk) if let Some(block) = fk.body.as_ref() => visitor.visit_block(block),
- _ => (),
- }
+ },
+ // Mod: Don't visit non-inline modules
+ // ForeignMod: I don't think this is necessary, but just incase let's not take any chances (don't want to
+ // cause any false positives)
+ ItemKind::Mod(..) | ItemKind::ForeignMod(..) => {},
+ _ => walk_item(self, item),
}
}
-
- visitor.nest_level -= 1;
-}
-
-fn check_indent(visitor: &NestingVisitor<'_, '_>, span: Span) -> bool {
- if visitor.nest_level > visitor.conf.excessive_nesting_threshold && !in_external_macro(visitor.cx.sess(), span) {
- span_lint_and_help(
- visitor.cx,
- EXCESSIVE_NESTING,
- span,
- "this block is too nested",
- None,
- "try refactoring your code, extraction is often both easier to read and less nested",
- );
-
- return true;
- }
-
- false
}
diff --git a/tests/ui-toml/excessive_nesting/excessive_nesting.rs b/tests/ui-toml/excessive_nesting/excessive_nesting.rs
index 66fc81663145..62cf51258160 100644
--- a/tests/ui-toml/excessive_nesting/excessive_nesting.rs
+++ b/tests/ui-toml/excessive_nesting/excessive_nesting.rs
@@ -77,7 +77,7 @@ trait Lol {
fn lmao() {
fn bb() {
fn cc() {
- let x = { 1 }; // not a warning
+ let x = { 1 }; // not a warning, but cc is
}
let x = { 1 }; // warning
@@ -93,8 +93,8 @@ pub mod a {
pub mod d {
pub mod e {
pub mod f {}
- }
- }
+ } // not here
+ } // only warning should be here
}
}
}
@@ -139,6 +139,7 @@ fn main() {
let boo = true;
!{boo as u32 + !{boo as u32 + !{boo as u32}}};
+ // this is a mess, but that's intentional
let mut y = 1;
y += {{{{{5}}}}};
let z = y + {{{{{{{{{5}}}}}}}}};
diff --git a/tests/ui-toml/excessive_nesting/excessive_nesting.stderr b/tests/ui-toml/excessive_nesting/excessive_nesting.stderr
index 0bd43da21e24..c98c6fefe010 100644
--- a/tests/ui-toml/excessive_nesting/excessive_nesting.stderr
+++ b/tests/ui-toml/excessive_nesting/excessive_nesting.stderr
@@ -8,7 +8,7 @@ LL | | w
LL | | };
| |_____________^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
= note: `-D clippy::excessive-nesting` implied by `-D warnings`
error: this block is too nested
@@ -24,26 +24,18 @@ LL | | }
LL | | }
| |_____________^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
-
-error: this block is too nested
- --> $DIR/excessive_nesting.rs:67:32
- |
-LL | pub fn c() {}
- | ^^
- |
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
--> $DIR/excessive_nesting.rs:79:21
|
LL | fn cc() {
| _____________________^
-LL | | let x = { 1 }; // not a warning
+LL | | let x = { 1 }; // not a warning, but cc is
LL | | }
| |_____________^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
--> $DIR/excessive_nesting.rs:83:21
@@ -51,7 +43,19 @@ error: this block is too nested
LL | let x = { 1 }; // warning
| ^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:93:13
+ |
+LL | / pub mod d {
+LL | | pub mod e {
+LL | | pub mod f {}
+LL | | } // not here
+LL | | } // only warning should be here
+ | |_____________^
+ |
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
--> $DIR/excessive_nesting.rs:107:17
@@ -59,7 +63,7 @@ error: this block is too nested
LL | a_but_not({{{{{{{{0}}}}}}}});
| ^^^^^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
--> $DIR/excessive_nesting.rs:108:11
@@ -67,7 +71,7 @@ error: this block is too nested
LL | a.a({{{{{{{{{0}}}}}}}}});
| ^^^^^^^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
--> $DIR/excessive_nesting.rs:109:11
@@ -75,7 +79,7 @@ error: this block is too nested
LL | (0, {{{{{{{1}}}}}}});
| ^^^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
--> $DIR/excessive_nesting.rs:113:21
@@ -90,7 +94,22 @@ LL | | }
LL | | }
| |_____________^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:125:25
+ |
+LL | let y = (|| {
+ | _________________________^
+LL | | let z = (|| {
+LL | | let w = { 3 };
+LL | | w
+LL | | })();
+LL | | z
+LL | | })();
+ | |_____________^
+ |
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
--> $DIR/excessive_nesting.rs:33:13
@@ -107,7 +126,7 @@ LL | | }
LL | xx!();
| ----- in this macro invocation
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
= note: this error originates in the macro `xx` (in Nightly builds, run with -Z macro-backtrace for more info)
error: this block is too nested
@@ -116,210 +135,210 @@ error: this block is too nested
LL | !{boo as u32 + !{boo as u32 + !{boo as u32}}};
| ^^^^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:143:12
+ --> $DIR/excessive_nesting.rs:144:12
|
LL | y += {{{{{5}}}}};
| ^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:144:19
+ --> $DIR/excessive_nesting.rs:145:19
|
LL | let z = y + {{{{{{{{{5}}}}}}}}};
| ^^^^^^^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:145:11
+ --> $DIR/excessive_nesting.rs:146:11
|
LL | [0, {{{{{{{{{{0}}}}}}}}}}];
| ^^^^^^^^^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:146:24
+ --> $DIR/excessive_nesting.rs:147:24
|
LL | let mut xx = [0; {{{{{{{{100}}}}}}}}];
| ^^^^^^^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:147:10
+ --> $DIR/excessive_nesting.rs:148:10
|
LL | xx[{{{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}}}];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:148:12
+ --> $DIR/excessive_nesting.rs:149:12
|
LL | &mut {{{{{{{{{{y}}}}}}}}}};
| ^^^^^^^^^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:150:16
+ --> $DIR/excessive_nesting.rs:151:16
|
LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}}
| ^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:150:27
+ --> $DIR/excessive_nesting.rs:151:27
|
LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}}
| ^^^^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:152:27
+ --> $DIR/excessive_nesting.rs:153:27
|
LL | while let Some(i) = {{{{{{Some(1)}}}}}} {{{{{{{}}}}}}}
| ^^^^^^^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:152:47
+ --> $DIR/excessive_nesting.rs:153:47
|
LL | while let Some(i) = {{{{{{Some(1)}}}}}} {{{{{{{}}}}}}}
| ^^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:154:13
+ --> $DIR/excessive_nesting.rs:155:13
|
LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}}
| ^^^^^^^^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:154:34
+ --> $DIR/excessive_nesting.rs:155:34
|
LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}}
| ^^^^^^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:156:22
+ --> $DIR/excessive_nesting.rs:157:22
|
LL | let d = D { d: {{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}} };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:158:7
+ --> $DIR/excessive_nesting.rs:159:7
|
LL | {{{{1;}}}}..{{{{{{3}}}}}};
| ^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:158:19
+ --> $DIR/excessive_nesting.rs:159:19
|
LL | {{{{1;}}}}..{{{{{{3}}}}}};
| ^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:159:7
+ --> $DIR/excessive_nesting.rs:160:7
|
LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}};
| ^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:159:20
+ --> $DIR/excessive_nesting.rs:160:20
|
LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}};
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:160:9
+ --> $DIR/excessive_nesting.rs:161:9
|
LL | ..{{{{{{{5}}}}}}};
| ^^^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:161:10
+ --> $DIR/excessive_nesting.rs:162:10
|
LL | ..={{{{{3}}}}};
| ^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:162:7
+ --> $DIR/excessive_nesting.rs:163:7
|
LL | {{{{{1;}}}}}..;
| ^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:164:19
+ --> $DIR/excessive_nesting.rs:165:19
|
LL | loop { break {{{{1}}}} };
| ^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:165:12
+ --> $DIR/excessive_nesting.rs:166:12
|
LL | loop {{{{{{}}}}}}
| ^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:167:13
+ --> $DIR/excessive_nesting.rs:168:13
|
LL | match {{{{{{true}}}}}} {
| ^^^^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:168:19
+ --> $DIR/excessive_nesting.rs:169:19
|
LL | true => {{{{}}}},
| ^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:169:20
+ --> $DIR/excessive_nesting.rs:170:20
|
LL | false => {{{{}}}},
| ^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:174:13
+ --> $DIR/excessive_nesting.rs:175:13
|
LL | / {
LL | | {
@@ -328,31 +347,23 @@ LL | | }
LL | | }
| |_____________^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:184:27
+ --> $DIR/excessive_nesting.rs:185:27
|
LL | async fn c() -> u32 {{{{{{{0}}}}}}}
| ^^^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
-
-error: this block is too nested
- --> $DIR/excessive_nesting.rs:184:28
- |
-LL | async fn c() -> u32 {{{{{{{0}}}}}}}
- | ^^^^^^^^^
- |
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:190:7
+ --> $DIR/excessive_nesting.rs:191:7
|
LL | {{{{b().await}}}};
| ^^^^^^^^^^^^^
|
- = help: try refactoring your code, extraction is often both easier to read and less nested
+ = help: try refactoring your code to minimize nesting
error: aborting due to 40 previous errors
From 493a23e957f9d639c13bddb19937bd19cbc6cfe2 Mon Sep 17 00:00:00 2001
From: Centri3 <114838443+Centri3@users.noreply.github.com>
Date: Sat, 6 May 2023 18:43:53 -0500
Subject: [PATCH 06/10] check non-inline modules, ignore all macros
---
clippy_lints/src/excessive_nesting.rs | 24 +++-
.../excessive_nesting/auxiliary/mod.rs | 16 +++
.../excessive_nesting/excessive_nesting.rs | 6 +-
.../excessive_nesting.stderr | 118 +++++++++---------
4 files changed, 98 insertions(+), 66 deletions(-)
create mode 100644 tests/ui-toml/excessive_nesting/auxiliary/mod.rs
diff --git a/clippy_lints/src/excessive_nesting.rs b/clippy_lints/src/excessive_nesting.rs
index a133bdc2edf0..5ade713f8164 100644
--- a/clippy_lints/src/excessive_nesting.rs
+++ b/clippy_lints/src/excessive_nesting.rs
@@ -11,7 +11,7 @@ use rustc_span::Span;
declare_clippy_lint! {
/// ### What it does
///
- /// Checks for blocks which are indented beyond a certain threshold.
+ /// Checks for blocks which are nested beyond a certain threshold.
///
/// ### Why is this bad?
///
@@ -106,6 +106,11 @@ impl NestingVisitor<'_, '_> {
impl<'conf, 'cx> Visitor<'_> for NestingVisitor<'conf, 'cx> {
fn visit_block(&mut self, block: &Block) {
+ // TODO: Probably not necessary, since any block would already be ignored by the check in visit_item
+ if block.span.from_expansion() {
+ return;
+ }
+
self.nest_level += 1;
if !self.check_indent(block.span) {
@@ -116,6 +121,10 @@ impl<'conf, 'cx> Visitor<'_> for NestingVisitor<'conf, 'cx> {
}
fn visit_item(&mut self, item: &Item) {
+ if item.span.from_expansion() {
+ return;
+ }
+
match &item.kind {
ItemKind::Trait(_) | ItemKind::Impl(_) | ItemKind::Mod(.., ModKind::Loaded(_, Inline::Yes, _)) => {
self.nest_level += 1;
@@ -126,10 +135,15 @@ impl<'conf, 'cx> Visitor<'_> for NestingVisitor<'conf, 'cx> {
self.nest_level -= 1;
},
- // Mod: Don't visit non-inline modules
- // ForeignMod: I don't think this is necessary, but just incase let's not take any chances (don't want to
- // cause any false positives)
- ItemKind::Mod(..) | ItemKind::ForeignMod(..) => {},
+ // Reset nesting level for non-inline modules (since these are in another file)
+ ItemKind::Mod(..) => walk_item(
+ &mut NestingVisitor {
+ conf: self.conf,
+ cx: self.cx,
+ nest_level: 0,
+ },
+ item,
+ ),
_ => walk_item(self, item),
}
}
diff --git a/tests/ui-toml/excessive_nesting/auxiliary/mod.rs b/tests/ui-toml/excessive_nesting/auxiliary/mod.rs
new file mode 100644
index 000000000000..967b3af3bcad
--- /dev/null
+++ b/tests/ui-toml/excessive_nesting/auxiliary/mod.rs
@@ -0,0 +1,16 @@
+#![rustfmt::skip]
+
+mod a {
+ mod b {
+ mod c {
+ mod d {
+ mod e {}
+ }
+ }
+ }
+}
+
+fn main() {
+ // this should lint
+ {{{}}}
+}
diff --git a/tests/ui-toml/excessive_nesting/excessive_nesting.rs b/tests/ui-toml/excessive_nesting/excessive_nesting.rs
index 62cf51258160..3e3ac7c923ac 100644
--- a/tests/ui-toml/excessive_nesting/excessive_nesting.rs
+++ b/tests/ui-toml/excessive_nesting/excessive_nesting.rs
@@ -10,6 +10,8 @@
#![warn(clippy::excessive_nesting)]
#![allow(clippy::collapsible_if)]
+mod auxiliary;
+
#[macro_use]
extern crate macro_rules;
@@ -39,7 +41,7 @@ macro_rules! xx {
{
{
{
- println!("ehe");
+ println!("ehe"); // should not lint
}
}
}
@@ -135,7 +137,7 @@ fn main() {
})();
excessive_nesting!(); // ensure this isn't linted in external macros
- xx!();
+ xx!(); // ensure this is never linted
let boo = true;
!{boo as u32 + !{boo as u32 + !{boo as u32}}};
diff --git a/tests/ui-toml/excessive_nesting/excessive_nesting.stderr b/tests/ui-toml/excessive_nesting/excessive_nesting.stderr
index c98c6fefe010..524282d11f78 100644
--- a/tests/ui-toml/excessive_nesting/excessive_nesting.stderr
+++ b/tests/ui-toml/excessive_nesting/excessive_nesting.stderr
@@ -1,5 +1,24 @@
error: this block is too nested
- --> $DIR/excessive_nesting.rs:19:21
+ --> $DIR/auxiliary/mod.rs:6:13
+ |
+LL | / mod d {
+LL | | mod e {}
+LL | | }
+ | |_____________^
+ |
+ = help: try refactoring your code to minimize nesting
+ = note: `-D clippy::excessive-nesting` implied by `-D warnings`
+
+error: this block is too nested
+ --> $DIR/auxiliary/mod.rs:15:7
+ |
+LL | {{{}}}
+ | ^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:21:21
|
LL | let z = {
| _____________________^
@@ -9,10 +28,9 @@ LL | | };
| |_____________^
|
= help: try refactoring your code to minimize nesting
- = note: `-D clippy::excessive-nesting` implied by `-D warnings`
error: this block is too nested
- --> $DIR/excessive_nesting.rs:63:24
+ --> $DIR/excessive_nesting.rs:65:24
|
LL | pub fn b() {
| ________________________^
@@ -27,7 +45,7 @@ LL | | }
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:79:21
+ --> $DIR/excessive_nesting.rs:81:21
|
LL | fn cc() {
| _____________________^
@@ -38,7 +56,7 @@ LL | | }
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:83:21
+ --> $DIR/excessive_nesting.rs:85:21
|
LL | let x = { 1 }; // warning
| ^^^^^
@@ -46,7 +64,7 @@ LL | let x = { 1 }; // warning
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:93:13
+ --> $DIR/excessive_nesting.rs:95:13
|
LL | / pub mod d {
LL | | pub mod e {
@@ -58,7 +76,7 @@ LL | | } // only warning should be here
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:107:17
+ --> $DIR/excessive_nesting.rs:109:17
|
LL | a_but_not({{{{{{{{0}}}}}}}});
| ^^^^^^^^^^^^^
@@ -66,7 +84,7 @@ LL | a_but_not({{{{{{{{0}}}}}}}});
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:108:11
+ --> $DIR/excessive_nesting.rs:110:11
|
LL | a.a({{{{{{{{{0}}}}}}}}});
| ^^^^^^^^^^^^^^^
@@ -74,7 +92,7 @@ LL | a.a({{{{{{{{{0}}}}}}}}});
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:109:11
+ --> $DIR/excessive_nesting.rs:111:11
|
LL | (0, {{{{{{{1}}}}}}});
| ^^^^^^^^^^^
@@ -82,7 +100,7 @@ LL | (0, {{{{{{{1}}}}}}});
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:113:21
+ --> $DIR/excessive_nesting.rs:115:21
|
LL | if true {
| _____________________^
@@ -97,7 +115,7 @@ LL | | }
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:125:25
+ --> $DIR/excessive_nesting.rs:127:25
|
LL | let y = (|| {
| _________________________^
@@ -112,25 +130,7 @@ LL | | })();
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:33:13
- |
-LL | / {
-LL | | {
-LL | | {
-LL | | {
-... |
-LL | | }
-LL | | }
- | |_____________^
-...
-LL | xx!();
- | ----- in this macro invocation
- |
- = help: try refactoring your code to minimize nesting
- = note: this error originates in the macro `xx` (in Nightly builds, run with -Z macro-backtrace for more info)
-
-error: this block is too nested
- --> $DIR/excessive_nesting.rs:140:36
+ --> $DIR/excessive_nesting.rs:142:36
|
LL | !{boo as u32 + !{boo as u32 + !{boo as u32}}};
| ^^^^^^^^^^^^
@@ -138,7 +138,7 @@ LL | !{boo as u32 + !{boo as u32 + !{boo as u32}}};
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:144:12
+ --> $DIR/excessive_nesting.rs:146:12
|
LL | y += {{{{{5}}}}};
| ^^^^^^^
@@ -146,7 +146,7 @@ LL | y += {{{{{5}}}}};
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:145:19
+ --> $DIR/excessive_nesting.rs:147:19
|
LL | let z = y + {{{{{{{{{5}}}}}}}}};
| ^^^^^^^^^^^^^^^
@@ -154,7 +154,7 @@ LL | let z = y + {{{{{{{{{5}}}}}}}}};
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:146:11
+ --> $DIR/excessive_nesting.rs:148:11
|
LL | [0, {{{{{{{{{{0}}}}}}}}}}];
| ^^^^^^^^^^^^^^^^^
@@ -162,7 +162,7 @@ LL | [0, {{{{{{{{{{0}}}}}}}}}}];
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:147:24
+ --> $DIR/excessive_nesting.rs:149:24
|
LL | let mut xx = [0; {{{{{{{{100}}}}}}}}];
| ^^^^^^^^^^^^^^^
@@ -170,7 +170,7 @@ LL | let mut xx = [0; {{{{{{{{100}}}}}}}}];
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:148:10
+ --> $DIR/excessive_nesting.rs:150:10
|
LL | xx[{{{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}}}];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -178,7 +178,7 @@ LL | xx[{{{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}}}];
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:149:12
+ --> $DIR/excessive_nesting.rs:151:12
|
LL | &mut {{{{{{{{{{y}}}}}}}}}};
| ^^^^^^^^^^^^^^^^^
@@ -186,7 +186,7 @@ LL | &mut {{{{{{{{{{y}}}}}}}}}};
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:151:16
+ --> $DIR/excessive_nesting.rs:153:16
|
LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}}
| ^^^^^^
@@ -194,7 +194,7 @@ LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}}
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:151:27
+ --> $DIR/excessive_nesting.rs:153:27
|
LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}}
| ^^^^^^^^^^^^
@@ -202,7 +202,7 @@ LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}}
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:153:27
+ --> $DIR/excessive_nesting.rs:155:27
|
LL | while let Some(i) = {{{{{{Some(1)}}}}}} {{{{{{{}}}}}}}
| ^^^^^^^^^^^^^^^
@@ -210,7 +210,7 @@ LL | while let Some(i) = {{{{{{Some(1)}}}}}} {{{{{{{}}}}}}}
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:153:47
+ --> $DIR/excessive_nesting.rs:155:47
|
LL | while let Some(i) = {{{{{{Some(1)}}}}}} {{{{{{{}}}}}}}
| ^^^^^^^^^^
@@ -218,7 +218,7 @@ LL | while let Some(i) = {{{{{{Some(1)}}}}}} {{{{{{{}}}}}}}
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:155:13
+ --> $DIR/excessive_nesting.rs:157:13
|
LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}}
| ^^^^^^^^^^^^^^^^
@@ -226,7 +226,7 @@ LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}}
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:155:34
+ --> $DIR/excessive_nesting.rs:157:34
|
LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}}
| ^^^^^^^^^^^^^^
@@ -234,7 +234,7 @@ LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}}
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:157:22
+ --> $DIR/excessive_nesting.rs:159:22
|
LL | let d = D { d: {{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}} };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -242,7 +242,7 @@ LL | let d = D { d: {{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}} };
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:159:7
+ --> $DIR/excessive_nesting.rs:161:7
|
LL | {{{{1;}}}}..{{{{{{3}}}}}};
| ^^^^^^
@@ -250,7 +250,7 @@ LL | {{{{1;}}}}..{{{{{{3}}}}}};
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:159:19
+ --> $DIR/excessive_nesting.rs:161:19
|
LL | {{{{1;}}}}..{{{{{{3}}}}}};
| ^^^^^^^^^
@@ -258,7 +258,7 @@ LL | {{{{1;}}}}..{{{{{{3}}}}}};
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:160:7
+ --> $DIR/excessive_nesting.rs:162:7
|
LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}};
| ^^^^^^
@@ -266,7 +266,7 @@ LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}};
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:160:20
+ --> $DIR/excessive_nesting.rs:162:20
|
LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}};
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -274,7 +274,7 @@ LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}};
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:161:9
+ --> $DIR/excessive_nesting.rs:163:9
|
LL | ..{{{{{{{5}}}}}}};
| ^^^^^^^^^^^
@@ -282,7 +282,7 @@ LL | ..{{{{{{{5}}}}}}};
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:162:10
+ --> $DIR/excessive_nesting.rs:164:10
|
LL | ..={{{{{3}}}}};
| ^^^^^^^
@@ -290,7 +290,7 @@ LL | ..={{{{{3}}}}};
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:163:7
+ --> $DIR/excessive_nesting.rs:165:7
|
LL | {{{{{1;}}}}}..;
| ^^^^^^^^
@@ -298,7 +298,7 @@ LL | {{{{{1;}}}}}..;
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:165:19
+ --> $DIR/excessive_nesting.rs:167:19
|
LL | loop { break {{{{1}}}} };
| ^^^^^^^
@@ -306,7 +306,7 @@ LL | loop { break {{{{1}}}} };
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:166:12
+ --> $DIR/excessive_nesting.rs:168:12
|
LL | loop {{{{{{}}}}}}
| ^^^^^^^^
@@ -314,7 +314,7 @@ LL | loop {{{{{{}}}}}}
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:168:13
+ --> $DIR/excessive_nesting.rs:170:13
|
LL | match {{{{{{true}}}}}} {
| ^^^^^^^^^^^^
@@ -322,7 +322,7 @@ LL | match {{{{{{true}}}}}} {
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:169:19
+ --> $DIR/excessive_nesting.rs:171:19
|
LL | true => {{{{}}}},
| ^^^^
@@ -330,7 +330,7 @@ LL | true => {{{{}}}},
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:170:20
+ --> $DIR/excessive_nesting.rs:172:20
|
LL | false => {{{{}}}},
| ^^^^
@@ -338,7 +338,7 @@ LL | false => {{{{}}}},
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:175:13
+ --> $DIR/excessive_nesting.rs:177:13
|
LL | / {
LL | | {
@@ -350,7 +350,7 @@ LL | | }
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:185:27
+ --> $DIR/excessive_nesting.rs:187:27
|
LL | async fn c() -> u32 {{{{{{{0}}}}}}}
| ^^^^^^^^^^^
@@ -358,12 +358,12 @@ LL | async fn c() -> u32 {{{{{{{0}}}}}}}
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:191:7
+ --> $DIR/excessive_nesting.rs:193:7
|
LL | {{{{b().await}}}};
| ^^^^^^^^^^^^^
|
= help: try refactoring your code to minimize nesting
-error: aborting due to 40 previous errors
+error: aborting due to 41 previous errors
From 378d77584acdd4864bf495ce67c932c0f422e6f4 Mon Sep 17 00:00:00 2001
From: Centri3 <114838443+Centri3@users.noreply.github.com>
Date: Mon, 15 May 2023 15:22:59 -0500
Subject: [PATCH 07/10] work with lint attributes
---
clippy_lints/src/excessive_nesting.rs | 54 ++-
clippy_lints/src/lib.rs | 1 +
.../excessive_nesting/{ => below}/clippy.toml | 0
.../excessive_nesting/default/clippy.toml | 1 +
.../excessive_nesting.below.stderr | 369 ++++++++++++++++++
.../excessive_nesting.default.stderr | 43 ++
.../excessive_nesting/excessive_nesting.rs | 6 +
.../excessive_nesting.stderr | 70 ++--
8 files changed, 492 insertions(+), 52 deletions(-)
rename tests/ui-toml/excessive_nesting/{ => below}/clippy.toml (100%)
create mode 100644 tests/ui-toml/excessive_nesting/default/clippy.toml
create mode 100644 tests/ui-toml/excessive_nesting/excessive_nesting.below.stderr
create mode 100644 tests/ui-toml/excessive_nesting/excessive_nesting.default.stderr
diff --git a/clippy_lints/src/excessive_nesting.rs b/clippy_lints/src/excessive_nesting.rs
index 5ade713f8164..f1aafa3cbc38 100644
--- a/clippy_lints/src/excessive_nesting.rs
+++ b/clippy_lints/src/excessive_nesting.rs
@@ -1,7 +1,8 @@
use clippy_utils::diagnostics::span_lint_and_help;
use rustc_ast::{
+ node_id::NodeSet,
visit::{walk_block, walk_item, Visitor},
- Block, Crate, Inline, Item, ItemKind, ModKind,
+ Block, Crate, Inline, Item, ItemKind, ModKind, NodeId,
};
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
@@ -52,6 +53,10 @@ declare_clippy_lint! {
/// }
/// }
/// ```
+ /// lib.rs:
+ /// ```rust,ignore
+ /// pub mod a;
+ /// ```
#[clippy::version = "1.70.0"]
pub EXCESSIVE_NESTING,
restriction,
@@ -59,16 +64,31 @@ declare_clippy_lint! {
}
impl_lint_pass!(ExcessiveNesting => [EXCESSIVE_NESTING]);
-#[derive(Clone, Copy)]
+#[derive(Clone)]
pub struct ExcessiveNesting {
pub excessive_nesting_threshold: u64,
+ pub nodes: NodeSet,
+}
+
+impl ExcessiveNesting {
+ pub fn check_node_id(&self, cx: &EarlyContext<'_>, span: Span, node_id: NodeId) {
+ if self.nodes.contains(&node_id) {
+ span_lint_and_help(
+ cx,
+ EXCESSIVE_NESTING,
+ span,
+ "this block is too nested",
+ None,
+ "try refactoring your code to minimize nesting",
+ );
+ }
+ }
}
impl EarlyLintPass for ExcessiveNesting {
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
- let conf = self;
let mut visitor = NestingVisitor {
- conf,
+ conf: self,
cx,
nest_level: 0,
};
@@ -77,25 +97,26 @@ impl EarlyLintPass for ExcessiveNesting {
visitor.visit_item(item);
}
}
+
+ fn check_block(&mut self, cx: &EarlyContext<'_>, block: &Block) {
+ self.check_node_id(cx, block.span, block.id);
+ }
+
+ fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
+ self.check_node_id(cx, item.span, item.id);
+ }
}
struct NestingVisitor<'conf, 'cx> {
- conf: &'conf ExcessiveNesting,
+ conf: &'conf mut ExcessiveNesting,
cx: &'cx EarlyContext<'cx>,
nest_level: u64,
}
impl NestingVisitor<'_, '_> {
- fn check_indent(&self, span: Span) -> bool {
+ fn check_indent(&mut self, span: Span, id: NodeId) -> bool {
if self.nest_level > self.conf.excessive_nesting_threshold && !in_external_macro(self.cx.sess(), span) {
- span_lint_and_help(
- self.cx,
- EXCESSIVE_NESTING,
- span,
- "this block is too nested",
- None,
- "try refactoring your code to minimize nesting",
- );
+ self.conf.nodes.insert(id);
return true;
}
@@ -106,14 +127,13 @@ impl NestingVisitor<'_, '_> {
impl<'conf, 'cx> Visitor<'_> for NestingVisitor<'conf, 'cx> {
fn visit_block(&mut self, block: &Block) {
- // TODO: Probably not necessary, since any block would already be ignored by the check in visit_item
if block.span.from_expansion() {
return;
}
self.nest_level += 1;
- if !self.check_indent(block.span) {
+ if !self.check_indent(block.span, block.id) {
walk_block(self, block);
}
@@ -129,7 +149,7 @@ impl<'conf, 'cx> Visitor<'_> for NestingVisitor<'conf, 'cx> {
ItemKind::Trait(_) | ItemKind::Impl(_) | ItemKind::Mod(.., ModKind::Loaded(_, Inline::Yes, _)) => {
self.nest_level += 1;
- if !self.check_indent(item.span) {
+ if !self.check_indent(item.span, item.id) {
walk_item(self, item);
}
diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs
index 202b4709ad41..189096d7ba99 100644
--- a/clippy_lints/src/lib.rs
+++ b/clippy_lints/src/lib.rs
@@ -1012,6 +1012,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_early_pass(move || {
Box::new(excessive_nesting::ExcessiveNesting {
excessive_nesting_threshold,
+ nodes: rustc_ast::node_id::NodeSet::new(),
})
});
store.register_late_pass(|_| Box::new(items_after_test_module::ItemsAfterTestModule));
diff --git a/tests/ui-toml/excessive_nesting/clippy.toml b/tests/ui-toml/excessive_nesting/below/clippy.toml
similarity index 100%
rename from tests/ui-toml/excessive_nesting/clippy.toml
rename to tests/ui-toml/excessive_nesting/below/clippy.toml
diff --git a/tests/ui-toml/excessive_nesting/default/clippy.toml b/tests/ui-toml/excessive_nesting/default/clippy.toml
new file mode 100644
index 000000000000..b01e482e4859
--- /dev/null
+++ b/tests/ui-toml/excessive_nesting/default/clippy.toml
@@ -0,0 +1 @@
+excessive-nesting-threshold = 10
diff --git a/tests/ui-toml/excessive_nesting/excessive_nesting.below.stderr b/tests/ui-toml/excessive_nesting/excessive_nesting.below.stderr
new file mode 100644
index 000000000000..1f069437dfe5
--- /dev/null
+++ b/tests/ui-toml/excessive_nesting/excessive_nesting.below.stderr
@@ -0,0 +1,369 @@
+error: this block is too nested
+ --> $DIR/auxiliary/mod.rs:6:13
+ |
+LL | / mod d {
+LL | | mod e {}
+LL | | }
+ | |_____________^
+ |
+ = help: try refactoring your code to minimize nesting
+ = note: `-D clippy::excessive-nesting` implied by `-D warnings`
+
+error: this block is too nested
+ --> $DIR/auxiliary/mod.rs:15:7
+ |
+LL | {{{}}}
+ | ^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:24:21
+ |
+LL | let z = {
+ | _____________________^
+LL | | let w = { 3 };
+LL | | w
+LL | | };
+ | |_____________^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:68:24
+ |
+LL | pub fn b() {
+ | ________________________^
+LL | | struct C;
+LL | |
+LL | | impl C {
+LL | | pub fn c() {}
+LL | | }
+LL | | }
+ | |_____________^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:84:21
+ |
+LL | fn cc() {
+ | _____________________^
+LL | | let x = { 1 }; // not a warning, but cc is
+LL | | }
+ | |_____________^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:88:21
+ |
+LL | let x = { 1 }; // warning
+ | ^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:101:13
+ |
+LL | / pub mod d {
+LL | | pub mod e {
+LL | | pub mod f {}
+LL | | } // not here
+LL | | } // only warning should be here
+ | |_____________^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:115:17
+ |
+LL | a_but_not({{{{{{{{0}}}}}}}});
+ | ^^^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:116:11
+ |
+LL | a.a({{{{{{{{{0}}}}}}}}});
+ | ^^^^^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:117:11
+ |
+LL | (0, {{{{{{{1}}}}}}});
+ | ^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:121:21
+ |
+LL | if true {
+ | _____________________^
+LL | | if true {
+LL | | if true {
+LL | |
+LL | | }
+LL | | }
+LL | | }
+ | |_____________^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:133:25
+ |
+LL | let y = (|| {
+ | _________________________^
+LL | | let z = (|| {
+LL | | let w = { 3 };
+LL | | w
+LL | | })();
+LL | | z
+LL | | })();
+ | |_____________^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:148:36
+ |
+LL | !{boo as u32 + !{boo as u32 + !{boo as u32}}};
+ | ^^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:152:12
+ |
+LL | y += {{{{{5}}}}};
+ | ^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:153:19
+ |
+LL | let z = y + {{{{{{{{{5}}}}}}}}};
+ | ^^^^^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:154:11
+ |
+LL | [0, {{{{{{{{{{0}}}}}}}}}}];
+ | ^^^^^^^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:155:24
+ |
+LL | let mut xx = [0; {{{{{{{{100}}}}}}}}];
+ | ^^^^^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:156:10
+ |
+LL | xx[{{{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}}}];
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:157:12
+ |
+LL | &mut {{{{{{{{{{y}}}}}}}}}};
+ | ^^^^^^^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:159:16
+ |
+LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}}
+ | ^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:159:27
+ |
+LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}}
+ | ^^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:161:27
+ |
+LL | while let Some(i) = {{{{{{Some(1)}}}}}} {{{{{{{}}}}}}}
+ | ^^^^^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:161:47
+ |
+LL | while let Some(i) = {{{{{{Some(1)}}}}}} {{{{{{{}}}}}}}
+ | ^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:163:13
+ |
+LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}}
+ | ^^^^^^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:163:34
+ |
+LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}}
+ | ^^^^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:165:22
+ |
+LL | let d = D { d: {{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}} };
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:167:7
+ |
+LL | {{{{1;}}}}..{{{{{{3}}}}}};
+ | ^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:167:19
+ |
+LL | {{{{1;}}}}..{{{{{{3}}}}}};
+ | ^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:168:7
+ |
+LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}};
+ | ^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:168:20
+ |
+LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}};
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:169:9
+ |
+LL | ..{{{{{{{5}}}}}}};
+ | ^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:170:10
+ |
+LL | ..={{{{{3}}}}};
+ | ^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:171:7
+ |
+LL | {{{{{1;}}}}}..;
+ | ^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:173:19
+ |
+LL | loop { break {{{{1}}}} };
+ | ^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:174:12
+ |
+LL | loop {{{{{{}}}}}}
+ | ^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:176:13
+ |
+LL | match {{{{{{true}}}}}} {
+ | ^^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:177:19
+ |
+LL | true => {{{{}}}},
+ | ^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:178:20
+ |
+LL | false => {{{{}}}},
+ | ^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:183:13
+ |
+LL | / {
+LL | | {
+LL | | println!("warning! :)");
+LL | | }
+LL | | }
+ | |_____________^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:193:27
+ |
+LL | async fn c() -> u32 {{{{{{{0}}}}}}}
+ | ^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:199:7
+ |
+LL | {{{{b().await}}}};
+ | ^^^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: aborting due to 41 previous errors
+
diff --git a/tests/ui-toml/excessive_nesting/excessive_nesting.default.stderr b/tests/ui-toml/excessive_nesting/excessive_nesting.default.stderr
new file mode 100644
index 000000000000..02fa2cdcf9fa
--- /dev/null
+++ b/tests/ui-toml/excessive_nesting/excessive_nesting.default.stderr
@@ -0,0 +1,43 @@
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:154:18
+ |
+LL | [0, {{{{{{{{{{0}}}}}}}}}}];
+ | ^^^
+ |
+ = help: try refactoring your code to minimize nesting
+ = note: `-D clippy::excessive-nesting` implied by `-D warnings`
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:156:17
+ |
+LL | xx[{{{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}}}];
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:157:19
+ |
+LL | &mut {{{{{{{{{{y}}}}}}}}}};
+ | ^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:165:29
+ |
+LL | let d = D { d: {{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}} };
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: this block is too nested
+ --> $DIR/excessive_nesting.rs:168:27
+ |
+LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}};
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+ |
+ = help: try refactoring your code to minimize nesting
+
+error: aborting due to 5 previous errors
+
diff --git a/tests/ui-toml/excessive_nesting/excessive_nesting.rs b/tests/ui-toml/excessive_nesting/excessive_nesting.rs
index 3e3ac7c923ac..745031db1eb9 100644
--- a/tests/ui-toml/excessive_nesting/excessive_nesting.rs
+++ b/tests/ui-toml/excessive_nesting/excessive_nesting.rs
@@ -1,4 +1,7 @@
//@aux-build:macro_rules.rs
+//@revisions: below default
+//@[below] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/excessive_nesting/below
+//@[default] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/excessive_nesting/default
#![rustfmt::skip]
#![feature(custom_inner_attributes)]
#![allow(unused)]
@@ -87,6 +90,9 @@ trait Lol {
}
}
+#[allow(clippy::excessive_nesting)]
+fn l() {{{{{{{{{}}}}}}}}}
+
use a::{b::{c::{d::{e::{f::{}}}}}}; // should not lint
pub mod a {
diff --git a/tests/ui-toml/excessive_nesting/excessive_nesting.stderr b/tests/ui-toml/excessive_nesting/excessive_nesting.stderr
index 524282d11f78..f1db396fe480 100644
--- a/tests/ui-toml/excessive_nesting/excessive_nesting.stderr
+++ b/tests/ui-toml/excessive_nesting/excessive_nesting.stderr
@@ -64,7 +64,7 @@ LL | let x = { 1 }; // warning
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:95:13
+ --> $DIR/excessive_nesting.rs:98:13
|
LL | / pub mod d {
LL | | pub mod e {
@@ -76,7 +76,7 @@ LL | | } // only warning should be here
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:109:17
+ --> $DIR/excessive_nesting.rs:112:17
|
LL | a_but_not({{{{{{{{0}}}}}}}});
| ^^^^^^^^^^^^^
@@ -84,7 +84,7 @@ LL | a_but_not({{{{{{{{0}}}}}}}});
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:110:11
+ --> $DIR/excessive_nesting.rs:113:11
|
LL | a.a({{{{{{{{{0}}}}}}}}});
| ^^^^^^^^^^^^^^^
@@ -92,7 +92,7 @@ LL | a.a({{{{{{{{{0}}}}}}}}});
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:111:11
+ --> $DIR/excessive_nesting.rs:114:11
|
LL | (0, {{{{{{{1}}}}}}});
| ^^^^^^^^^^^
@@ -100,7 +100,7 @@ LL | (0, {{{{{{{1}}}}}}});
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:115:21
+ --> $DIR/excessive_nesting.rs:118:21
|
LL | if true {
| _____________________^
@@ -115,7 +115,7 @@ LL | | }
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:127:25
+ --> $DIR/excessive_nesting.rs:130:25
|
LL | let y = (|| {
| _________________________^
@@ -130,7 +130,7 @@ LL | | })();
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:142:36
+ --> $DIR/excessive_nesting.rs:145:36
|
LL | !{boo as u32 + !{boo as u32 + !{boo as u32}}};
| ^^^^^^^^^^^^
@@ -138,7 +138,7 @@ LL | !{boo as u32 + !{boo as u32 + !{boo as u32}}};
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:146:12
+ --> $DIR/excessive_nesting.rs:149:12
|
LL | y += {{{{{5}}}}};
| ^^^^^^^
@@ -146,7 +146,7 @@ LL | y += {{{{{5}}}}};
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:147:19
+ --> $DIR/excessive_nesting.rs:150:19
|
LL | let z = y + {{{{{{{{{5}}}}}}}}};
| ^^^^^^^^^^^^^^^
@@ -154,7 +154,7 @@ LL | let z = y + {{{{{{{{{5}}}}}}}}};
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:148:11
+ --> $DIR/excessive_nesting.rs:151:11
|
LL | [0, {{{{{{{{{{0}}}}}}}}}}];
| ^^^^^^^^^^^^^^^^^
@@ -162,7 +162,7 @@ LL | [0, {{{{{{{{{{0}}}}}}}}}}];
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:149:24
+ --> $DIR/excessive_nesting.rs:152:24
|
LL | let mut xx = [0; {{{{{{{{100}}}}}}}}];
| ^^^^^^^^^^^^^^^
@@ -170,7 +170,7 @@ LL | let mut xx = [0; {{{{{{{{100}}}}}}}}];
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:150:10
+ --> $DIR/excessive_nesting.rs:153:10
|
LL | xx[{{{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}}}];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -178,7 +178,7 @@ LL | xx[{{{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}}}];
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:151:12
+ --> $DIR/excessive_nesting.rs:154:12
|
LL | &mut {{{{{{{{{{y}}}}}}}}}};
| ^^^^^^^^^^^^^^^^^
@@ -186,7 +186,7 @@ LL | &mut {{{{{{{{{{y}}}}}}}}}};
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:153:16
+ --> $DIR/excessive_nesting.rs:156:16
|
LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}}
| ^^^^^^
@@ -194,7 +194,7 @@ LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}}
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:153:27
+ --> $DIR/excessive_nesting.rs:156:27
|
LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}}
| ^^^^^^^^^^^^
@@ -202,7 +202,7 @@ LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}}
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:155:27
+ --> $DIR/excessive_nesting.rs:158:27
|
LL | while let Some(i) = {{{{{{Some(1)}}}}}} {{{{{{{}}}}}}}
| ^^^^^^^^^^^^^^^
@@ -210,7 +210,7 @@ LL | while let Some(i) = {{{{{{Some(1)}}}}}} {{{{{{{}}}}}}}
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:155:47
+ --> $DIR/excessive_nesting.rs:158:47
|
LL | while let Some(i) = {{{{{{Some(1)}}}}}} {{{{{{{}}}}}}}
| ^^^^^^^^^^
@@ -218,7 +218,7 @@ LL | while let Some(i) = {{{{{{Some(1)}}}}}} {{{{{{{}}}}}}}
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:157:13
+ --> $DIR/excessive_nesting.rs:160:13
|
LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}}
| ^^^^^^^^^^^^^^^^
@@ -226,7 +226,7 @@ LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}}
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:157:34
+ --> $DIR/excessive_nesting.rs:160:34
|
LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}}
| ^^^^^^^^^^^^^^
@@ -234,7 +234,7 @@ LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}}
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:159:22
+ --> $DIR/excessive_nesting.rs:162:22
|
LL | let d = D { d: {{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}} };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -242,7 +242,7 @@ LL | let d = D { d: {{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}} };
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:161:7
+ --> $DIR/excessive_nesting.rs:164:7
|
LL | {{{{1;}}}}..{{{{{{3}}}}}};
| ^^^^^^
@@ -250,7 +250,7 @@ LL | {{{{1;}}}}..{{{{{{3}}}}}};
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:161:19
+ --> $DIR/excessive_nesting.rs:164:19
|
LL | {{{{1;}}}}..{{{{{{3}}}}}};
| ^^^^^^^^^
@@ -258,7 +258,7 @@ LL | {{{{1;}}}}..{{{{{{3}}}}}};
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:162:7
+ --> $DIR/excessive_nesting.rs:165:7
|
LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}};
| ^^^^^^
@@ -266,7 +266,7 @@ LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}};
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:162:20
+ --> $DIR/excessive_nesting.rs:165:20
|
LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}};
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -274,7 +274,7 @@ LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}};
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:163:9
+ --> $DIR/excessive_nesting.rs:166:9
|
LL | ..{{{{{{{5}}}}}}};
| ^^^^^^^^^^^
@@ -282,7 +282,7 @@ LL | ..{{{{{{{5}}}}}}};
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:164:10
+ --> $DIR/excessive_nesting.rs:167:10
|
LL | ..={{{{{3}}}}};
| ^^^^^^^
@@ -290,7 +290,7 @@ LL | ..={{{{{3}}}}};
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:165:7
+ --> $DIR/excessive_nesting.rs:168:7
|
LL | {{{{{1;}}}}}..;
| ^^^^^^^^
@@ -298,7 +298,7 @@ LL | {{{{{1;}}}}}..;
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:167:19
+ --> $DIR/excessive_nesting.rs:170:19
|
LL | loop { break {{{{1}}}} };
| ^^^^^^^
@@ -306,7 +306,7 @@ LL | loop { break {{{{1}}}} };
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:168:12
+ --> $DIR/excessive_nesting.rs:171:12
|
LL | loop {{{{{{}}}}}}
| ^^^^^^^^
@@ -314,7 +314,7 @@ LL | loop {{{{{{}}}}}}
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:170:13
+ --> $DIR/excessive_nesting.rs:173:13
|
LL | match {{{{{{true}}}}}} {
| ^^^^^^^^^^^^
@@ -322,7 +322,7 @@ LL | match {{{{{{true}}}}}} {
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:171:19
+ --> $DIR/excessive_nesting.rs:174:19
|
LL | true => {{{{}}}},
| ^^^^
@@ -330,7 +330,7 @@ LL | true => {{{{}}}},
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:172:20
+ --> $DIR/excessive_nesting.rs:175:20
|
LL | false => {{{{}}}},
| ^^^^
@@ -338,7 +338,7 @@ LL | false => {{{{}}}},
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:177:13
+ --> $DIR/excessive_nesting.rs:180:13
|
LL | / {
LL | | {
@@ -350,7 +350,7 @@ LL | | }
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:187:27
+ --> $DIR/excessive_nesting.rs:190:27
|
LL | async fn c() -> u32 {{{{{{{0}}}}}}}
| ^^^^^^^^^^^
@@ -358,7 +358,7 @@ LL | async fn c() -> u32 {{{{{{{0}}}}}}}
= help: try refactoring your code to minimize nesting
error: this block is too nested
- --> $DIR/excessive_nesting.rs:193:7
+ --> $DIR/excessive_nesting.rs:196:7
|
LL | {{{{b().await}}}};
| ^^^^^^^^^^^^^
From 725399a178158be33f0754c423b95b5f7f8b53ac Mon Sep 17 00:00:00 2001
From: Centri3 <114838443+Centri3@users.noreply.github.com>
Date: Tue, 30 May 2023 12:40:40 -0500
Subject: [PATCH 08/10] move to `complexity` but don't lint by default
---
book/src/lint_configuration.md | 2 +-
clippy_lints/src/excessive_nesting.rs | 13 +++---
clippy_lints/src/utils/conf.rs | 2 +-
.../excessive_nesting/default/clippy.toml | 2 +-
.../excessive_nesting.default.stderr | 43 -------------------
.../excessive_nesting/excessive_nesting.rs | 4 +-
...ow.stderr => excessive_nesting.set.stderr} | 0
.../{below => set}/clippy.toml | 0
8 files changed, 12 insertions(+), 54 deletions(-)
rename tests/ui-toml/excessive_nesting/{excessive_nesting.below.stderr => excessive_nesting.set.stderr} (100%)
rename tests/ui-toml/excessive_nesting/{below => set}/clippy.toml (100%)
diff --git a/book/src/lint_configuration.md b/book/src/lint_configuration.md
index 4fa6b81c0027..3c955c9e5748 100644
--- a/book/src/lint_configuration.md
+++ b/book/src/lint_configuration.md
@@ -161,7 +161,7 @@ The maximum cognitive complexity a function can have
## `excessive-nesting-threshold`
The maximum amount of nesting a block can reside in
-**Default Value:** `10` (`u64`)
+**Default Value:** `0` (`u64`)
---
**Affected lints:**
diff --git a/clippy_lints/src/excessive_nesting.rs b/clippy_lints/src/excessive_nesting.rs
index f1aafa3cbc38..1d68d7f94780 100644
--- a/clippy_lints/src/excessive_nesting.rs
+++ b/clippy_lints/src/excessive_nesting.rs
@@ -11,13 +11,12 @@ use rustc_span::Span;
declare_clippy_lint! {
/// ### What it does
- ///
/// Checks for blocks which are nested beyond a certain threshold.
///
- /// ### Why is this bad?
+ /// Note: Even though this lint is warn-by-default, it will only trigger if a maximum nesting level is defined in the clippy.toml file.
///
- /// It can severely hinder readability. The default is very generous; if you
- /// exceed this, it's a sign you should refactor.
+ /// ### Why is this bad?
+ /// It can severely hinder readability.
///
/// ### Example
/// An example clippy.toml configuration:
@@ -59,7 +58,7 @@ declare_clippy_lint! {
/// ```
#[clippy::version = "1.70.0"]
pub EXCESSIVE_NESTING,
- restriction,
+ complexity,
"checks for blocks nested beyond a certain threshold"
}
impl_lint_pass!(ExcessiveNesting => [EXCESSIVE_NESTING]);
@@ -115,7 +114,9 @@ struct NestingVisitor<'conf, 'cx> {
impl NestingVisitor<'_, '_> {
fn check_indent(&mut self, span: Span, id: NodeId) -> bool {
- if self.nest_level > self.conf.excessive_nesting_threshold && !in_external_macro(self.cx.sess(), span) {
+ let threshold = self.conf.excessive_nesting_threshold;
+
+ if threshold != 0 && self.nest_level > threshold && !in_external_macro(self.cx.sess(), span) {
self.conf.nodes.insert(id);
return true;
diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs
index cf0da266dc97..69143c3c7260 100644
--- a/clippy_lints/src/utils/conf.rs
+++ b/clippy_lints/src/utils/conf.rs
@@ -308,7 +308,7 @@ define_Conf! {
/// Lint: EXCESSIVE_NESTING.
///
/// The maximum amount of nesting a block can reside in
- (excessive_nesting_threshold: u64 = 10),
+ (excessive_nesting_threshold: u64 = 0),
/// DEPRECATED LINT: CYCLOMATIC_COMPLEXITY.
///
/// Use the Cognitive Complexity lint instead.
diff --git a/tests/ui-toml/excessive_nesting/default/clippy.toml b/tests/ui-toml/excessive_nesting/default/clippy.toml
index b01e482e4859..e8b115a0f7c6 100644
--- a/tests/ui-toml/excessive_nesting/default/clippy.toml
+++ b/tests/ui-toml/excessive_nesting/default/clippy.toml
@@ -1 +1 @@
-excessive-nesting-threshold = 10
+excessive-nesting-threshold = 0
diff --git a/tests/ui-toml/excessive_nesting/excessive_nesting.default.stderr b/tests/ui-toml/excessive_nesting/excessive_nesting.default.stderr
index 02fa2cdcf9fa..e69de29bb2d1 100644
--- a/tests/ui-toml/excessive_nesting/excessive_nesting.default.stderr
+++ b/tests/ui-toml/excessive_nesting/excessive_nesting.default.stderr
@@ -1,43 +0,0 @@
-error: this block is too nested
- --> $DIR/excessive_nesting.rs:154:18
- |
-LL | [0, {{{{{{{{{{0}}}}}}}}}}];
- | ^^^
- |
- = help: try refactoring your code to minimize nesting
- = note: `-D clippy::excessive-nesting` implied by `-D warnings`
-
-error: this block is too nested
- --> $DIR/excessive_nesting.rs:156:17
- |
-LL | xx[{{{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}}}];
- | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- |
- = help: try refactoring your code to minimize nesting
-
-error: this block is too nested
- --> $DIR/excessive_nesting.rs:157:19
- |
-LL | &mut {{{{{{{{{{y}}}}}}}}}};
- | ^^^
- |
- = help: try refactoring your code to minimize nesting
-
-error: this block is too nested
- --> $DIR/excessive_nesting.rs:165:29
- |
-LL | let d = D { d: {{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}} };
- | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- |
- = help: try refactoring your code to minimize nesting
-
-error: this block is too nested
- --> $DIR/excessive_nesting.rs:168:27
- |
-LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}};
- | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- |
- = help: try refactoring your code to minimize nesting
-
-error: aborting due to 5 previous errors
-
diff --git a/tests/ui-toml/excessive_nesting/excessive_nesting.rs b/tests/ui-toml/excessive_nesting/excessive_nesting.rs
index 745031db1eb9..5e436b97ee83 100644
--- a/tests/ui-toml/excessive_nesting/excessive_nesting.rs
+++ b/tests/ui-toml/excessive_nesting/excessive_nesting.rs
@@ -1,6 +1,6 @@
//@aux-build:macro_rules.rs
-//@revisions: below default
-//@[below] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/excessive_nesting/below
+//@revisions: set default
+//@[set] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/excessive_nesting/set
//@[default] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/excessive_nesting/default
#![rustfmt::skip]
#![feature(custom_inner_attributes)]
diff --git a/tests/ui-toml/excessive_nesting/excessive_nesting.below.stderr b/tests/ui-toml/excessive_nesting/excessive_nesting.set.stderr
similarity index 100%
rename from tests/ui-toml/excessive_nesting/excessive_nesting.below.stderr
rename to tests/ui-toml/excessive_nesting/excessive_nesting.set.stderr
diff --git a/tests/ui-toml/excessive_nesting/below/clippy.toml b/tests/ui-toml/excessive_nesting/set/clippy.toml
similarity index 100%
rename from tests/ui-toml/excessive_nesting/below/clippy.toml
rename to tests/ui-toml/excessive_nesting/set/clippy.toml
From 5da34559ee6a1cf34ddfc889513f205c819bc112 Mon Sep 17 00:00:00 2001
From: Centri3 <114838443+Centri3@users.noreply.github.com>
Date: Wed, 7 Jun 2023 18:22:17 -0500
Subject: [PATCH 09/10] Check if from proc macro and better tests
---
clippy_lints/src/excessive_nesting.rs | 17 +-
.../excessive_nesting/above/clippy.toml | 1 +
.../auxiliary/macro_rules.rs | 7 -
.../excessive_nesting/auxiliary/mod.rs | 16 -
.../auxiliary/proc_macros.rs | 476 ++++++++++++++++++
.../excessive_nesting/default/clippy.toml | 1 -
....stderr => excessive_nesting.above.stderr} | 223 ++++----
.../excessive_nesting.default.stderr | 0
.../excessive_nesting/excessive_nesting.rs | 13 +-
.../excessive_nesting.set.stderr | 100 ++--
.../toml_unknown_key/conf_unknown_key.stderr | 1 +
11 files changed, 622 insertions(+), 233 deletions(-)
create mode 100644 tests/ui-toml/excessive_nesting/above/clippy.toml
delete mode 100644 tests/ui-toml/excessive_nesting/auxiliary/macro_rules.rs
delete mode 100644 tests/ui-toml/excessive_nesting/auxiliary/mod.rs
create mode 100644 tests/ui-toml/excessive_nesting/auxiliary/proc_macros.rs
delete mode 100644 tests/ui-toml/excessive_nesting/default/clippy.toml
rename tests/ui-toml/excessive_nesting/{excessive_nesting.stderr => excessive_nesting.above.stderr} (55%)
delete mode 100644 tests/ui-toml/excessive_nesting/excessive_nesting.default.stderr
diff --git a/clippy_lints/src/excessive_nesting.rs b/clippy_lints/src/excessive_nesting.rs
index 1d68d7f94780..1c6cd27789da 100644
--- a/clippy_lints/src/excessive_nesting.rs
+++ b/clippy_lints/src/excessive_nesting.rs
@@ -1,4 +1,4 @@
-use clippy_utils::diagnostics::span_lint_and_help;
+use clippy_utils::{diagnostics::span_lint_and_help, source::snippet};
use rustc_ast::{
node_id::NodeSet,
visit::{walk_block, walk_item, Visitor},
@@ -86,6 +86,10 @@ impl ExcessiveNesting {
impl EarlyLintPass for ExcessiveNesting {
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
+ if self.excessive_nesting_threshold == 0 {
+ return;
+ }
+
let mut visitor = NestingVisitor {
conf: self,
cx,
@@ -114,9 +118,7 @@ struct NestingVisitor<'conf, 'cx> {
impl NestingVisitor<'_, '_> {
fn check_indent(&mut self, span: Span, id: NodeId) -> bool {
- let threshold = self.conf.excessive_nesting_threshold;
-
- if threshold != 0 && self.nest_level > threshold && !in_external_macro(self.cx.sess(), span) {
+ if self.nest_level > self.conf.excessive_nesting_threshold && !in_external_macro(self.cx.sess(), span) {
self.conf.nodes.insert(id);
return true;
@@ -132,6 +134,13 @@ impl<'conf, 'cx> Visitor<'_> for NestingVisitor<'conf, 'cx> {
return;
}
+ // TODO: This should be rewritten using `LateLintPass` so we can use `is_from_proc_macro` instead,
+ // but for now, this is fine.
+ let snippet = snippet(self.cx, block.span, "{}").trim().to_owned();
+ if !snippet.starts_with('{') || !snippet.ends_with('}') {
+ return;
+ }
+
self.nest_level += 1;
if !self.check_indent(block.span, block.id) {
diff --git a/tests/ui-toml/excessive_nesting/above/clippy.toml b/tests/ui-toml/excessive_nesting/above/clippy.toml
new file mode 100644
index 000000000000..e60ac978cac9
--- /dev/null
+++ b/tests/ui-toml/excessive_nesting/above/clippy.toml
@@ -0,0 +1 @@
+excessive-nesting-threshold = 4
diff --git a/tests/ui-toml/excessive_nesting/auxiliary/macro_rules.rs b/tests/ui-toml/excessive_nesting/auxiliary/macro_rules.rs
deleted file mode 100644
index 86663db68f24..000000000000
--- a/tests/ui-toml/excessive_nesting/auxiliary/macro_rules.rs
+++ /dev/null
@@ -1,7 +0,0 @@
-#[rustfmt::skip]
-#[macro_export]
-macro_rules! excessive_nesting {
- () => {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
- println!("hi!!")
- }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
-}
diff --git a/tests/ui-toml/excessive_nesting/auxiliary/mod.rs b/tests/ui-toml/excessive_nesting/auxiliary/mod.rs
deleted file mode 100644
index 967b3af3bcad..000000000000
--- a/tests/ui-toml/excessive_nesting/auxiliary/mod.rs
+++ /dev/null
@@ -1,16 +0,0 @@
-#![rustfmt::skip]
-
-mod a {
- mod b {
- mod c {
- mod d {
- mod e {}
- }
- }
- }
-}
-
-fn main() {
- // this should lint
- {{{}}}
-}
diff --git a/tests/ui-toml/excessive_nesting/auxiliary/proc_macros.rs b/tests/ui-toml/excessive_nesting/auxiliary/proc_macros.rs
new file mode 100644
index 000000000000..87620fe91bab
--- /dev/null
+++ b/tests/ui-toml/excessive_nesting/auxiliary/proc_macros.rs
@@ -0,0 +1,476 @@
+//@compile-flags: --emit=link
+//@no-prefer-dynamic
+
+// NOTE: Copied from `ui/auxiliary/proc_macros.rs`, couldn't get `../` to work for some reason
+
+#![crate_type = "proc-macro"]
+#![feature(let_chains)]
+#![feature(proc_macro_span)]
+#![allow(dead_code)]
+
+extern crate proc_macro;
+
+use core::mem;
+use proc_macro::{
+ token_stream::IntoIter,
+ Delimiter::{self, Brace, Parenthesis},
+ Group, Ident, Literal, Punct,
+ Spacing::{self, Alone, Joint},
+ Span, TokenStream, TokenTree as TT,
+};
+
+type Result