Skip to content

Commit 58fb47a

Browse files
authored
Merge pull request #2519 from topecongiro/nested-parens
Remove nested parens
2 parents 5f99ebe + 9502de1 commit 58fb47a

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

src/expr.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub fn format_expr(
7979
let callee_str = callee.rewrite(context, shape)?;
8080
rewrite_call(context, &callee_str, args, inner_span, shape)
8181
}
82-
ast::ExprKind::Paren(ref subexpr) => rewrite_paren(context, subexpr, shape),
82+
ast::ExprKind::Paren(ref subexpr) => rewrite_paren(context, subexpr, shape, expr.span),
8383
ast::ExprKind::Binary(ref op, ref lhs, ref rhs) => {
8484
// FIXME: format comments between operands and operator
8585
rewrite_pair(
@@ -2425,8 +2425,36 @@ fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
24252425
result
24262426
}
24272427

2428-
fn rewrite_paren(context: &RewriteContext, subexpr: &ast::Expr, shape: Shape) -> Option<String> {
2428+
fn rewrite_paren(
2429+
context: &RewriteContext,
2430+
mut subexpr: &ast::Expr,
2431+
shape: Shape,
2432+
mut span: Span,
2433+
) -> Option<String> {
24292434
debug!("rewrite_paren, shape: {:?}", shape);
2435+
2436+
// Extract comments within parens.
2437+
let mut pre_comment;
2438+
let mut post_comment;
2439+
loop {
2440+
// 1 = "(" or ")"
2441+
let pre_span = mk_sp(span.lo() + BytePos(1), subexpr.span.lo());
2442+
let post_span = mk_sp(subexpr.span.hi(), span.hi() - BytePos(1));
2443+
pre_comment = rewrite_missing_comment(pre_span, shape, context)?;
2444+
post_comment = rewrite_missing_comment(post_span, shape, context)?;
2445+
2446+
// Remove nested parens if there are no comments.
2447+
if let ast::ExprKind::Paren(ref subsubexpr) = subexpr.node {
2448+
if pre_comment.is_empty() && post_comment.is_empty() {
2449+
span = subexpr.span;
2450+
subexpr = subsubexpr;
2451+
continue;
2452+
}
2453+
}
2454+
2455+
break;
2456+
}
2457+
24302458
let total_paren_overhead = paren_overhead(context);
24312459
let paren_overhead = total_paren_overhead / 2;
24322460
let sub_shape = shape
@@ -2435,9 +2463,9 @@ fn rewrite_paren(context: &RewriteContext, subexpr: &ast::Expr, shape: Shape) ->
24352463

24362464
let paren_wrapper = |s: &str| {
24372465
if context.config.spaces_within_parens_and_brackets() && !s.is_empty() {
2438-
format!("( {} )", s)
2466+
format!("( {}{}{} )", pre_comment, s, post_comment)
24392467
} else {
2440-
format!("({})", s)
2468+
format!("({}{}{})", pre_comment, s, post_comment)
24412469
}
24422470
};
24432471

tests/source/paren.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Remove nested parens.
2+
3+
fn main() {
4+
let x = (((1)));
5+
let y = (/* comment */((2)));
6+
let z = (((3)/* comment */));
7+
let a = (((4/* comment */)));
8+
}

tests/target/expr.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ fn foo() -> bool {
2020
10000 * 30000000000 + 40000 / 1002200000000 - 50000 * sqrt(-1),
2121
trivial_value,
2222
);
23-
(((((((((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
24-
+ a
25-
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
26-
+ aaaaa)))))))));
23+
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + a
24+
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaa);
2725

2826
{
2927
for _ in 0..10 {}

tests/target/paren.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Remove nested parens.
2+
3+
fn main() {
4+
let x = (1);
5+
let y = (/* comment */(2));
6+
let z = ((3)/* comment */);
7+
let a = (4/* comment */);
8+
}

0 commit comments

Comments
 (0)