Skip to content

Commit

Permalink
Check if from proc macro and better tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Centri3 committed Jun 7, 2023
1 parent 725399a commit 5da3455
Show file tree
Hide file tree
Showing 11 changed files with 622 additions and 233 deletions.
17 changes: 13 additions & 4 deletions clippy_lints/src/excessive_nesting.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions tests/ui-toml/excessive_nesting/above/clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
excessive-nesting-threshold = 4
7 changes: 0 additions & 7 deletions tests/ui-toml/excessive_nesting/auxiliary/macro_rules.rs

This file was deleted.

16 changes: 0 additions & 16 deletions tests/ui-toml/excessive_nesting/auxiliary/mod.rs

This file was deleted.

Loading

0 comments on commit 5da3455

Please sign in to comment.