diff --git a/rustfmt-core/src/expr.rs b/rustfmt-core/src/expr.rs index 1d572f642e2..d21c2726310 100644 --- a/rustfmt-core/src/expr.rs +++ b/rustfmt-core/src/expr.rs @@ -1406,17 +1406,22 @@ fn rewrite_match_arm( } else { (mk_sp(arm.span().lo(), arm.span().lo()), String::new()) }; - let pats_str = - rewrite_match_pattern(context, &arm.pats, &arm.guard, shape).and_then(|pats_str| { - combine_strs_with_missing_comments( - context, - &attrs_str, - &pats_str, - missing_span, - shape, - false, - ) - })?; + let pats_str = rewrite_match_pattern( + context, + &arm.pats, + &arm.guard, + arm.beginning_vert.is_some(), + shape, + ).and_then(|pats_str| { + combine_strs_with_missing_comments( + context, + &attrs_str, + &pats_str, + missing_span, + shape, + false, + ) + })?; rewrite_match_body( context, &arm.body, @@ -1463,11 +1468,15 @@ fn rewrite_match_pattern( context: &RewriteContext, pats: &[ptr::P], guard: &Option>, + has_beginning_vert: bool, shape: Shape, ) -> Option { // Patterns // 5 = ` => {` - let pat_shape = shape.sub_width(5)?; + // 2 = `| ` + let pat_shape = shape + .sub_width(5)? + .offset_left(if has_beginning_vert { 2 } else { 0 })?; let pat_strs = pats.iter() .map(|p| p.rewrite(context, pat_shape)) @@ -1498,11 +1507,12 @@ fn rewrite_match_pattern( config: context.config, }; let pats_str = write_list(&items, &fmt)?; + let beginning_vert = if has_beginning_vert { "| " } else { "" }; // Guard let guard_str = rewrite_guard(context, guard, shape, trimmed_last_line_width(&pats_str))?; - Some(format!("{}{}", pats_str, guard_str)) + Some(format!("{}{}{}", beginning_vert, pats_str, guard_str)) } // (extend, body) diff --git a/rustfmt-core/src/spanned.rs b/rustfmt-core/src/spanned.rs index a431f3a544a..20dd8438798 100644 --- a/rustfmt-core/src/spanned.rs +++ b/rustfmt-core/src/spanned.rs @@ -89,7 +89,12 @@ impl Spanned for ast::Ty { impl Spanned for ast::Arm { fn span(&self) -> Span { - span_with_attrs_lo_hi!(self, self.pats[0].span.lo(), self.body.span.hi()) + let lo = if let Some(sp) = self.beginning_vert { + sp.lo() + } else { + self.pats[0].span.lo() + }; + span_with_attrs_lo_hi!(self, lo, self.body.span.hi()) } } diff --git a/rustfmt-core/tests/source/match.rs b/rustfmt-core/tests/source/match.rs index 23df0d3b681..f38bf7cca97 100644 --- a/rustfmt-core/tests/source/match.rs +++ b/rustfmt-core/tests/source/match.rs @@ -451,3 +451,14 @@ fn issue_2152() { "bind" | "writev" | "readv" | "sendmsg" | "recvmsg" if android && (aarch64 || x86_64) => true, } } + +// #2462 +// Preserve a `|` at the beginning of a match arm. +fn match_with_beginning_vert() { + let x = Foo::A; + match x { + | Foo::A + | Foo::B => println!("AB"), + | Foo::C => println!("C"), + } +} diff --git a/rustfmt-core/tests/target/match.rs b/rustfmt-core/tests/target/match.rs index 83ee4f97b73..72ffa966c01 100644 --- a/rustfmt-core/tests/target/match.rs +++ b/rustfmt-core/tests/target/match.rs @@ -483,3 +483,13 @@ fn issue_2152() { } } } + +// #2462 +// Preserve a `|` at the beginning of a match arm. +fn match_with_beginning_vert() { + let x = Foo::A; + match x { + | Foo::A | Foo::B => println!("AB"), + | Foo::C => println!("C"), + } +}