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 = core::result::Result; + +/// Make a `compile_error!` pointing to the given span. +fn make_error(msg: &str, span: Span) -> TokenStream { + TokenStream::from_iter([ + TT::Ident(Ident::new("compile_error", span)), + TT::Punct(punct_with_span('!', Alone, span)), + TT::Group({ + let mut msg = Literal::string(msg); + msg.set_span(span); + group_with_span(Parenthesis, TokenStream::from_iter([TT::Literal(msg)]), span) + }), + ]) +} + +fn expect_tt(tt: Option, f: impl FnOnce(TT) -> Option, expected: &str, span: Span) -> Result { + match tt { + None => Err(make_error( + &format!("unexpected end of input, expected {expected}"), + span, + )), + Some(tt) => { + let span = tt.span(); + match f(tt) { + Some(x) => Ok(x), + None => Err(make_error(&format!("unexpected token, expected {expected}"), span)), + } + }, + } +} + +fn punct_with_span(c: char, spacing: Spacing, span: Span) -> Punct { + let mut p = Punct::new(c, spacing); + p.set_span(span); + p +} + +fn group_with_span(delimiter: Delimiter, stream: TokenStream, span: Span) -> Group { + let mut g = Group::new(delimiter, stream); + g.set_span(span); + g +} + +/// Token used to escape the following token from the macro's span rules. +const ESCAPE_CHAR: char = '$'; + +/// Takes a single token followed by a sequence of tokens. Returns the sequence of tokens with their +/// span set to that of the first token. Tokens may be escaped with either `#ident` or `#(tokens)`. +#[proc_macro] +pub fn with_span(input: TokenStream) -> TokenStream { + let mut iter = input.into_iter(); + let span = iter.next().unwrap().span(); + let mut res = TokenStream::new(); + if let Err(e) = write_with_span(span, iter, &mut res) { + e + } else { + res + } +} + +/// Takes a sequence of tokens and return the tokens with the span set such that they appear to be +/// from an external macro. Tokens may be escaped with either `#ident` or `#(tokens)`. +#[proc_macro] +pub fn external(input: TokenStream) -> TokenStream { + let mut res = TokenStream::new(); + if let Err(e) = write_with_span(Span::mixed_site(), input.into_iter(), &mut res) { + e + } else { + res + } +} + +/// Copies all the tokens, replacing all their spans with the given span. Tokens can be escaped +/// either by `#ident` or `#(tokens)`. +fn write_with_span(s: Span, mut input: IntoIter, out: &mut TokenStream) -> Result<()> { + while let Some(tt) = input.next() { + match tt { + TT::Punct(p) if p.as_char() == ESCAPE_CHAR => { + expect_tt( + input.next(), + |tt| match tt { + tt @ (TT::Ident(_) | TT::Literal(_)) => { + out.extend([tt]); + Some(()) + }, + TT::Punct(mut p) if p.as_char() == ESCAPE_CHAR => { + p.set_span(s); + out.extend([TT::Punct(p)]); + Some(()) + }, + TT::Group(g) if g.delimiter() == Parenthesis => { + out.extend([TT::Group(group_with_span(Delimiter::None, g.stream(), g.span()))]); + Some(()) + }, + _ => None, + }, + "an ident, a literal, or parenthesized tokens", + p.span(), + )?; + }, + TT::Group(g) => { + let mut stream = TokenStream::new(); + write_with_span(s, g.stream().into_iter(), &mut stream)?; + out.extend([TT::Group(group_with_span(g.delimiter(), stream, s))]); + }, + mut tt => { + tt.set_span(s); + out.extend([tt]); + }, + } + } + Ok(()) +} + +/// Within the item this attribute is attached to, an `inline!` macro is available which expands the +/// contained tokens as though they came from a macro expansion. +/// +/// Within the `inline!` macro, any token preceded by `$` is passed as though it were an argument +/// with an automatically chosen fragment specifier. `$ident` will be passed as `ident`, `$1` or +/// `$"literal"` will be passed as `literal`, `$'lt` will be passed as `lifetime`, and `$(...)` will +/// pass the contained tokens as a `tt` sequence (the wrapping parenthesis are removed). If another +/// specifier is required it can be specified within parenthesis like `$(@expr ...)`. This will +/// expand the remaining tokens as a single argument. +/// +/// Multiple `inline!` macros may be nested within each other. This will expand as nested macro +/// calls. However, any arguments will be passed as though they came from the outermost context. +#[proc_macro_attribute] +pub fn inline_macros(args: TokenStream, input: TokenStream) -> TokenStream { + let mut args = args.into_iter(); + let mac_name = match args.next() { + Some(TT::Ident(name)) => Some(name), + Some(tt) => { + return make_error( + "unexpected argument, expected either an ident or no arguments", + tt.span(), + ); + }, + None => None, + }; + if let Some(tt) = args.next() { + return make_error( + "unexpected argument, expected either an ident or no arguments", + tt.span(), + ); + }; + + let mac_name = if let Some(mac_name) = mac_name { + Ident::new(&format!("__inline_mac_{mac_name}"), Span::call_site()) + } else { + let mut input = match LookaheadIter::new(input.clone().into_iter()) { + Some(x) => x, + None => return input, + }; + loop { + match input.next() { + None => break Ident::new("__inline_mac", Span::call_site()), + Some(TT::Ident(kind)) => match &*kind.to_string() { + "impl" => break Ident::new("__inline_mac_impl", Span::call_site()), + kind @ ("struct" | "enum" | "union" | "fn" | "mod" | "trait" | "type" | "const" | "static") => { + if let TT::Ident(name) = &input.tt { + break Ident::new(&format!("__inline_mac_{kind}_{name}"), Span::call_site()); + } else { + break Ident::new(&format!("__inline_mac_{kind}"), Span::call_site()); + } + }, + _ => {}, + }, + _ => {}, + } + } + }; + + let mut expander = Expander::default(); + let mut mac = MacWriter::new(mac_name); + if let Err(e) = expander.expand(input.into_iter(), &mut mac) { + return e; + } + let mut out = TokenStream::new(); + mac.finish(&mut out); + out.extend(expander.expn); + out +} + +/// Wraps a `TokenStream` iterator with a single token lookahead. +struct LookaheadIter { + tt: TT, + iter: IntoIter, +} +impl LookaheadIter { + fn new(mut iter: IntoIter) -> Option { + iter.next().map(|tt| Self { tt, iter }) + } + + /// Get's the lookahead token, replacing it with the next token in the stream. + /// Note: If there isn't a next token, this will not return the lookahead token. + fn next(&mut self) -> Option { + self.iter.next().map(|tt| mem::replace(&mut self.tt, tt)) + } +} + +/// Builds the macro used to implement all the `inline!` macro calls. +struct MacWriter { + name: Ident, + macros: TokenStream, + next_idx: usize, +} +impl MacWriter { + fn new(name: Ident) -> Self { + Self { + name, + macros: TokenStream::new(), + next_idx: 0, + } + } + + /// Inserts a new `inline!` call. + fn insert(&mut self, name_span: Span, bang_span: Span, body: Group, expander: &mut Expander) -> Result<()> { + let idx = self.next_idx; + self.next_idx += 1; + + let mut inner = Expander::for_arm(idx); + inner.expand(body.stream().into_iter(), self)?; + let new_arm = inner.arm.unwrap(); + + self.macros.extend([ + TT::Group(Group::new(Parenthesis, new_arm.args_def)), + TT::Punct(Punct::new('=', Joint)), + TT::Punct(Punct::new('>', Alone)), + TT::Group(Group::new(Parenthesis, inner.expn)), + TT::Punct(Punct::new(';', Alone)), + ]); + + expander.expn.extend([ + TT::Ident({ + let mut name = self.name.clone(); + name.set_span(name_span); + name + }), + TT::Punct(punct_with_span('!', Alone, bang_span)), + ]); + let mut call_body = TokenStream::from_iter([TT::Literal(Literal::usize_unsuffixed(idx))]); + if let Some(arm) = expander.arm.as_mut() { + if !new_arm.args.is_empty() { + arm.add_sub_args(new_arm.args, &mut call_body); + } + } else { + call_body.extend(new_arm.args); + } + let mut g = Group::new(body.delimiter(), call_body); + g.set_span(body.span()); + expander.expn.extend([TT::Group(g)]); + Ok(()) + } + + /// Creates the macro definition. + fn finish(self, out: &mut TokenStream) { + if self.next_idx != 0 { + out.extend([ + TT::Ident(Ident::new("macro_rules", Span::call_site())), + TT::Punct(Punct::new('!', Alone)), + TT::Ident(self.name), + TT::Group(Group::new(Brace, self.macros)), + ]) + } + } +} + +struct MacroArm { + args_def: TokenStream, + args: Vec, +} +impl MacroArm { + fn add_single_arg_def(&mut self, kind: &str, dollar_span: Span, arg_span: Span, out: &mut TokenStream) { + let mut name = Ident::new(&format!("_{}", self.args.len()), Span::call_site()); + self.args_def.extend([ + TT::Punct(Punct::new('$', Alone)), + TT::Ident(name.clone()), + TT::Punct(Punct::new(':', Alone)), + TT::Ident(Ident::new(kind, Span::call_site())), + ]); + name.set_span(arg_span); + out.extend([TT::Punct(punct_with_span('$', Alone, dollar_span)), TT::Ident(name)]); + } + + fn add_parenthesized_arg_def(&mut self, kind: Ident, dollar_span: Span, arg_span: Span, out: &mut TokenStream) { + let mut name = Ident::new(&format!("_{}", self.args.len()), Span::call_site()); + self.args_def.extend([TT::Group(Group::new( + Parenthesis, + TokenStream::from_iter([ + TT::Punct(Punct::new('$', Alone)), + TT::Ident(name.clone()), + TT::Punct(Punct::new(':', Alone)), + TT::Ident(kind), + ]), + ))]); + name.set_span(arg_span); + out.extend([TT::Punct(punct_with_span('$', Alone, dollar_span)), TT::Ident(name)]); + } + + fn add_multi_arg_def(&mut self, dollar_span: Span, arg_span: Span, out: &mut TokenStream) { + let mut name = Ident::new(&format!("_{}", self.args.len()), Span::call_site()); + self.args_def.extend([TT::Group(Group::new( + Parenthesis, + TokenStream::from_iter([ + TT::Punct(Punct::new('$', Alone)), + TT::Group(Group::new( + Parenthesis, + TokenStream::from_iter([ + TT::Punct(Punct::new('$', Alone)), + TT::Ident(name.clone()), + TT::Punct(Punct::new(':', Alone)), + TT::Ident(Ident::new("tt", Span::call_site())), + ]), + )), + TT::Punct(Punct::new('*', Alone)), + ]), + ))]); + name.set_span(arg_span); + out.extend([ + TT::Punct(punct_with_span('$', Alone, dollar_span)), + TT::Group(group_with_span( + Parenthesis, + TokenStream::from_iter([TT::Punct(punct_with_span('$', Alone, dollar_span)), TT::Ident(name)]), + dollar_span, + )), + TT::Punct(punct_with_span('*', Alone, dollar_span)), + ]); + } + + fn add_arg(&mut self, dollar_span: Span, tt: TT, input: &mut IntoIter, out: &mut TokenStream) -> Result<()> { + match tt { + TT::Punct(p) if p.as_char() == ESCAPE_CHAR => out.extend([TT::Punct(p)]), + TT::Punct(p) if p.as_char() == '\'' && p.spacing() == Joint => { + let lt_name = expect_tt( + input.next(), + |tt| match tt { + TT::Ident(x) => Some(x), + _ => None, + }, + "lifetime name", + p.span(), + )?; + let arg_span = p.span().join(lt_name.span()).unwrap_or(p.span()); + self.add_single_arg_def("lifetime", dollar_span, arg_span, out); + self.args.extend([TT::Punct(p), TT::Ident(lt_name)]); + }, + TT::Ident(x) => { + self.add_single_arg_def("ident", dollar_span, x.span(), out); + self.args.push(TT::Ident(x)); + }, + TT::Literal(x) => { + self.add_single_arg_def("literal", dollar_span, x.span(), out); + self.args.push(TT::Literal(x)); + }, + TT::Group(g) if g.delimiter() == Parenthesis => { + let mut inner = g.stream().into_iter(); + if let Some(TT::Punct(p)) = inner.next() + && p.as_char() == '@' + { + let kind = expect_tt( + inner.next(), + |tt| match tt { + TT::Ident(kind) => Some(kind), + _ => None, + }, + "a macro fragment specifier", + p.span(), + )?; + self.add_parenthesized_arg_def(kind, dollar_span, g.span(), out); + self.args.push(TT::Group(group_with_span(Parenthesis, inner.collect(), g.span()))) + } else { + self.add_multi_arg_def(dollar_span, g.span(), out); + self.args.push(TT::Group(g)); + } + }, + tt => return Err(make_error("unsupported escape", tt.span())), + }; + Ok(()) + } + + fn add_sub_args(&mut self, args: Vec, out: &mut TokenStream) { + self.add_multi_arg_def(Span::call_site(), Span::call_site(), out); + self.args + .extend([TT::Group(Group::new(Parenthesis, TokenStream::from_iter(args)))]); + } +} + +#[derive(Default)] +struct Expander { + arm: Option, + expn: TokenStream, +} +impl Expander { + fn for_arm(idx: usize) -> Self { + Self { + arm: Some(MacroArm { + args_def: TokenStream::from_iter([TT::Literal(Literal::usize_unsuffixed(idx))]), + args: Vec::new(), + }), + expn: TokenStream::new(), + } + } + + fn write_tt(&mut self, tt: TT, mac: &mut MacWriter) -> Result<()> { + match tt { + TT::Group(g) => { + let outer = mem::take(&mut self.expn); + self.expand(g.stream().into_iter(), mac)?; + let inner = mem::replace(&mut self.expn, outer); + self.expn + .extend([TT::Group(group_with_span(g.delimiter(), inner, g.span()))]); + }, + tt => self.expn.extend([tt]), + } + Ok(()) + } + + fn expand(&mut self, input: IntoIter, mac: &mut MacWriter) -> Result<()> { + let Some(mut input) = LookaheadIter::new(input) else { + return Ok(()); + }; + while let Some(tt) = input.next() { + if let TT::Punct(p) = &tt + && p.as_char() == ESCAPE_CHAR + && let Some(arm) = self.arm.as_mut() + { + arm.add_arg(p.span(), mem::replace(&mut input.tt, tt), &mut input.iter, &mut self.expn)?; + if input.next().is_none() { + return Ok(()); + } + } else if let TT::Punct(p) = &input.tt + && p.as_char() == '!' + && let TT::Ident(name) = &tt + && name.to_string() == "inline" + { + let g = expect_tt( + input.iter.next(), + |tt| match tt { + TT::Group(g) => Some(g), + _ => None, + }, + "macro arguments", + p.span(), + )?; + mac.insert(name.span(), p.span(), g, self)?; + if input.next().is_none() { + return Ok(()); + } + } else { + self.write_tt(tt, mac)?; + } + } + self.write_tt(input.tt, mac) + } +} diff --git a/tests/ui-toml/excessive_nesting/default/clippy.toml b/tests/ui-toml/excessive_nesting/default/clippy.toml deleted file mode 100644 index e8b115a0f7c6..000000000000 --- a/tests/ui-toml/excessive_nesting/default/clippy.toml +++ /dev/null @@ -1 +0,0 @@ -excessive-nesting-threshold = 0 diff --git a/tests/ui-toml/excessive_nesting/excessive_nesting.stderr b/tests/ui-toml/excessive_nesting/excessive_nesting.above.stderr similarity index 55% rename from tests/ui-toml/excessive_nesting/excessive_nesting.stderr rename to tests/ui-toml/excessive_nesting/excessive_nesting.above.stderr index f1db396fe480..0ec41fbaa078 100644 --- a/tests/ui-toml/excessive_nesting/excessive_nesting.stderr +++ b/tests/ui-toml/excessive_nesting/excessive_nesting.above.stderr @@ -1,369 +1,314 @@ error: this block is too nested - --> $DIR/auxiliary/mod.rs:6:13 + --> $DIR/excessive_nesting.rs:23:25 | -LL | / mod d { -LL | | mod e {} -LL | | } - | |_____________^ +LL | let w = { 3 }; + | ^^^^^ | = 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 + --> $DIR/excessive_nesting.rs:69:17 | -LL | {{{}}} - | ^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:21: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:65:24 - | -LL | pub fn b() { - | ________________________^ -LL | | struct C; -LL | | -LL | | impl C { +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:81:21 + --> $DIR/excessive_nesting.rs:83:25 | -LL | fn cc() { - | _____________________^ -LL | | let x = { 1 }; // not a warning, but cc is -LL | | } - | |_____________^ +LL | let x = { 1 }; // not a warning, but cc is + | ^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:85:21 - | -LL | let x = { 1 }; // warning - | ^^^^^ + --> $DIR/excessive_nesting.rs:100:17 | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:98:13 - | -LL | / pub mod d { -LL | | pub mod e { +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:112:17 + --> $DIR/excessive_nesting.rs:113:18 | LL | a_but_not({{{{{{{{0}}}}}}}}); - | ^^^^^^^^^^^^^ + | ^^^^^^^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:113:11 + --> $DIR/excessive_nesting.rs:114:12 | LL | a.a({{{{{{{{{0}}}}}}}}}); - | ^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:114:11 + --> $DIR/excessive_nesting.rs:115:12 | LL | (0, {{{{{{{1}}}}}}}); - | ^^^^^^^^^^^ + | ^^^^^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:118:21 + --> $DIR/excessive_nesting.rs:120:25 | -LL | if true { - | _____________________^ -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:130:25 + --> $DIR/excessive_nesting.rs:132:29 | -LL | let y = (|| { - | _________________________^ -LL | | let z = (|| { +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:145: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:149:12 + --> $DIR/excessive_nesting.rs:151:13 | LL | y += {{{{{5}}}}}; - | ^^^^^^^ + | ^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:150:19 + --> $DIR/excessive_nesting.rs:152:20 | LL | let z = y + {{{{{{{{{5}}}}}}}}}; - | ^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:151:11 + --> $DIR/excessive_nesting.rs:153:12 | LL | [0, {{{{{{{{{{0}}}}}}}}}}]; - | ^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:152:24 + --> $DIR/excessive_nesting.rs:154:25 | LL | let mut xx = [0; {{{{{{{{100}}}}}}}}]; - | ^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:153:10 + --> $DIR/excessive_nesting.rs:155:11 | LL | xx[{{{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}}}]; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:154:12 + --> $DIR/excessive_nesting.rs:156:13 | LL | &mut {{{{{{{{{{y}}}}}}}}}}; - | ^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:156:16 + --> $DIR/excessive_nesting.rs:158:17 | LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}} - | ^^^^^^ + | ^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:156:27 + --> $DIR/excessive_nesting.rs:158:28 | LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}} - | ^^^^^^^^^^^^ + | ^^^^^^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:158:27 + --> $DIR/excessive_nesting.rs:160:28 | 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:158:47 + --> $DIR/excessive_nesting.rs:160:48 | 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:160:13 + --> $DIR/excessive_nesting.rs:162:14 | LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}} - | ^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:160:34 + --> $DIR/excessive_nesting.rs:162:35 | LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}} - | ^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:162:22 + --> $DIR/excessive_nesting.rs:164:23 | LL | let d = D { d: {{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}} }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:164:7 + --> $DIR/excessive_nesting.rs:166:8 | LL | {{{{1;}}}}..{{{{{{3}}}}}}; - | ^^^^^^ + | ^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:164:19 + --> $DIR/excessive_nesting.rs:166:20 | LL | {{{{1;}}}}..{{{{{{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:167:8 | LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}}; - | ^^^^^^ + | ^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:165:20 + --> $DIR/excessive_nesting.rs:167:21 | LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}}; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:166:9 + --> $DIR/excessive_nesting.rs:168:10 | LL | ..{{{{{{{5}}}}}}}; - | ^^^^^^^^^^^ + | ^^^^^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:167:10 + --> $DIR/excessive_nesting.rs:169:11 | LL | ..={{{{{3}}}}}; - | ^^^^^^^ + | ^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:168:7 + --> $DIR/excessive_nesting.rs:170:8 | LL | {{{{{1;}}}}}..; - | ^^^^^^^^ + | ^^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:170:19 + --> $DIR/excessive_nesting.rs:172:20 | LL | loop { break {{{{1}}}} }; - | ^^^^^^^ + | ^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:171:12 + --> $DIR/excessive_nesting.rs:173:13 | LL | loop {{{{{{}}}}}} - | ^^^^^^^^ + | ^^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:173:13 + --> $DIR/excessive_nesting.rs:175:14 | LL | match {{{{{{true}}}}}} { - | ^^^^^^^^^^^^ + | ^^^^^^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:174:19 + --> $DIR/excessive_nesting.rs:176:20 | LL | true => {{{{}}}}, - | ^^^^ + | ^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:175:20 + --> $DIR/excessive_nesting.rs:177:21 | LL | false => {{{{}}}}, - | ^^^^ + | ^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:180:13 + --> $DIR/excessive_nesting.rs:183:17 | -LL | / { -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:190:27 + --> $DIR/excessive_nesting.rs:192:28 | LL | async fn c() -> u32 {{{{{{{0}}}}}}} - | ^^^^^^^^^^^ + | ^^^^^^^^^ | = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:196:7 + --> $DIR/excessive_nesting.rs:198:8 | LL | {{{{b().await}}}}; - | ^^^^^^^^^^^^^ + | ^^^^^^^^^^^ | = help: try refactoring your code to minimize nesting -error: aborting due to 41 previous errors +error: aborting due to 37 previous errors diff --git a/tests/ui-toml/excessive_nesting/excessive_nesting.default.stderr b/tests/ui-toml/excessive_nesting/excessive_nesting.default.stderr deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/tests/ui-toml/excessive_nesting/excessive_nesting.rs b/tests/ui-toml/excessive_nesting/excessive_nesting.rs index 5e436b97ee83..432c4132b57b 100644 --- a/tests/ui-toml/excessive_nesting/excessive_nesting.rs +++ b/tests/ui-toml/excessive_nesting/excessive_nesting.rs @@ -1,7 +1,7 @@ -//@aux-build:macro_rules.rs -//@revisions: set default +//@aux-build:proc_macros.rs +//@revisions: set above //@[set] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/excessive_nesting/set -//@[default] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/excessive_nesting/default +//@[above] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/excessive_nesting/above #![rustfmt::skip] #![feature(custom_inner_attributes)] #![allow(unused)] @@ -13,10 +13,8 @@ #![warn(clippy::excessive_nesting)] #![allow(clippy::collapsible_if)] -mod auxiliary; - #[macro_use] -extern crate macro_rules; +extern crate proc_macros; static X: u32 = { let x = { @@ -142,7 +140,8 @@ fn main() { x })(); - excessive_nesting!(); // ensure this isn't linted in external macros + external! { {{{{{{{{{{{{{{{{}}}}}}}}}}}}}}}} }; // ensure this isn't linted in external macros + with_span! { span {{{{{{{{{{{{}}}}}}}}}}}} }; // don't lint for proc macros 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.set.stderr b/tests/ui-toml/excessive_nesting/excessive_nesting.set.stderr index 1f069437dfe5..237cc6c4a90b 100644 --- a/tests/ui-toml/excessive_nesting/excessive_nesting.set.stderr +++ b/tests/ui-toml/excessive_nesting/excessive_nesting.set.stderr @@ -1,24 +1,5 @@ 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 + --> $DIR/excessive_nesting.rs:22:21 | LL | let z = { | _____________________^ @@ -28,9 +9,10 @@ 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:68:24 + --> $DIR/excessive_nesting.rs:66:24 | LL | pub fn b() { | ________________________^ @@ -45,7 +27,7 @@ LL | | } = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:84:21 + --> $DIR/excessive_nesting.rs:82:21 | LL | fn cc() { | _____________________^ @@ -56,7 +38,7 @@ LL | | } = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:88:21 + --> $DIR/excessive_nesting.rs:86:21 | LL | let x = { 1 }; // warning | ^^^^^ @@ -64,7 +46,7 @@ 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 + --> $DIR/excessive_nesting.rs:99:13 | LL | / pub mod d { LL | | pub mod e { @@ -76,7 +58,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:115:17 + --> $DIR/excessive_nesting.rs:113:17 | LL | a_but_not({{{{{{{{0}}}}}}}}); | ^^^^^^^^^^^^^ @@ -84,7 +66,7 @@ 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 + --> $DIR/excessive_nesting.rs:114:11 | LL | a.a({{{{{{{{{0}}}}}}}}}); | ^^^^^^^^^^^^^^^ @@ -92,7 +74,7 @@ LL | a.a({{{{{{{{{0}}}}}}}}}); = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:117:11 + --> $DIR/excessive_nesting.rs:115:11 | LL | (0, {{{{{{{1}}}}}}}); | ^^^^^^^^^^^ @@ -100,7 +82,7 @@ LL | (0, {{{{{{{1}}}}}}}); = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:121:21 + --> $DIR/excessive_nesting.rs:119:21 | LL | if true { | _____________________^ @@ -115,7 +97,7 @@ LL | | } = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:133:25 + --> $DIR/excessive_nesting.rs:131:25 | LL | let y = (|| { | _________________________^ @@ -130,7 +112,7 @@ LL | | })(); = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:148:36 + --> $DIR/excessive_nesting.rs:147:36 | LL | !{boo as u32 + !{boo as u32 + !{boo as u32}}}; | ^^^^^^^^^^^^ @@ -138,7 +120,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:152:12 + --> $DIR/excessive_nesting.rs:151:12 | LL | y += {{{{{5}}}}}; | ^^^^^^^ @@ -146,7 +128,7 @@ LL | y += {{{{{5}}}}}; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:153:19 + --> $DIR/excessive_nesting.rs:152:19 | LL | let z = y + {{{{{{{{{5}}}}}}}}}; | ^^^^^^^^^^^^^^^ @@ -154,7 +136,7 @@ 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 + --> $DIR/excessive_nesting.rs:153:11 | LL | [0, {{{{{{{{{{0}}}}}}}}}}]; | ^^^^^^^^^^^^^^^^^ @@ -162,7 +144,7 @@ LL | [0, {{{{{{{{{{0}}}}}}}}}}]; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:155:24 + --> $DIR/excessive_nesting.rs:154:24 | LL | let mut xx = [0; {{{{{{{{100}}}}}}}}]; | ^^^^^^^^^^^^^^^ @@ -170,7 +152,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:156:10 + --> $DIR/excessive_nesting.rs:155:10 | LL | xx[{{{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}}}]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -178,7 +160,7 @@ LL | xx[{{{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}}}]; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:157:12 + --> $DIR/excessive_nesting.rs:156:12 | LL | &mut {{{{{{{{{{y}}}}}}}}}}; | ^^^^^^^^^^^^^^^^^ @@ -186,7 +168,7 @@ LL | &mut {{{{{{{{{{y}}}}}}}}}}; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:159:16 + --> $DIR/excessive_nesting.rs:158:16 | LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}} | ^^^^^^ @@ -194,7 +176,7 @@ 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 + --> $DIR/excessive_nesting.rs:158:27 | LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}} | ^^^^^^^^^^^^ @@ -202,7 +184,7 @@ 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 + --> $DIR/excessive_nesting.rs:160:27 | LL | while let Some(i) = {{{{{{Some(1)}}}}}} {{{{{{{}}}}}}} | ^^^^^^^^^^^^^^^ @@ -210,7 +192,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:161:47 + --> $DIR/excessive_nesting.rs:160:47 | LL | while let Some(i) = {{{{{{Some(1)}}}}}} {{{{{{{}}}}}}} | ^^^^^^^^^^ @@ -218,7 +200,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:163:13 + --> $DIR/excessive_nesting.rs:162:13 | LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}} | ^^^^^^^^^^^^^^^^ @@ -226,7 +208,7 @@ LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}} = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:163:34 + --> $DIR/excessive_nesting.rs:162:34 | LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}} | ^^^^^^^^^^^^^^ @@ -234,7 +216,7 @@ LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}} = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:165:22 + --> $DIR/excessive_nesting.rs:164:22 | LL | let d = D { d: {{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}} }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -242,7 +224,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:167:7 + --> $DIR/excessive_nesting.rs:166:7 | LL | {{{{1;}}}}..{{{{{{3}}}}}}; | ^^^^^^ @@ -250,7 +232,7 @@ LL | {{{{1;}}}}..{{{{{{3}}}}}}; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:167:19 + --> $DIR/excessive_nesting.rs:166:19 | LL | {{{{1;}}}}..{{{{{{3}}}}}}; | ^^^^^^^^^ @@ -258,7 +240,7 @@ LL | {{{{1;}}}}..{{{{{{3}}}}}}; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:168:7 + --> $DIR/excessive_nesting.rs:167:7 | LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}}; | ^^^^^^ @@ -266,7 +248,7 @@ LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}}; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:168:20 + --> $DIR/excessive_nesting.rs:167:20 | LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}}; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -274,7 +256,7 @@ LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}}; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:169:9 + --> $DIR/excessive_nesting.rs:168:9 | LL | ..{{{{{{{5}}}}}}}; | ^^^^^^^^^^^ @@ -282,7 +264,7 @@ LL | ..{{{{{{{5}}}}}}}; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:170:10 + --> $DIR/excessive_nesting.rs:169:10 | LL | ..={{{{{3}}}}}; | ^^^^^^^ @@ -290,7 +272,7 @@ LL | ..={{{{{3}}}}}; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:171:7 + --> $DIR/excessive_nesting.rs:170:7 | LL | {{{{{1;}}}}}..; | ^^^^^^^^ @@ -298,7 +280,7 @@ LL | {{{{{1;}}}}}..; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:173:19 + --> $DIR/excessive_nesting.rs:172:19 | LL | loop { break {{{{1}}}} }; | ^^^^^^^ @@ -306,7 +288,7 @@ LL | loop { break {{{{1}}}} }; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:174:12 + --> $DIR/excessive_nesting.rs:173:12 | LL | loop {{{{{{}}}}}} | ^^^^^^^^ @@ -314,7 +296,7 @@ LL | loop {{{{{{}}}}}} = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:176:13 + --> $DIR/excessive_nesting.rs:175:13 | LL | match {{{{{{true}}}}}} { | ^^^^^^^^^^^^ @@ -322,7 +304,7 @@ LL | match {{{{{{true}}}}}} { = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:177:19 + --> $DIR/excessive_nesting.rs:176:19 | LL | true => {{{{}}}}, | ^^^^ @@ -330,7 +312,7 @@ LL | true => {{{{}}}}, = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:178:20 + --> $DIR/excessive_nesting.rs:177:20 | LL | false => {{{{}}}}, | ^^^^ @@ -338,7 +320,7 @@ LL | false => {{{{}}}}, = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:183:13 + --> $DIR/excessive_nesting.rs:182:13 | LL | / { LL | | { @@ -350,7 +332,7 @@ LL | | } = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:193:27 + --> $DIR/excessive_nesting.rs:192:27 | LL | async fn c() -> u32 {{{{{{{0}}}}}}} | ^^^^^^^^^^^ @@ -358,12 +340,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:199:7 + --> $DIR/excessive_nesting.rs:198:7 | LL | {{{{b().await}}}}; | ^^^^^^^^^^^^^ | = help: try refactoring your code to minimize nesting -error: aborting due to 41 previous errors +error: aborting due to 39 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 28b6e7aa8248..5ec6f3decc3c 100644 --- a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr +++ b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr @@ -86,6 +86,7 @@ error: error reading Clippy's configuration file: unknown field `barfoo`, expect enforced-import-renames enum-variant-name-threshold enum-variant-size-threshold + excessive-nesting-threshold future-size-threshold ignore-interior-mutability large-error-threshold From 6afb3555d303b0708fc0f687f531c25e47806ce8 Mon Sep 17 00:00:00 2001 From: Centri3 <114838443+Centri3@users.noreply.github.com> Date: Thu, 8 Jun 2023 10:15:58 -0500 Subject: [PATCH 10/10] remove revisions --- .../auxiliary/proc_macros.rs | 2 +- .../excessive_nesting/{above => }/clippy.toml | 0 .../excessive_nesting/excessive_nesting.rs | 3 - .../excessive_nesting.set.stderr | 351 ------------------ ....above.stderr => excessive_nesting.stderr} | 74 ++-- .../ui-toml/excessive_nesting/set/clippy.toml | 1 - 6 files changed, 38 insertions(+), 393 deletions(-) rename tests/ui-toml/excessive_nesting/{above => }/clippy.toml (100%) delete mode 100644 tests/ui-toml/excessive_nesting/excessive_nesting.set.stderr rename tests/ui-toml/excessive_nesting/{excessive_nesting.above.stderr => excessive_nesting.stderr} (83%) delete mode 100644 tests/ui-toml/excessive_nesting/set/clippy.toml diff --git a/tests/ui-toml/excessive_nesting/auxiliary/proc_macros.rs b/tests/ui-toml/excessive_nesting/auxiliary/proc_macros.rs index 87620fe91bab..df17974de033 100644 --- a/tests/ui-toml/excessive_nesting/auxiliary/proc_macros.rs +++ b/tests/ui-toml/excessive_nesting/auxiliary/proc_macros.rs @@ -6,7 +6,7 @@ #![crate_type = "proc-macro"] #![feature(let_chains)] #![feature(proc_macro_span)] -#![allow(dead_code)] +#![allow(clippy::excessive_nesting, dead_code)] extern crate proc_macro; diff --git a/tests/ui-toml/excessive_nesting/above/clippy.toml b/tests/ui-toml/excessive_nesting/clippy.toml similarity index 100% rename from tests/ui-toml/excessive_nesting/above/clippy.toml rename to tests/ui-toml/excessive_nesting/clippy.toml diff --git a/tests/ui-toml/excessive_nesting/excessive_nesting.rs b/tests/ui-toml/excessive_nesting/excessive_nesting.rs index 432c4132b57b..d771cb9b55e1 100644 --- a/tests/ui-toml/excessive_nesting/excessive_nesting.rs +++ b/tests/ui-toml/excessive_nesting/excessive_nesting.rs @@ -1,7 +1,4 @@ //@aux-build:proc_macros.rs -//@revisions: set above -//@[set] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/excessive_nesting/set -//@[above] rustc-env:CLIPPY_CONF_DIR=tests/ui-toml/excessive_nesting/above #![rustfmt::skip] #![feature(custom_inner_attributes)] #![allow(unused)] diff --git a/tests/ui-toml/excessive_nesting/excessive_nesting.set.stderr b/tests/ui-toml/excessive_nesting/excessive_nesting.set.stderr deleted file mode 100644 index 237cc6c4a90b..000000000000 --- a/tests/ui-toml/excessive_nesting/excessive_nesting.set.stderr +++ /dev/null @@ -1,351 +0,0 @@ -error: this block is too nested - --> $DIR/excessive_nesting.rs:22:21 - | -LL | let z = { - | _____________________^ -LL | | let w = { 3 }; -LL | | w -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:66: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:82: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:86:21 - | -LL | let x = { 1 }; // warning - | ^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:99: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:113:17 - | -LL | a_but_not({{{{{{{{0}}}}}}}}); - | ^^^^^^^^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:114:11 - | -LL | a.a({{{{{{{{{0}}}}}}}}}); - | ^^^^^^^^^^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:115:11 - | -LL | (0, {{{{{{{1}}}}}}}); - | ^^^^^^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:119: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:131: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:147: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:151:12 - | -LL | y += {{{{{5}}}}}; - | ^^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:152:19 - | -LL | let z = y + {{{{{{{{{5}}}}}}}}}; - | ^^^^^^^^^^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:153:11 - | -LL | [0, {{{{{{{{{{0}}}}}}}}}}]; - | ^^^^^^^^^^^^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:154: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:155:10 - | -LL | xx[{{{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}}}]; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:156:12 - | -LL | &mut {{{{{{{{{{y}}}}}}}}}}; - | ^^^^^^^^^^^^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:158:16 - | -LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}} - | ^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:158:27 - | -LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}} - | ^^^^^^^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:160: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:160: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:162:13 - | -LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}} - | ^^^^^^^^^^^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:162:34 - | -LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}} - | ^^^^^^^^^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:164: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:166:7 - | -LL | {{{{1;}}}}..{{{{{{3}}}}}}; - | ^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:166:19 - | -LL | {{{{1;}}}}..{{{{{{3}}}}}}; - | ^^^^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:167:7 - | -LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}}; - | ^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:167:20 - | -LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}}; - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:168:9 - | -LL | ..{{{{{{{5}}}}}}}; - | ^^^^^^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:169:10 - | -LL | ..={{{{{3}}}}}; - | ^^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:170:7 - | -LL | {{{{{1;}}}}}..; - | ^^^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:172:19 - | -LL | loop { break {{{{1}}}} }; - | ^^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:173:12 - | -LL | loop {{{{{{}}}}}} - | ^^^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:175:13 - | -LL | match {{{{{{true}}}}}} { - | ^^^^^^^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:176:19 - | -LL | true => {{{{}}}}, - | ^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:177:20 - | -LL | false => {{{{}}}}, - | ^^^^ - | - = help: try refactoring your code to minimize nesting - -error: this block is too nested - --> $DIR/excessive_nesting.rs:182: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:192: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:198:7 - | -LL | {{{{b().await}}}}; - | ^^^^^^^^^^^^^ - | - = help: try refactoring your code to minimize nesting - -error: aborting due to 39 previous errors - diff --git a/tests/ui-toml/excessive_nesting/excessive_nesting.above.stderr b/tests/ui-toml/excessive_nesting/excessive_nesting.stderr similarity index 83% rename from tests/ui-toml/excessive_nesting/excessive_nesting.above.stderr rename to tests/ui-toml/excessive_nesting/excessive_nesting.stderr index 0ec41fbaa078..a9fb159ef2a7 100644 --- a/tests/ui-toml/excessive_nesting/excessive_nesting.above.stderr +++ b/tests/ui-toml/excessive_nesting/excessive_nesting.stderr @@ -1,5 +1,5 @@ error: this block is too nested - --> $DIR/excessive_nesting.rs:23:25 + --> $DIR/excessive_nesting.rs:20:25 | LL | let w = { 3 }; | ^^^^^ @@ -8,7 +8,7 @@ LL | let w = { 3 }; = note: `-D clippy::excessive-nesting` implied by `-D warnings` error: this block is too nested - --> $DIR/excessive_nesting.rs:69:17 + --> $DIR/excessive_nesting.rs:66:17 | LL | / impl C { LL | | pub fn c() {} @@ -18,7 +18,7 @@ LL | | } = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:83:25 + --> $DIR/excessive_nesting.rs:80:25 | LL | let x = { 1 }; // not a warning, but cc is | ^^^^^ @@ -26,7 +26,7 @@ LL | let x = { 1 }; // not a warning, but cc is = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:100:17 + --> $DIR/excessive_nesting.rs:97:17 | LL | / pub mod e { LL | | pub mod f {} @@ -36,7 +36,7 @@ LL | | } // not here = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:113:18 + --> $DIR/excessive_nesting.rs:110:18 | LL | a_but_not({{{{{{{{0}}}}}}}}); | ^^^^^^^^^^^ @@ -44,7 +44,7 @@ LL | a_but_not({{{{{{{{0}}}}}}}}); = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:114:12 + --> $DIR/excessive_nesting.rs:111:12 | LL | a.a({{{{{{{{{0}}}}}}}}}); | ^^^^^^^^^^^^^ @@ -52,7 +52,7 @@ LL | a.a({{{{{{{{{0}}}}}}}}}); = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:115:12 + --> $DIR/excessive_nesting.rs:112:12 | LL | (0, {{{{{{{1}}}}}}}); | ^^^^^^^^^ @@ -60,7 +60,7 @@ LL | (0, {{{{{{{1}}}}}}}); = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:120:25 + --> $DIR/excessive_nesting.rs:117:25 | LL | if true { | _________________________^ @@ -73,7 +73,7 @@ LL | | } = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:132:29 + --> $DIR/excessive_nesting.rs:129:29 | LL | let z = (|| { | _____________________________^ @@ -85,7 +85,7 @@ LL | | })(); = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:151:13 + --> $DIR/excessive_nesting.rs:148:13 | LL | y += {{{{{5}}}}}; | ^^^^^ @@ -93,7 +93,7 @@ LL | y += {{{{{5}}}}}; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:152:20 + --> $DIR/excessive_nesting.rs:149:20 | LL | let z = y + {{{{{{{{{5}}}}}}}}}; | ^^^^^^^^^^^^^ @@ -101,7 +101,7 @@ LL | let z = y + {{{{{{{{{5}}}}}}}}}; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:153:12 + --> $DIR/excessive_nesting.rs:150:12 | LL | [0, {{{{{{{{{{0}}}}}}}}}}]; | ^^^^^^^^^^^^^^^ @@ -109,7 +109,7 @@ LL | [0, {{{{{{{{{{0}}}}}}}}}}]; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:154:25 + --> $DIR/excessive_nesting.rs:151:25 | LL | let mut xx = [0; {{{{{{{{100}}}}}}}}]; | ^^^^^^^^^^^^^ @@ -117,7 +117,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:155:11 + --> $DIR/excessive_nesting.rs:152:11 | LL | xx[{{{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}}}]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -125,7 +125,7 @@ LL | xx[{{{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}}}]; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:156:13 + --> $DIR/excessive_nesting.rs:153:13 | LL | &mut {{{{{{{{{{y}}}}}}}}}}; | ^^^^^^^^^^^^^^^ @@ -133,7 +133,7 @@ LL | &mut {{{{{{{{{{y}}}}}}}}}}; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:158:17 + --> $DIR/excessive_nesting.rs:155:17 | LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}} | ^^^^ @@ -141,7 +141,7 @@ LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}} = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:158:28 + --> $DIR/excessive_nesting.rs:155:28 | LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}} | ^^^^^^^^^^ @@ -149,7 +149,7 @@ LL | for i in {{{{xx}}}} {{{{{{{{}}}}}}}} = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:160:28 + --> $DIR/excessive_nesting.rs:157:28 | LL | while let Some(i) = {{{{{{Some(1)}}}}}} {{{{{{{}}}}}}} | ^^^^^^^^^^^^^ @@ -157,7 +157,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:160:48 + --> $DIR/excessive_nesting.rs:157:48 | LL | while let Some(i) = {{{{{{Some(1)}}}}}} {{{{{{{}}}}}}} | ^^^^^^^^ @@ -165,7 +165,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:162:14 + --> $DIR/excessive_nesting.rs:159:14 | LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}} | ^^^^^^^^^^^^^^ @@ -173,7 +173,7 @@ LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}} = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:162:35 + --> $DIR/excessive_nesting.rs:159:35 | LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}} | ^^^^^^^^^^^^ @@ -181,7 +181,7 @@ LL | while {{{{{{{{true}}}}}}}} {{{{{{{{{}}}}}}}}} = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:164:23 + --> $DIR/excessive_nesting.rs:161:23 | LL | let d = D { d: {{{{{{{{{{{{{{{{{{{{{{{3}}}}}}}}}}}}}}}}}}}}}}} }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -189,7 +189,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:166:8 + --> $DIR/excessive_nesting.rs:163:8 | LL | {{{{1;}}}}..{{{{{{3}}}}}}; | ^^^^ @@ -197,7 +197,7 @@ LL | {{{{1;}}}}..{{{{{{3}}}}}}; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:166:20 + --> $DIR/excessive_nesting.rs:163:20 | LL | {{{{1;}}}}..{{{{{{3}}}}}}; | ^^^^^^^ @@ -205,7 +205,7 @@ LL | {{{{1;}}}}..{{{{{{3}}}}}}; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:167:8 + --> $DIR/excessive_nesting.rs:164:8 | LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}}; | ^^^^ @@ -213,7 +213,7 @@ LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}}; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:167:21 + --> $DIR/excessive_nesting.rs:164:21 | LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}}; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -221,7 +221,7 @@ LL | {{{{1;}}}}..={{{{{{{{{{{{{{{{{{{{{{{{{{6}}}}}}}}}}}}}}}}}}}}}}}}}}; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:168:10 + --> $DIR/excessive_nesting.rs:165:10 | LL | ..{{{{{{{5}}}}}}}; | ^^^^^^^^^ @@ -229,7 +229,7 @@ LL | ..{{{{{{{5}}}}}}}; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:169:11 + --> $DIR/excessive_nesting.rs:166:11 | LL | ..={{{{{3}}}}}; | ^^^^^ @@ -237,7 +237,7 @@ LL | ..={{{{{3}}}}}; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:170:8 + --> $DIR/excessive_nesting.rs:167:8 | LL | {{{{{1;}}}}}..; | ^^^^^^ @@ -245,7 +245,7 @@ LL | {{{{{1;}}}}}..; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:172:20 + --> $DIR/excessive_nesting.rs:169:20 | LL | loop { break {{{{1}}}} }; | ^^^^^ @@ -253,7 +253,7 @@ LL | loop { break {{{{1}}}} }; = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:173:13 + --> $DIR/excessive_nesting.rs:170:13 | LL | loop {{{{{{}}}}}} | ^^^^^^ @@ -261,7 +261,7 @@ LL | loop {{{{{{}}}}}} = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:175:14 + --> $DIR/excessive_nesting.rs:172:14 | LL | match {{{{{{true}}}}}} { | ^^^^^^^^^^ @@ -269,7 +269,7 @@ LL | match {{{{{{true}}}}}} { = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:176:20 + --> $DIR/excessive_nesting.rs:173:20 | LL | true => {{{{}}}}, | ^^ @@ -277,7 +277,7 @@ LL | true => {{{{}}}}, = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:177:21 + --> $DIR/excessive_nesting.rs:174:21 | LL | false => {{{{}}}}, | ^^ @@ -285,7 +285,7 @@ LL | false => {{{{}}}}, = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:183:17 + --> $DIR/excessive_nesting.rs:180:17 | LL | / { LL | | println!("warning! :)"); @@ -295,7 +295,7 @@ LL | | } = help: try refactoring your code to minimize nesting error: this block is too nested - --> $DIR/excessive_nesting.rs:192:28 + --> $DIR/excessive_nesting.rs:189:28 | LL | async fn c() -> u32 {{{{{{{0}}}}}}} | ^^^^^^^^^ @@ -303,7 +303,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:198:8 + --> $DIR/excessive_nesting.rs:195:8 | LL | {{{{b().await}}}}; | ^^^^^^^^^^^ diff --git a/tests/ui-toml/excessive_nesting/set/clippy.toml b/tests/ui-toml/excessive_nesting/set/clippy.toml deleted file mode 100644 index 10be2751a867..000000000000 --- a/tests/ui-toml/excessive_nesting/set/clippy.toml +++ /dev/null @@ -1 +0,0 @@ -excessive-nesting-threshold = 3