From 8393df5cb0108e8a51cc0bc7dd4be6e8d9f48a7e Mon Sep 17 00:00:00 2001 From: Karol Zwolak Date: Wed, 26 Feb 2025 22:16:22 +0100 Subject: [PATCH] docs: improve documentation of `remove_nested_blocks` --- Configurations.md | 20 +++++++++++--------- src/expr.rs | 3 +++ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Configurations.md b/Configurations.md index 4dbe6cf59b5..d5b31a30bfa 100644 --- a/Configurations.md +++ b/Configurations.md @@ -2235,32 +2235,34 @@ Works similarly to [`remove_nested_parens`](#remove_nested_parens), but removes - **Possible values**: `true`, `false` - **Stable**: No +Blocks with any sort of comments or attributes, `unsafe` and `const` blocks will not be removed. -#### `true`: +#### `false` (default): ```rust fn main() { { - foo(); + { + // comment + { + foo(); + } + } } } ``` -#### `false` (default): +#### `true`: ```rust fn main() { { + // comment { - { - { - foo(); - } - } + foo(); } } } ``` - ## `reorder_impl_items` Reorder impl items. `type` and `const` are put first, then macros and methods. diff --git a/src/expr.rs b/src/expr.rs index 5d007483a38..483dc0a9cb9 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -676,12 +676,15 @@ fn rewrite_block_inner( context: &RewriteContext<'_>, shape: Shape, can_be_removed: bool, + // ^ this is a fix for const blocks, which are not removed, but are passed identically to blocks ) -> RewriteResult { debug!("rewrite_block : {:?}", context.snippet(block.span)); let prefix = block_prefix(context, block, shape)?; let no_attrs = attrs.is_none() || attrs.unwrap().is_empty(); + // If the option `remove_nested_blocks` is enabled, we remove all unnecessary nested blocks. + // Blocks with any sort of comments or attributes, unsafe and const blocks will not be removed. if context.config.remove_nested_blocks() && can_be_removed && prefix.is_empty()