Skip to content

Commit 5da3455

Browse files
committed
Check if from proc macro and better tests
1 parent 725399a commit 5da3455

File tree

11 files changed

+622
-233
lines changed

11 files changed

+622
-233
lines changed

clippy_lints/src/excessive_nesting.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_help;
1+
use clippy_utils::{diagnostics::span_lint_and_help, source::snippet};
22
use rustc_ast::{
33
node_id::NodeSet,
44
visit::{walk_block, walk_item, Visitor},
@@ -86,6 +86,10 @@ impl ExcessiveNesting {
8686

8787
impl EarlyLintPass for ExcessiveNesting {
8888
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
89+
if self.excessive_nesting_threshold == 0 {
90+
return;
91+
}
92+
8993
let mut visitor = NestingVisitor {
9094
conf: self,
9195
cx,
@@ -114,9 +118,7 @@ struct NestingVisitor<'conf, 'cx> {
114118

115119
impl NestingVisitor<'_, '_> {
116120
fn check_indent(&mut self, span: Span, id: NodeId) -> bool {
117-
let threshold = self.conf.excessive_nesting_threshold;
118-
119-
if threshold != 0 && self.nest_level > threshold && !in_external_macro(self.cx.sess(), span) {
121+
if self.nest_level > self.conf.excessive_nesting_threshold && !in_external_macro(self.cx.sess(), span) {
120122
self.conf.nodes.insert(id);
121123

122124
return true;
@@ -132,6 +134,13 @@ impl<'conf, 'cx> Visitor<'_> for NestingVisitor<'conf, 'cx> {
132134
return;
133135
}
134136

137+
// TODO: This should be rewritten using `LateLintPass` so we can use `is_from_proc_macro` instead,
138+
// but for now, this is fine.
139+
let snippet = snippet(self.cx, block.span, "{}").trim().to_owned();
140+
if !snippet.starts_with('{') || !snippet.ends_with('}') {
141+
return;
142+
}
143+
135144
self.nest_level += 1;
136145

137146
if !self.check_indent(block.span, block.id) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
excessive-nesting-threshold = 4

tests/ui-toml/excessive_nesting/auxiliary/macro_rules.rs

-7
This file was deleted.

tests/ui-toml/excessive_nesting/auxiliary/mod.rs

-16
This file was deleted.

0 commit comments

Comments
 (0)