diff --git a/book/src/lint_configuration.md b/book/src/lint_configuration.md index 7c850b4b023a..0b0b0c5dc4f0 100644 --- a/book/src/lint_configuration.md +++ b/book/src/lint_configuration.md @@ -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:** diff --git a/clippy_config/src/conf.rs b/clippy_config/src/conf.rs index 87158cec42b2..98cd6b2f46fc 100644 --- a/clippy_config/src/conf.rs +++ b/clippy_config/src/conf.rs @@ -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, diff --git a/clippy_config/src/lib.rs b/clippy_config/src/lib.rs index 67904b4fcdc8..d91b2600eb91 100644 --- a/clippy_config/src/lib.rs +++ b/clippy_config/src/lib.rs @@ -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; diff --git a/clippy_dev/src/lib.rs b/clippy_dev/src/lib.rs index 3361443196ab..73736644376a 100644 --- a/clippy_dev/src/lib.rs +++ b/clippy_dev/src/lib.rs @@ -1,3 +1,4 @@ +#![allow(clippy::excessive_nesting)] #![feature( rustc_private, exit_status_error, diff --git a/clippy_lints/src/excessive_nesting.rs b/clippy_lints/src/excessive_nesting.rs index 1d3ae8949441..c6d4274b8aae 100644 --- a/clippy_lints/src/excessive_nesting.rs +++ b/clippy_lints/src/excessive_nesting.rs @@ -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. /// @@ -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]); @@ -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, diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 92eb3d7a7c59..681ab5c5c593 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -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, @@ -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, it’s used in `update_lints` +// begin lints modules, do not remove this comment, it's used in `update_lints` mod absolute_paths; mod almost_complete_range; mod approx_const; @@ -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, it’s 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; diff --git a/clippy_lints_internal/src/lib.rs b/clippy_lints_internal/src/lib.rs index 43cde86504f5..b9305613d249 100644 --- a/clippy_lints_internal/src/lib.rs +++ b/clippy_lints_internal/src/lib.rs @@ -1,3 +1,4 @@ +#![allow(clippy::excessive_nesting)] #![feature(rustc_private)] #![allow( clippy::missing_docs_in_private_items, diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index 42136fdbc51a..db1480068940 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -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)] diff --git a/tests/compile-test.rs b/tests/compile-test.rs index 78b27e2f6139..39e42e6e58ed 100644 --- a/tests/compile-test.rs +++ b/tests/compile-test.rs @@ -1,3 +1,4 @@ +#![allow(clippy::excessive_nesting)] #![feature(rustc_private)] #![warn(rust_2018_idioms, unused_lifetimes)] #![allow(unused_extern_crates)] diff --git a/tests/ui/excessive_nesting.rs b/tests/ui/excessive_nesting.rs new file mode 100644 index 000000000000..3e8bb213273e --- /dev/null +++ b/tests/ui/excessive_nesting.rs @@ -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 }; + } + } + } + } + } + } +} diff --git a/tests/ui/excessive_nesting.stderr b/tests/ui/excessive_nesting.stderr new file mode 100644 index 000000000000..ceb5e965a72f --- /dev/null +++ b/tests/ui/excessive_nesting.stderr @@ -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 +