about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCentri3 <114838443+Centri3@users.noreply.github.com>2023-05-06 18:43:53 -0500
committerCentri3 <114838443+Centri3@users.noreply.github.com>2023-06-07 18:34:34 -0500
commit493a23e957f9d639c13bddb19937bd19cbc6cfe2 (patch)
tree4e8dc345da1ca6bfe0a53b879ec8214d0f8c0293
parent88143ac295ba0656b78af8f9413e4074529b42d9 (diff)
downloadrust-493a23e957f9d639c13bddb19937bd19cbc6cfe2.tar.gz
rust-493a23e957f9d639c13bddb19937bd19cbc6cfe2.zip
check non-inline modules, ignore all macros
-rw-r--r--clippy_lints/src/excessive_nesting.rs24
-rw-r--r--tests/ui-toml/excessive_nesting/auxiliary/mod.rs16
-rw-r--r--tests/ui-toml/excessive_nesting/excessive_nesting.rs6
-rw-r--r--tests/ui-toml/excessive_nesting/excessive_nesting.stderr118
4 files changed, 98 insertions, 66 deletions
diff --git a/clippy_lints/src/excessive_nesting.rs b/clippy_lints/src/excessive_nesting.rs
index a133bdc2edf..5ade713f816 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 00000000000..967b3af3bca
--- /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 62cf5125816..3e3ac7c923a 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 c98c6fefe01..524282d11f7 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