Skip to content

Format a match arm with the beginning vertical bar #2464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions rustfmt-core/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1463,11 +1468,15 @@ fn rewrite_match_pattern(
context: &RewriteContext,
pats: &[ptr::P<ast::Pat>],
guard: &Option<ptr::P<ast::Expr>>,
has_beginning_vert: bool,
shape: Shape,
) -> Option<String> {
// 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))
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion rustfmt-core/src/spanned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand Down
11 changes: 11 additions & 0 deletions rustfmt-core/tests/source/match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}
10 changes: 10 additions & 0 deletions rustfmt-core/tests/target/match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}