Skip to content

Remove nested parens #2519

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 3 commits into from
Mar 9, 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
36 changes: 32 additions & 4 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub fn format_expr(
let callee_str = callee.rewrite(context, shape)?;
rewrite_call(context, &callee_str, args, inner_span, shape)
}
ast::ExprKind::Paren(ref subexpr) => rewrite_paren(context, subexpr, shape),
ast::ExprKind::Paren(ref subexpr) => rewrite_paren(context, subexpr, shape, expr.span),
ast::ExprKind::Binary(ref op, ref lhs, ref rhs) => {
// FIXME: format comments between operands and operator
rewrite_pair(
Expand Down Expand Up @@ -2425,8 +2425,36 @@ fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
result
}

fn rewrite_paren(context: &RewriteContext, subexpr: &ast::Expr, shape: Shape) -> Option<String> {
fn rewrite_paren(
context: &RewriteContext,
mut subexpr: &ast::Expr,
shape: Shape,
mut span: Span,
) -> Option<String> {
debug!("rewrite_paren, shape: {:?}", shape);

// Extract comments within parens.
let mut pre_comment;
let mut post_comment;
loop {
// 1 = "(" or ")"
let pre_span = mk_sp(span.lo() + BytePos(1), subexpr.span.lo());
let post_span = mk_sp(subexpr.span.hi(), span.hi() - BytePos(1));
pre_comment = rewrite_missing_comment(pre_span, shape, context)?;
post_comment = rewrite_missing_comment(post_span, shape, context)?;

// 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() {
span = subexpr.span;
subexpr = subsubexpr;
continue;
}
}

break;
}

let total_paren_overhead = paren_overhead(context);
let paren_overhead = total_paren_overhead / 2;
let sub_shape = shape
Expand All @@ -2435,9 +2463,9 @@ fn rewrite_paren(context: &RewriteContext, subexpr: &ast::Expr, shape: Shape) ->

let paren_wrapper = |s: &str| {
if context.config.spaces_within_parens_and_brackets() && !s.is_empty() {
format!("( {} )", s)
format!("( {}{}{} )", pre_comment, s, post_comment)
} else {
format!("({})", s)
format!("({}{}{})", pre_comment, s, post_comment)
}
};

Expand Down
8 changes: 8 additions & 0 deletions tests/source/paren.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Remove nested parens.

fn main() {
let x = (((1)));
let y = (/* comment */((2)));
let z = (((3)/* comment */));
let a = (((4/* comment */)));
}
6 changes: 2 additions & 4 deletions tests/target/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ fn foo() -> bool {
10000 * 30000000000 + 40000 / 1002200000000 - 50000 * sqrt(-1),
trivial_value,
);
(((((((((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ a
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ aaaaa)))))))));
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + a
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa + aaaaa);

{
for _ in 0..10 {}
Expand Down
8 changes: 8 additions & 0 deletions tests/target/paren.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Remove nested parens.

fn main() {
let x = (1);
let y = (/* comment */(2));
let z = ((3)/* comment */);
let a = (4/* comment */);
}