Skip to content

Commit 493a23e

Browse files
committed
check non-inline modules, ignore all macros
1 parent 88143ac commit 493a23e

File tree

4 files changed

+98
-66
lines changed

4 files changed

+98
-66
lines changed

clippy_lints/src/excessive_nesting.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_span::Span;
1111
declare_clippy_lint! {
1212
/// ### What it does
1313
///
14-
/// Checks for blocks which are indented beyond a certain threshold.
14+
/// Checks for blocks which are nested beyond a certain threshold.
1515
///
1616
/// ### Why is this bad?
1717
///
@@ -106,6 +106,11 @@ impl NestingVisitor<'_, '_> {
106106

107107
impl<'conf, 'cx> Visitor<'_> for NestingVisitor<'conf, 'cx> {
108108
fn visit_block(&mut self, block: &Block) {
109+
// TODO: Probably not necessary, since any block would already be ignored by the check in visit_item
110+
if block.span.from_expansion() {
111+
return;
112+
}
113+
109114
self.nest_level += 1;
110115

111116
if !self.check_indent(block.span) {
@@ -116,6 +121,10 @@ impl<'conf, 'cx> Visitor<'_> for NestingVisitor<'conf, 'cx> {
116121
}
117122

118123
fn visit_item(&mut self, item: &Item) {
124+
if item.span.from_expansion() {
125+
return;
126+
}
127+
119128
match &item.kind {
120129
ItemKind::Trait(_) | ItemKind::Impl(_) | ItemKind::Mod(.., ModKind::Loaded(_, Inline::Yes, _)) => {
121130
self.nest_level += 1;
@@ -126,10 +135,15 @@ impl<'conf, 'cx> Visitor<'_> for NestingVisitor<'conf, 'cx> {
126135

127136
self.nest_level -= 1;
128137
},
129-
// Mod: Don't visit non-inline modules
130-
// ForeignMod: I don't think this is necessary, but just incase let's not take any chances (don't want to
131-
// cause any false positives)
132-
ItemKind::Mod(..) | ItemKind::ForeignMod(..) => {},
138+
// Reset nesting level for non-inline modules (since these are in another file)
139+
ItemKind::Mod(..) => walk_item(
140+
&mut NestingVisitor {
141+
conf: self.conf,
142+
cx: self.cx,
143+
nest_level: 0,
144+
},
145+
item,
146+
),
133147
_ => walk_item(self, item),
134148
}
135149
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![rustfmt::skip]
2+
3+
mod a {
4+
mod b {
5+
mod c {
6+
mod d {
7+
mod e {}
8+
}
9+
}
10+
}
11+
}
12+
13+
fn main() {
14+
// this should lint
15+
{{{}}}
16+
}

tests/ui-toml/excessive_nesting/excessive_nesting.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#![warn(clippy::excessive_nesting)]
1111
#![allow(clippy::collapsible_if)]
1212

13+
mod auxiliary;
14+
1315
#[macro_use]
1416
extern crate macro_rules;
1517

@@ -39,7 +41,7 @@ macro_rules! xx {
3941
{
4042
{
4143
{
42-
println!("ehe");
44+
println!("ehe"); // should not lint
4345
}
4446
}
4547
}
@@ -135,7 +137,7 @@ fn main() {
135137
})();
136138

137139
excessive_nesting!(); // ensure this isn't linted in external macros
138-
xx!();
140+
xx!(); // ensure this is never linted
139141
let boo = true;
140142
!{boo as u32 + !{boo as u32 + !{boo as u32}}};
141143

0 commit comments

Comments
 (0)