Merge pull request #2677 from csmoe/remove_nested_parens_opt

Add remove nested parens option
This commit is contained in:
Nick Cameron 2018-05-06 14:01:17 +12:00 committed by GitHub
commit 17b04f181d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 40 additions and 3 deletions

View file

@ -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`

View file

@ -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";

View file

@ -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());
@ -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;

View file

@ -0,0 +1,5 @@
// rustfmt-remove_nested_parens: true
fn main() {
((((((foo()))))));
}

View file

@ -1,5 +1,6 @@
// rustfmt-normalize_comments: true
// rustfmt-wrap_comments: true
// rustfmt-remove_nested_parens: true
// Test expressions
fn foo() -> bool {

View file

@ -1,4 +1,4 @@
// Remove nested parens.
// rustfmt-remove_nested_parens: true
fn main() {
let x = (((1)));

View file

@ -0,0 +1,5 @@
// rustfmt-remove_nested_parens: true
fn main() {
(foo());
}

View file

@ -1,5 +1,6 @@
// rustfmt-normalize_comments: true
// rustfmt-wrap_comments: true
// rustfmt-remove_nested_parens: true
// Test expressions
fn foo() -> bool {

View file

@ -1,4 +1,4 @@
// Remove nested parens.
// rustfmt-remove_nested_parens: true
fn main() {
let x = (1);