Skip to content

Commit 17b04f1

Browse files
authored
Merge pull request #2677 from csmoe/remove_nested_parens_opt
Add remove nested parens option
2 parents 0cc4a8d + 5b12158 commit 17b04f1

File tree

9 files changed

+40
-3
lines changed

9 files changed

+40
-3
lines changed

Configurations.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,28 @@ fn dolor() -> usize {}
12701270
fn adipiscing() -> usize {}
12711271
```
12721272

1273+
## `remove_nested_parens`
1274+
1275+
Remove nested parens.
1276+
1277+
- **Defalut value**: `false`,
1278+
- **Possible values**: `true`, `false`
1279+
- **Stable**: No
1280+
1281+
#### `false` (default):
1282+
```rust
1283+
fn main() {
1284+
((((foo()))));
1285+
}
1286+
```
1287+
1288+
#### `true`:
1289+
```rust
1290+
fn main() {
1291+
(foo());
1292+
}
1293+
```
1294+
12731295

12741296
## `reorder_imports`
12751297

src/config/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ create_config! {
106106
"Maximum number of blank lines which can be put between items.";
107107
blank_lines_lower_bound: usize, 0, false,
108108
"Minimum number of blank lines which must be put between items.";
109+
remove_nested_parens: bool, false, false,
110+
"Remove nested parens.";
109111

110112
// Options that can change the source code beyond whitespace/blocks (somewhat linty things)
111113
merge_derives: bool, true, true, "Merge multiple `#[derive(...)]` into a single one";

src/expr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,7 @@ fn rewrite_paren(
14741474
// Extract comments within parens.
14751475
let mut pre_comment;
14761476
let mut post_comment;
1477+
let remove_nested_parens = context.config.remove_nested_parens();
14771478
loop {
14781479
// 1 = "(" or ")"
14791480
let pre_span = mk_sp(span.lo() + BytePos(1), subexpr.span.lo());
@@ -1483,7 +1484,7 @@ fn rewrite_paren(
14831484

14841485
// Remove nested parens if there are no comments.
14851486
if let ast::ExprKind::Paren(ref subsubexpr) = subexpr.node {
1486-
if pre_comment.is_empty() && post_comment.is_empty() {
1487+
if remove_nested_parens && pre_comment.is_empty() && post_comment.is_empty() {
14871488
span = subexpr.span;
14881489
subexpr = subsubexpr;
14891490
continue;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-remove_nested_parens: true
2+
3+
fn main() {
4+
((((((foo()))))));
5+
}

tests/source/expr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// rustfmt-normalize_comments: true
22
// rustfmt-wrap_comments: true
3+
// rustfmt-remove_nested_parens: true
34
// Test expressions
45

56
fn foo() -> bool {

tests/source/paren.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Remove nested parens.
1+
// rustfmt-remove_nested_parens: true
22

33
fn main() {
44
let x = (((1)));
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// rustfmt-remove_nested_parens: true
2+
3+
fn main() {
4+
(foo());
5+
}

tests/target/expr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// rustfmt-normalize_comments: true
22
// rustfmt-wrap_comments: true
3+
// rustfmt-remove_nested_parens: true
34
// Test expressions
45

56
fn foo() -> bool {

tests/target/paren.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Remove nested parens.
1+
// rustfmt-remove_nested_parens: true
22

33
fn main() {
44
let x = (1);

0 commit comments

Comments
 (0)