Skip to content

Commit ea3c01e

Browse files
committed
Format a match arm with the beginning vertical bar
1 parent 197c3b4 commit ea3c01e

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

rustfmt-core/src/expr.rs

+23-13
Original file line numberDiff line numberDiff line change
@@ -1406,17 +1406,22 @@ fn rewrite_match_arm(
14061406
} else {
14071407
(mk_sp(arm.span().lo(), arm.span().lo()), String::new())
14081408
};
1409-
let pats_str =
1410-
rewrite_match_pattern(context, &arm.pats, &arm.guard, shape).and_then(|pats_str| {
1411-
combine_strs_with_missing_comments(
1412-
context,
1413-
&attrs_str,
1414-
&pats_str,
1415-
missing_span,
1416-
shape,
1417-
false,
1418-
)
1419-
})?;
1409+
let pats_str = rewrite_match_pattern(
1410+
context,
1411+
&arm.pats,
1412+
&arm.guard,
1413+
arm.beginning_vert.is_some(),
1414+
shape,
1415+
).and_then(|pats_str| {
1416+
combine_strs_with_missing_comments(
1417+
context,
1418+
&attrs_str,
1419+
&pats_str,
1420+
missing_span,
1421+
shape,
1422+
false,
1423+
)
1424+
})?;
14201425
rewrite_match_body(
14211426
context,
14221427
&arm.body,
@@ -1463,11 +1468,15 @@ fn rewrite_match_pattern(
14631468
context: &RewriteContext,
14641469
pats: &[ptr::P<ast::Pat>],
14651470
guard: &Option<ptr::P<ast::Expr>>,
1471+
has_beginning_vert: bool,
14661472
shape: Shape,
14671473
) -> Option<String> {
14681474
// Patterns
14691475
// 5 = ` => {`
1470-
let pat_shape = shape.sub_width(5)?;
1476+
// 2 = `| `
1477+
let pat_shape = shape
1478+
.sub_width(5)?
1479+
.offset_left(if has_beginning_vert { 2 } else { 0 })?;
14711480

14721481
let pat_strs = pats.iter()
14731482
.map(|p| p.rewrite(context, pat_shape))
@@ -1498,11 +1507,12 @@ fn rewrite_match_pattern(
14981507
config: context.config,
14991508
};
15001509
let pats_str = write_list(&items, &fmt)?;
1510+
let beginning_vert = if has_beginning_vert { "| " } else { "" };
15011511

15021512
// Guard
15031513
let guard_str = rewrite_guard(context, guard, shape, trimmed_last_line_width(&pats_str))?;
15041514

1505-
Some(format!("{}{}", pats_str, guard_str))
1515+
Some(format!("{}{}{}", beginning_vert, pats_str, guard_str))
15061516
}
15071517

15081518
// (extend, body)

rustfmt-core/src/spanned.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ impl Spanned for ast::Ty {
8989

9090
impl Spanned for ast::Arm {
9191
fn span(&self) -> Span {
92-
span_with_attrs_lo_hi!(self, self.pats[0].span.lo(), self.body.span.hi())
92+
let lo = if let Some(sp) = self.beginning_vert {
93+
sp.lo()
94+
} else {
95+
self.pats[0].span.lo()
96+
};
97+
span_with_attrs_lo_hi!(self, lo, self.body.span.hi())
9398
}
9499
}
95100

rustfmt-core/tests/source/match.rs

+11
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,14 @@ fn issue_2152() {
451451
"bind" | "writev" | "readv" | "sendmsg" | "recvmsg" if android && (aarch64 || x86_64) => true,
452452
}
453453
}
454+
455+
// #2462
456+
// Preserve a `|` at the beginning of a match arm.
457+
fn match_with_beginning_vert() {
458+
let x = Foo::A;
459+
match x {
460+
| Foo::A
461+
| Foo::B => println!("AB"),
462+
| Foo::C => println!("C"),
463+
}
464+
}

rustfmt-core/tests/target/match.rs

+10
Original file line numberDiff line numberDiff line change
@@ -483,3 +483,13 @@ fn issue_2152() {
483483
}
484484
}
485485
}
486+
487+
// #2462
488+
// Preserve a `|` at the beginning of a match arm.
489+
fn match_with_beginning_vert() {
490+
let x = Foo::A;
491+
match x {
492+
| Foo::A | Foo::B => println!("AB"),
493+
| Foo::C => println!("C"),
494+
}
495+
}

0 commit comments

Comments
 (0)