Skip to content

Commit 4dc5557

Browse files
authored
Merge pull request rust-lang#3070 from topecongiro/issue-3030
Format a paren expr with double slash comment
2 parents 80d7449 + 70c8e36 commit 4dc5557

File tree

3 files changed

+70
-13
lines changed

3 files changed

+70
-13
lines changed

src/expr.rs

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ use spanned::Spanned;
3939
use string::{rewrite_string, StringFormat};
4040
use types::{rewrite_path, PathContext};
4141
use utils::{
42-
colon_spaces, contains_skip, count_newlines, first_line_ends_with, first_line_width,
43-
inner_attributes, last_line_extendable, last_line_width, mk_sp, outer_attributes,
44-
ptr_vec_to_ref_vec, semicolon_for_stmt, wrap_str,
42+
colon_spaces, contains_skip, count_newlines, first_line_ends_with, inner_attributes,
43+
last_line_extendable, last_line_width, mk_sp, outer_attributes, ptr_vec_to_ref_vec,
44+
semicolon_for_stmt, wrap_str,
4545
};
4646
use vertical::rewrite_with_alignment;
4747
use visitor::FmtVisitor;
@@ -1388,13 +1388,15 @@ fn rewrite_paren(
13881388
debug!("rewrite_paren, shape: {:?}", shape);
13891389

13901390
// Extract comments within parens.
1391+
let mut pre_span;
1392+
let mut post_span;
13911393
let mut pre_comment;
13921394
let mut post_comment;
13931395
let remove_nested_parens = context.config.remove_nested_parens();
13941396
loop {
13951397
// 1 = "(" or ")"
1396-
let pre_span = mk_sp(span.lo() + BytePos(1), subexpr.span.lo());
1397-
let post_span = mk_sp(subexpr.span.hi(), span.hi() - BytePos(1));
1398+
pre_span = mk_sp(span.lo() + BytePos(1), subexpr.span.lo());
1399+
post_span = mk_sp(subexpr.span.hi(), span.hi() - BytePos(1));
13981400
pre_comment = rewrite_missing_comment(pre_span, shape, context)?;
13991401
post_comment = rewrite_missing_comment(post_span, shape, context)?;
14001402

@@ -1410,20 +1412,48 @@ fn rewrite_paren(
14101412
break;
14111413
}
14121414

1413-
// 1 `(`
1414-
let sub_shape = shape.offset_left(1).and_then(|s| s.sub_width(1))?;
1415-
1415+
// 1 = `(` and `)`
1416+
let sub_shape = shape.offset_left(1)?.sub_width(1)?;
14161417
let subexpr_str = subexpr.rewrite(context, sub_shape)?;
1417-
debug!("rewrite_paren, subexpr_str: `{:?}`", subexpr_str);
1418-
1419-
// 2 = `()`
1420-
if subexpr_str.contains('\n') || first_line_width(&subexpr_str) + 2 <= shape.width {
1418+
let fits_single_line = !pre_comment.contains("//") && !post_comment.contains("//");
1419+
if fits_single_line {
14211420
Some(format!("({}{}{})", pre_comment, &subexpr_str, post_comment))
14221421
} else {
1423-
None
1422+
rewrite_paren_in_multi_line(context, subexpr, shape, pre_span, post_span)
14241423
}
14251424
}
14261425

1426+
fn rewrite_paren_in_multi_line(
1427+
context: &RewriteContext,
1428+
subexpr: &ast::Expr,
1429+
shape: Shape,
1430+
pre_span: Span,
1431+
post_span: Span,
1432+
) -> Option<String> {
1433+
let nested_indent = shape.indent.block_indent(context.config);
1434+
let nested_shape = Shape::indented(nested_indent, context.config);
1435+
let pre_comment = rewrite_missing_comment(pre_span, nested_shape, context)?;
1436+
let post_comment = rewrite_missing_comment(post_span, nested_shape, context)?;
1437+
let subexpr_str = subexpr.rewrite(context, nested_shape)?;
1438+
1439+
let mut result = String::with_capacity(subexpr_str.len() * 2);
1440+
result.push('(');
1441+
if !pre_comment.is_empty() {
1442+
result.push_str(&nested_indent.to_string_with_newline(context.config));
1443+
result.push_str(&pre_comment);
1444+
}
1445+
result.push_str(&nested_indent.to_string_with_newline(context.config));
1446+
result.push_str(&subexpr_str);
1447+
if !post_comment.is_empty() {
1448+
result.push_str(&nested_indent.to_string_with_newline(context.config));
1449+
result.push_str(&post_comment);
1450+
}
1451+
result.push_str(&shape.indent.to_string_with_newline(context.config));
1452+
result.push(')');
1453+
1454+
Some(result)
1455+
}
1456+
14271457
fn rewrite_index(
14281458
expr: &ast::Expr,
14291459
index: &ast::Expr,

tests/source/match.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,3 +521,18 @@ fn issue_3040() {
521521
}
522522
}
523523
}
524+
525+
// #3030
526+
fn issue_3030() {
527+
match input.trim().parse::<f64>() {
528+
Ok(val)
529+
if !(
530+
// A valid number is the same as what rust considers to be valid,
531+
// except for +1., NaN, and Infinity.
532+
val.is_infinite() || val
533+
.is_nan() || input.ends_with(".") || input.starts_with("+")
534+
)
535+
=> {
536+
}
537+
}
538+
}

tests/target/match.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,3 +552,15 @@ fn issue_3040() {
552552
}
553553
}
554554
}
555+
556+
// #3030
557+
fn issue_3030() {
558+
match input.trim().parse::<f64>() {
559+
Ok(val)
560+
if !(
561+
// A valid number is the same as what rust considers to be valid,
562+
// except for +1., NaN, and Infinity.
563+
val.is_infinite() || val.is_nan() || input.ends_with(".") || input.starts_with("+")
564+
) => {}
565+
}
566+
}

0 commit comments

Comments
 (0)