From 7c246344f887358d129ec2451d47d46eb4f60cda Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Sat, 5 May 2018 23:13:49 +0800 Subject: [PATCH] remove nested parens option --- Configurations.md | 22 ++++++++++++++++++++++ src/config/mod.rs | 2 ++ src/expr.rs | 3 ++- tests/source/expr.rs | 1 + tests/source/paren.rs | 2 +- tests/target/expr.rs | 1 + tests/target/paren.rs | 2 +- 7 files changed, 30 insertions(+), 3 deletions(-) diff --git a/Configurations.md b/Configurations.md index 110925f8e02..428cdfd8516 100644 --- a/Configurations.md +++ b/Configurations.md @@ -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` diff --git a/src/config/mod.rs b/src/config/mod.rs index a4c0e1630a8..abfdb8049a2 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -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"; diff --git a/src/expr.rs b/src/expr.rs index 55fe2aa571b..f6dfb43b8a0 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -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; diff --git a/tests/source/expr.rs b/tests/source/expr.rs index d5b0babc5bb..91e8e300b98 100644 --- a/tests/source/expr.rs +++ b/tests/source/expr.rs @@ -1,5 +1,6 @@ // rustfmt-normalize_comments: true // rustfmt-wrap_comments: true +// rustfmt-remove_nested_parens: true // Test expressions fn foo() -> bool { diff --git a/tests/source/paren.rs b/tests/source/paren.rs index 09ac6b19e9e..ac5de236d8d 100644 --- a/tests/source/paren.rs +++ b/tests/source/paren.rs @@ -1,4 +1,4 @@ -// Remove nested parens. +// rustfmt-remove_nested_parens: true fn main() { let x = (((1))); diff --git a/tests/target/expr.rs b/tests/target/expr.rs index 911a44f0630..977c7f2331b 100644 --- a/tests/target/expr.rs +++ b/tests/target/expr.rs @@ -1,5 +1,6 @@ // rustfmt-normalize_comments: true // rustfmt-wrap_comments: true +// rustfmt-remove_nested_parens: true // Test expressions fn foo() -> bool { diff --git a/tests/target/paren.rs b/tests/target/paren.rs index 38569919403..3ffa2ceb474 100644 --- a/tests/target/paren.rs +++ b/tests/target/paren.rs @@ -1,4 +1,4 @@ -// Remove nested parens. +// rustfmt-remove_nested_parens: true fn main() { let x = (1);