Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion book/src/lint_configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ The maximum size of an enum's variant to avoid box suggestion
## `excessive-nesting-threshold`
The maximum amount of nesting a block can reside in

**Default Value:** `0`
**Default Value:** `6`

---
**Affected lints:**
Expand Down
2 changes: 1 addition & 1 deletion clippy_config/src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ define_Conf! {
enum_variant_size_threshold: u64 = 200,
/// The maximum amount of nesting a block can reside in
#[lints(excessive_nesting)]
excessive_nesting_threshold: u64 = 0,
excessive_nesting_threshold: u64 = 6,
/// The maximum byte size a `Future` can have, before it triggers the `clippy::large_futures` lint
#[lints(large_futures)]
future_size_threshold: u64 = 16 * 1024,
Expand Down
2 changes: 2 additions & 0 deletions clippy_config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
rustc::untranslatable_diagnostic
)]
#![deny(clippy::derive_deserialize_allowing_unknown)]
#![allow(clippy::excessive_nesting)]
#![warn(clippy::pedantic)]

extern crate rustc_data_structures;
extern crate rustc_errors;
Expand Down
1 change: 1 addition & 0 deletions clippy_dev/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::excessive_nesting)]
#![feature(
rustc_private,
exit_status_error,
Expand Down
8 changes: 1 addition & 7 deletions clippy_lints/src/excessive_nesting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ declare_clippy_lint! {
/// ### What it does
/// Checks for blocks which are nested beyond a certain threshold.
///
/// 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.
///
/// ### Why is this bad?
/// It can severely hinder readability.
///
Expand Down Expand Up @@ -58,7 +56,7 @@ declare_clippy_lint! {
/// ```
#[clippy::version = "1.72.0"]
pub EXCESSIVE_NESTING,
complexity,
pedantic,
"checks for blocks nested beyond a certain threshold"
}
impl_lint_pass!(ExcessiveNesting => [EXCESSIVE_NESTING]);
Expand Down Expand Up @@ -92,10 +90,6 @@ 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
7 changes: 4 additions & 3 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
clippy::must_use_candidate,
rustc::diagnostic_outside_of_impl,
rustc::untranslatable_diagnostic,
clippy::literal_string_with_formatting_args
clippy::literal_string_with_formatting_args,
clippy::excessive_nesting
)]
#![warn(
trivial_casts,
Expand Down Expand Up @@ -69,7 +70,7 @@ pub mod ctfe; // Very important lint, do not remove (rust#125116)
pub mod declared_lints;
pub mod deprecated_lints;

// begin lints modules, do not remove this comment, its used in `update_lints`
// begin lints modules, do not remove this comment, it's used in `update_lints`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to update the corresponding text in update_lints if you wanna do this. Though it should be in a separate PR.

mod absolute_paths;
mod almost_complete_range;
mod approx_const;
Expand Down Expand Up @@ -404,7 +405,7 @@ mod zero_div_zero;
mod zero_repeat_side_effects;
mod zero_sized_map_values;
mod zombie_processes;
// end lints modules, do not remove this comment, its used in `update_lints`
// end lints modules, do not remove this comment, it's used in `update_lints`

use clippy_config::{Conf, get_configuration_metadata, sanitize_explanation};
use clippy_utils::macros::FormatArgsStorage;
Expand Down
1 change: 1 addition & 0 deletions clippy_lints_internal/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::excessive_nesting)]
#![feature(rustc_private)]
#![allow(
clippy::missing_docs_in_private_items,
Expand Down
3 changes: 2 additions & 1 deletion clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![allow(clippy::excessive_nesting)]
#![feature(rustc_private)]
#![feature(box_patterns)]
#![feature(if_let_guard)]
#![feature(macro_metavar_expr)]
#![feature(never_type)]
#![feature(rustc_private)]
#![feature(assert_matches)]
#![feature(unwrap_infallible)]
#![feature(array_windows)]
Expand Down
1 change: 1 addition & 0 deletions tests/compile-test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::excessive_nesting)]
#![feature(rustc_private)]
#![warn(rust_2018_idioms, unused_lifetimes)]
#![allow(unused_extern_crates)]
Expand Down
96 changes: 96 additions & 0 deletions tests/ui/excessive_nesting.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#![warn(clippy::pedantic)]
#![allow(
unused,
clippy::let_and_return,
clippy::redundant_closure_call,
clippy::no_effect,
clippy::unnecessary_operation,
clippy::needless_if,
clippy::single_match,
clippy::unused_self
)]

fn main() {
// This should not trigger with default threshold of 6
let a = {
let b = {
let c = {
let d = {
let e = {
let f = { 42 };
//~^ ERROR: this block is too nested
f
};
e
};
d
};
c
};
b
};

// This should trigger with default threshold of 6
let x = {
let y = {
let z = {
let w = {
let v = {
let u = {
//~^ ERROR: this block is too nested
let t = { 42 };
t
};
u
};
v
};
w
};
z
};
y
};
}

struct A;

impl A {
fn test() {
// This should not trigger
struct B;
impl B {
fn test() {
struct C;
impl C {
fn test() {
if true {
//~^ ERROR: this block is too nested
let x = { 1 };
}
}
}
}
}
}
}

trait TestTrait {
fn test() {
// This should trigger (7 levels)
struct B;
impl B {
fn test() {
struct C;
impl C {
fn test() {
if true {
//~^ ERROR: this block is too nested
let x = { 1 };
}
}
}
}
}
}
}
49 changes: 49 additions & 0 deletions tests/ui/excessive_nesting.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
error: this block is too nested
--> tests/ui/excessive_nesting.rs:20:33
|
LL | let f = { 42 };
| ^^^^^^
|
= help: try refactoring your code to minimize nesting
= note: `-D clippy::excessive-nesting` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::excessive_nesting)]`

error: this block is too nested
--> tests/ui/excessive_nesting.rs:39:33
|
LL | let u = {
| _________________________________^
LL | |
LL | | let t = { 42 };
LL | | t
LL | | };
| |_________________________^
|
= help: try refactoring your code to minimize nesting

error: this block is too nested
--> tests/ui/excessive_nesting.rs:67:33
|
LL | if true {
| _________________________________^
LL | |
LL | | let x = { 1 };
LL | | }
| |_________________________^
|
= help: try refactoring your code to minimize nesting

error: this block is too nested
--> tests/ui/excessive_nesting.rs:87:33
|
LL | if true {
| _________________________________^
LL | |
LL | | let x = { 1 };
LL | | }
| |_________________________^
|
= help: try refactoring your code to minimize nesting

error: aborting due to 4 previous errors

Loading