Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b81b902

Browse files
committed
Simplify situations in which the last sub-expr in a bin-op can go multiline without making the whole expr multiline
Fixes rust-lang#3034
1 parent a6ef302 commit b81b902

File tree

1 file changed

+13
-26
lines changed

1 file changed

+13
-26
lines changed

src/pairs.rs

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub(crate) fn rewrite_all_pairs(
4343
context: &RewriteContext,
4444
) -> Option<String> {
4545
// First we try formatting on one line.
46-
if let Some(list) = expr.flatten(context, false) {
46+
if let Some(list) = expr.flatten(false) {
4747
if let Some(r) = rewrite_pairs_one_line(&list, shape, context) {
4848
return Some(r);
4949
}
@@ -53,7 +53,7 @@ pub(crate) fn rewrite_all_pairs(
5353
// to only flatten pairs with the same operator, that way we don't
5454
// necessarily need one line per sub-expression, but we don't do anything
5555
// too funny wrt precedence.
56-
expr.flatten(context, true)
56+
expr.flatten(true)
5757
.and_then(|list| rewrite_pairs_multiline(list, shape, context))
5858
}
5959

@@ -83,33 +83,22 @@ fn rewrite_pairs_one_line<T: Rewrite>(
8383
result.push(' ');
8484
}
8585

86+
let prefix_len = result.len();
8687
let last = list.list.last().unwrap();
8788
let cur_shape = base_shape.offset_left(last_line_width(&result))?;
88-
let rewrite = last.rewrite(context, cur_shape)?;
89-
result.push_str(&rewrite);
89+
let last_rewrite = last.rewrite(context, cur_shape)?;
90+
result.push_str(&last_rewrite);
9091

9192
if first_line_width(&result) > shape.width {
9293
return None;
9394
}
9495

95-
// Check the last expression in the list. We let this expression go over
96-
// multiple lines, but we check that if this is necessary, then we can't
97-
// do better using multi-line formatting.
98-
if !is_single_line(&result) {
99-
let multiline_shape = shape.offset_left(list.separators.last().unwrap().len() + 1)?;
100-
let multiline_list: PairList<T> = PairList {
101-
list: vec![last],
102-
separators: vec![],
103-
separator_place: list.separator_place,
104-
};
105-
// Format as if we were multi-line.
106-
if let Some(rewrite) = rewrite_pairs_multiline(multiline_list, multiline_shape, context) {
107-
// Also, don't let expressions surrounded by parens go multi-line,
108-
// this looks really bad.
109-
if rewrite.starts_with('(') || is_single_line(&rewrite) {
110-
return None;
111-
}
112-
}
96+
// Check the last expression in the list. We sometimes let this expression
97+
// go over multiple lines, but we check for some ugly conditions.
98+
if !(is_single_line(&result) || last_rewrite.starts_with('{'))
99+
&& (last_rewrite.starts_with('(') || prefix_len > context.config.tab_spaces())
100+
{
101+
return None;
113102
}
114103

115104
wrap_str(result, context.config.max_width(), shape)
@@ -272,19 +261,18 @@ trait FlattenPair: Rewrite + Sized {
272261
// operator into the list. E.g,, if the source is `a * b + c`, if `_same_op`
273262
// is true, we make `[(a * b), c]` if `_same_op` is false, we make
274263
// `[a, b, c]`
275-
fn flatten(&self, _context: &RewriteContext, _same_op: bool) -> Option<PairList<Self>> {
264+
fn flatten(&self, _same_op: bool) -> Option<PairList<Self>> {
276265
None
277266
}
278267
}
279268

280269
struct PairList<'a, 'b, T: Rewrite + 'b> {
281270
list: Vec<&'b T>,
282271
separators: Vec<&'a str>,
283-
separator_place: SeparatorPlace,
284272
}
285273

286274
impl FlattenPair for ast::Expr {
287-
fn flatten(&self, context: &RewriteContext, same_op: bool) -> Option<PairList<ast::Expr>> {
275+
fn flatten(&self, same_op: bool) -> Option<PairList<ast::Expr>> {
288276
let top_op = match self.node {
289277
ast::ExprKind::Binary(op, _, _) => op.node,
290278
_ => return None,
@@ -323,7 +311,6 @@ impl FlattenPair for ast::Expr {
323311
Some(PairList {
324312
list,
325313
separators,
326-
separator_place: context.config.binop_separator(),
327314
})
328315
}
329316
}

0 commit comments

Comments
 (0)