Skip to content

Commit ce4d5aa

Browse files
committed
Fix a bug with alignment in one-line match arms
1 parent a5f8b37 commit ce4d5aa

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/expr.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,7 @@ fn rewrite_match(context: &RewriteContext,
578578
result.push_str(&arm_indent_str);
579579

580580
let arm_str = arm.rewrite(&nested_context,
581-
context.config.max_width -
582-
arm_indent,
581+
context.config.max_width - arm_indent,
583582
arm_indent);
584583
if let Some(ref arm_str) = arm_str {
585584
result.push_str(arm_str);
@@ -694,15 +693,14 @@ impl Rewrite for ast::Arm {
694693
} else {
695694
","
696695
};
697-
let nested_indent = context.block_indent + context.config.tab_spaces;
698696

699697
// Let's try and get the arm body on the same line as the condition.
700698
// 4 = ` => `.len()
701699
if context.config.max_width > line_start + comma.len() + 4 {
702700
let budget = context.config.max_width - line_start - comma.len() - 4;
703701
if let Some(ref body_str) = body.rewrite(context,
704702
budget,
705-
nested_indent) {
703+
line_start + 4) {
706704
if first_line_width(body_str) <= budget {
707705
return Some(format!("{}{} => {}{}",
708706
attr_str.trim_left(),
@@ -720,7 +718,9 @@ impl Rewrite for ast::Arm {
720718
}
721719

722720
let body_budget = try_opt!(width.checked_sub(context.config.tab_spaces));
723-
let body_str = try_opt!(body.rewrite(context, body_budget, nested_indent));
721+
let body_str = try_opt!(body.rewrite(context,
722+
body_budget,
723+
context.block_indent));
724724
Some(format!("{}{} =>\n{}{},",
725725
attr_str.trim_left(),
726726
pats_str,

tests/target/match.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,18 @@ fn main() {
6262
None => sub_span,
6363
};
6464
}
65+
66+
// Test that one-line bodies align.
67+
fn main() {
68+
match r {
69+
Variableeeeeeeeeeeeeeeeee => ("variable",
70+
vec!("id","name","qualname","value","type","scopeid"),
71+
true,
72+
true),
73+
Enummmmmmmmmmmmmmmmmmmmm => ("enum", vec!("id","qualname","scopeid","value"), true, true),
74+
Variantttttttttttttttttttttttt => ("variant",
75+
vec!("id","name","qualname","type","value","scopeid"),
76+
true,
77+
true),
78+
}
79+
}

0 commit comments

Comments
 (0)