Skip to content

Commit 4328079

Browse files
committed
add tests and config spec for new option remove_nested_blocks
1 parent 96264d2 commit 4328079

File tree

5 files changed

+238
-0
lines changed

5 files changed

+238
-0
lines changed

Diff for: Configurations.md

+33
Original file line numberDiff line numberDiff line change
@@ -2227,6 +2227,39 @@ fn main() {
22272227
}
22282228
```
22292229

2230+
## `remove_nested_blocks`
2231+
2232+
Works similarly to [`remove_nested_parens`](#remove_nested_parens), but removes nested blocks.
2233+
2234+
- **Default value**: `false`,
2235+
- **Possible values**: `true`, `false`
2236+
- **Stable**: No
2237+
2238+
2239+
#### `true`:
2240+
```rust
2241+
fn main() {
2242+
{
2243+
foo();
2244+
}
2245+
}
2246+
```
2247+
2248+
#### `false` (default):
2249+
```rust
2250+
fn main() {
2251+
{
2252+
{
2253+
{
2254+
{
2255+
foo();
2256+
}
2257+
}
2258+
}
2259+
}
2260+
}
2261+
```
2262+
22302263

22312264
## `reorder_impl_items`
22322265

Diff for: src/config/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ create_config! {
117117

118118
// Misc.
119119
remove_nested_parens: RemoveNestedParens, true, "Remove nested parens";
120+
remove_nested_blocks: RemoveNestedBlocks, false, "Remove nested blocks";
120121
combine_control_expr: CombineControlExpr, false, "Combine control expressions with function \
121122
calls";
122123
short_array_element_width_threshold: ShortArrayElementWidthThreshold, true,
@@ -796,6 +797,7 @@ space_after_colon = true
796797
spaces_around_ranges = false
797798
binop_separator = "Front"
798799
remove_nested_parens = true
800+
remove_nested_blocks = false
799801
combine_control_expr = true
800802
short_array_element_width_threshold = 10
801803
overflow_delimited_expr = false
@@ -887,6 +889,7 @@ space_after_colon = true
887889
spaces_around_ranges = false
888890
binop_separator = "Front"
889891
remove_nested_parens = true
892+
remove_nested_blocks = false
890893
combine_control_expr = true
891894
short_array_element_width_threshold = 10
892895
overflow_delimited_expr = true

Diff for: src/config/options.rs

+1
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ config_option_with_style_edition_default!(
658658

659659
// Misc.
660660
RemoveNestedParens, bool, _ => true;
661+
RemoveNestedBlocks, bool, _ => false;
661662
CombineControlExpr, bool, _ => true;
662663
ShortArrayElementWidthThreshold, usize, _ => 10;
663664
OverflowDelimitedExpr, bool, Edition2024 => true, _ => false;

Diff for: tests/source/remove_nested_blocks.rs

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// rustfmt-remove_nested_blocks: true
2+
fn foo() {}
3+
4+
// The way it is implemented right now, only 'naked' blocks are removed.
5+
// This means unsafe blocks, const blocks, blocks with labels and attributes,
6+
// blocks belonging to other constructs such as loops are not removed.
7+
fn main() {
8+
{
9+
{
10+
{
11+
foo();
12+
}
13+
}
14+
}
15+
// The next block will be removed
16+
{
17+
{
18+
// Blocks with comments are not removed
19+
{
20+
{
21+
/* second comment */
22+
{
23+
foo();
24+
}
25+
}
26+
}
27+
}
28+
}
29+
{
30+
{
31+
/*
32+
* multi-line comment
33+
*/
34+
foo();
35+
}
36+
}
37+
{{{{{{{foo();}}}}}}}
38+
{{/*comment*/{{{foo();}}}}}
39+
{{/*comment*/{{/*comment*/{foo();}}}}}
40+
{
41+
const { const {} }
42+
}
43+
const { const {} }
44+
unsafe { unsafe {} }
45+
// As const and unsafe blocks are not 'naked' blocks, they are not removed.
46+
unsafe {
47+
unsafe {
48+
{
49+
const {
50+
const {
51+
{
52+
foo();
53+
}
54+
}
55+
}
56+
}
57+
}
58+
}
59+
const {
60+
unsafe {
61+
{
62+
unsafe {
63+
const {
64+
{
65+
foo();
66+
}
67+
}
68+
}
69+
}
70+
}
71+
}
72+
{
73+
'label1: {
74+
'label2: {
75+
foo();
76+
}
77+
}
78+
}
79+
'outer: loop {
80+
{
81+
'inner: loop {
82+
{
83+
foo();
84+
}
85+
}
86+
}
87+
}
88+
if let Some(x) = 5f64.map(|x| Ok(x)) {
89+
{
90+
if let Some(x) = 5f64.map(|x| Ok(x)) {
91+
foo();
92+
}
93+
}
94+
}
95+
if false {
96+
{ {} }
97+
}
98+
#[cfg(debug)]
99+
{
100+
{ {} }
101+
}
102+
}

Diff for: tests/target/remove_nested_blocks.rs

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// rustfmt-remove_nested_blocks: true
2+
fn foo() {}
3+
4+
// The way it is implemented right now, only 'naked' blocks are removed.
5+
// This means unsafe blocks, const blocks, blocks with labels and attributes,
6+
// blocks belonging to other constructs such as loops are not removed.
7+
fn main() {
8+
{
9+
foo();
10+
}
11+
// The next block will be removed
12+
{
13+
// Blocks with comments are not removed
14+
{
15+
/* second comment */
16+
{
17+
foo();
18+
}
19+
}
20+
}
21+
{
22+
/*
23+
* multi-line comment
24+
*/
25+
foo();
26+
}
27+
{
28+
foo();
29+
}
30+
{
31+
/*comment*/
32+
{
33+
foo();
34+
}
35+
}
36+
{
37+
/*comment*/
38+
{
39+
/*comment*/
40+
{
41+
foo();
42+
}
43+
}
44+
}
45+
const { const {} }
46+
const { const {} }
47+
unsafe { unsafe {} }
48+
// As const and unsafe blocks are not 'naked' blocks, they are not removed.
49+
unsafe {
50+
unsafe {
51+
const {
52+
const {
53+
{
54+
foo();
55+
}
56+
}
57+
}
58+
}
59+
}
60+
const {
61+
unsafe {
62+
unsafe {
63+
const {
64+
{
65+
foo();
66+
}
67+
}
68+
}
69+
}
70+
}
71+
'label1: {
72+
'label2: {
73+
foo();
74+
}
75+
}
76+
'outer: loop {
77+
{
78+
'inner: loop {
79+
{
80+
foo();
81+
}
82+
}
83+
}
84+
}
85+
if let Some(x) = 5f64.map(|x| Ok(x)) {
86+
{
87+
if let Some(x) = 5f64.map(|x| Ok(x)) {
88+
foo();
89+
}
90+
}
91+
}
92+
if false {
93+
{}
94+
}
95+
#[cfg(debug)]
96+
{
97+
{}
98+
}
99+
}

0 commit comments

Comments
 (0)