Skip to content

Commit

Permalink
Strip leading | in match arm patterns
Browse files Browse the repository at this point in the history
This adresses issue rust-lang#2621

This commit turns out to be a partial revert of
ea3c01e

The rationale is that a `|` character preceding a match pattern is not
semantically relevant and therefore should be considered a
style/formatting choice.

A discussion concluded that the best way to emit consistant formatting
here was to strip the leading `|`

Discussion at rust-lang/style-team#119
  • Loading branch information
Mike-Baker committed Apr 14, 2018
1 parent 84598bd commit b6fb2fd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 27 deletions.
40 changes: 15 additions & 25 deletions src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<'a> Spanned for ArmWrapper<'a> {

impl<'a> Rewrite for ArmWrapper<'a> {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
rewrite_match_arm(context, self.arm, shape, self.is_last, self.beginning_vert)
rewrite_match_arm(context, self.arm, shape, self.is_last)
}
}

Expand Down Expand Up @@ -231,7 +231,6 @@ fn rewrite_match_arm(
arm: &ast::Arm,
shape: Shape,
is_last: bool,
beginning_vert: Option<BytePos>,
) -> Option<String> {
let (missing_span, attrs_str) = if !arm.attrs.is_empty() {
if contains_skip(&arm.attrs) {
Expand All @@ -251,22 +250,18 @@ fn rewrite_match_arm(
} else {
(mk_sp(arm.span().lo(), arm.span().lo()), String::new())
};
let pats_str = rewrite_match_pattern(
context,
&ptr_vec_to_ref_vec(&arm.pats),
&arm.guard,
beginning_vert.is_some(),
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, &ptr_vec_to_ref_vec(&arm.pats), &arm.guard, 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 All @@ -281,22 +276,17 @@ fn rewrite_match_pattern(
context: &RewriteContext,
pats: &[&ast::Pat],
guard: &Option<ptr::P<ast::Expr>>,
has_beginning_vert: bool,
shape: Shape,
) -> Option<String> {
// Patterns
// 5 = ` => {`
// 2 = `| `
let pat_shape = shape
.sub_width(5)?
.offset_left(if has_beginning_vert { 2 } else { 0 })?;
let pat_shape = shape.sub_width(5)?;
let pats_str = rewrite_multiple_patterns(context, pats, pat_shape)?;
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!("{}{}{}", beginning_vert, pats_str, guard_str))
Some(format!("{}{}", pats_str, guard_str))
}

// (extend, body)
Expand Down
12 changes: 12 additions & 0 deletions tests/source/match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,3 +485,15 @@ fn issue_2376() {
}
}
}

// #2621
// Strip leading `|` in match arm patterns
fn issue_2621() {
let x = Foo::A;
match x {
| Foo::A
| Foo::B => println!("AB"),
| Foo::C => println!("C"),
Foo::D => println!("D"),
}
}
15 changes: 13 additions & 2 deletions tests/target/match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,8 @@ fn issue_2152() {
fn match_with_beginning_vert() {
let x = Foo::A;
match x {
| Foo::A | Foo::B => println!("AB"),
| Foo::C => println!("C"),
Foo::A | Foo::B => println!("AB"),
Foo::C => println!("C"),
}
}

Expand All @@ -513,3 +513,14 @@ fn issue_2376() {
}
}
}

// #2621
// Strip leading `|` in match arm patterns
fn issue_2621() {
let x = Foo::A;
match x {
Foo::A | Foo::B => println!("AB"),
Foo::C => println!("C"),
Foo::D => println!("D"),
}
}

0 comments on commit b6fb2fd

Please sign in to comment.