Skip to content

Commit 7ad3522

Browse files
committed
Fix width bug for long patterns in match arms
Fixes failing test
1 parent a84f42d commit 7ad3522

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

src/expr.rs

+5-14
Original file line numberDiff line numberDiff line change
@@ -1229,8 +1229,6 @@ impl Rewrite for ast::Arm {
12291229
trimmed_last_line_width(&pats_str)));
12301230

12311231
let pats_str = format!("{}{}", pats_str, guard_str);
1232-
// Where the next text can start.
1233-
let line_start = trimmed_last_line_width(&pats_str);
12341232

12351233
let body = match body.node {
12361234
ast::ExprKind::Block(ref block) if !is_unsafe_block(block) &&
@@ -1249,14 +1247,12 @@ impl Rewrite for ast::Arm {
12491247
let alt_block_sep = String::from("\n") +
12501248
&shape.indent.block_only().to_string(context.config);
12511249

1250+
let pat_width = extra_offset(&pats_str, shape);
12521251
// Let's try and get the arm body on the same line as the condition.
12531252
// 4 = ` => `.len()
1254-
if shape.width > line_start + comma.len() + 4 {
1253+
if shape.width > pat_width + comma.len() + 4 {
12551254
let arm_shape =
1256-
shape.shrink_left(line_start + 4).unwrap().sub_width(comma.len()).unwrap().block();
1257-
// TODO
1258-
// let offset = Indent::new(shape.indent.block_indent,
1259-
// line_start + 4 - shape.indent.block_indent);
1255+
shape.shrink_left(pat_width + 4).unwrap().sub_width(comma.len()).unwrap().block();
12601256
let rewrite = nop_block_collapse(body.rewrite(context, arm_shape), arm_shape.width);
12611257
let is_block = if let ast::ExprKind::Block(..) = body.node {
12621258
true
@@ -1285,9 +1281,6 @@ impl Rewrite for ast::Arm {
12851281

12861282
// FIXME: we're doing a second rewrite of the expr; This may not be
12871283
// necessary.
1288-
// TODO
1289-
// let body_budget = try_opt!(shape.width.checked_sub(context.config.tab_spaces));
1290-
// let indent = shape.indent.block_only().block_indent(context.config);
12911284
let body_shape = try_opt!(shape.sub_width(context.config.tab_spaces))
12921285
.block_indent(context.config.tab_spaces);
12931286
let next_line_body = try_opt!(nop_block_collapse(body.rewrite(context, body_shape),
@@ -1338,10 +1331,8 @@ fn rewrite_guard(context: &RewriteContext,
13381331
// 4 = ` if `, 5 = ` => {`
13391332
let overhead = pattern_width + 4 + 5;
13401333
if overhead < shape.width {
1341-
let cond_str =
1342-
guard.rewrite(context,
1343-
Shape::legacy(shape.width - overhead,
1344-
shape.indent + pattern_width + 4));
1334+
let cond_shape = shape.shrink_left(pattern_width + 4).unwrap().sub_width(5).unwrap();
1335+
let cond_str = guard.rewrite(context, cond_shape);
13451336
if let Some(cond_str) = cond_str {
13461337
return Some(format!(" if {}", cond_str));
13471338
}

tests/target/long-match-arms-brace-newline.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ fn main() {
77
{
88
aaaaaaaa::Bbbbb::Ccccccccccccc(_, Some(ref x)) if x ==
99
"aaaaaaaaaaa \
10-
aaaaaaa aaaaaa" => Ok(()),
10+
aaaaaaa aaaaaa" =>
11+
{
12+
Ok(())
13+
}
1114
_ => Err(x),
1215
}
1316
}

0 commit comments

Comments
 (0)