Skip to content

Add remove nested parens option #2677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2018
Merged
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
22 changes: 22 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,28 @@ fn dolor() -> usize {}
fn adipiscing() -> usize {}
```

## `remove_nested_parens`

Remove nested parens.

- **Defalut value**: `false`,
- **Possible values**: `true`, `false`
- **Stable**: No

#### `false` (default):
```rust
fn main() {
((((foo()))));
}
```

#### `true`:
```rust
fn main() {
(foo());
}
```


## `reorder_imports`

Expand Down
2 changes: 2 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ create_config! {
"Maximum number of blank lines which can be put between items.";
blank_lines_lower_bound: usize, 0, false,
"Minimum number of blank lines which must be put between items.";
remove_nested_parens: bool, false, false,
"Remove nested parens.";

// Options that can change the source code beyond whitespace/blocks (somewhat linty things)
merge_derives: bool, true, true, "Merge multiple `#[derive(...)]` into a single one";
Expand Down
3 changes: 2 additions & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,7 @@ fn rewrite_paren(
// Extract comments within parens.
let mut pre_comment;
let mut post_comment;
let remove_nested_parens = context.config.remove_nested_parens();
loop {
// 1 = "(" or ")"
let pre_span = mk_sp(span.lo() + BytePos(1), subexpr.span.lo());
Expand All @@ -1483,7 +1484,7 @@ fn rewrite_paren(

// Remove nested parens if there are no comments.
if let ast::ExprKind::Paren(ref subsubexpr) = subexpr.node {
if pre_comment.is_empty() && post_comment.is_empty() {
if remove_nested_parens && pre_comment.is_empty() && post_comment.is_empty() {
span = subexpr.span;
subexpr = subsubexpr;
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// rustfmt-remove_nested_parens: true

fn main() {
((((((foo()))))));
}
1 change: 1 addition & 0 deletions tests/source/expr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// rustfmt-normalize_comments: true
// rustfmt-wrap_comments: true
// rustfmt-remove_nested_parens: true
// Test expressions

fn foo() -> bool {
Expand Down
2 changes: 1 addition & 1 deletion tests/source/paren.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Remove nested parens.
// rustfmt-remove_nested_parens: true

fn main() {
let x = (((1)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// rustfmt-remove_nested_parens: true

fn main() {
(foo());
}
1 change: 1 addition & 0 deletions tests/target/expr.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// rustfmt-normalize_comments: true
// rustfmt-wrap_comments: true
// rustfmt-remove_nested_parens: true
// Test expressions

fn foo() -> bool {
Expand Down
2 changes: 1 addition & 1 deletion tests/target/paren.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Remove nested parens.
// rustfmt-remove_nested_parens: true

fn main() {
let x = (1);
Expand Down