Skip to content

Commit

Permalink
Fix broken tuple pattern (#3729)
Browse files Browse the repository at this point in the history
  • Loading branch information
topecongiro authored Aug 6, 2019
1 parent 127de25 commit c0cb5eb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,18 @@ impl<'a> Spanned for TuplePatField<'a> {
}
}

impl<'a> TuplePatField<'a> {
fn is_dotdot(&self) -> bool {
match self {
TuplePatField::Pat(pat) => match pat.node {
ast::PatKind::Rest => true,
_ => false,
},
TuplePatField::Dotdot(_) => true,
}
}
}

pub(crate) fn can_be_overflowed_pat(
context: &RewriteContext<'_>,
pat: &TuplePatField<'_>,
Expand Down Expand Up @@ -321,15 +333,22 @@ fn rewrite_tuple_pat(
(&pat_vec[..], span)
};

let is_last_pat_dotdot = pat_vec.last().map_or(false, |p| p.is_dotdot());
let add_comma = path_str.is_none() && pat_vec.len() == 1 && !is_last_pat_dotdot;
let path_str = path_str.unwrap_or_default();

overflow::rewrite_with_parens(
&context,
&path_str,
pat_vec.iter(),
shape,
span,
context.config.max_width(),
None,
if add_comma {
Some(SeparatorTactic::Always)
} else {
None
},
)
}

Expand Down
7 changes: 7 additions & 0 deletions tests/source/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,10 @@ fn slice_patterns() {
_ => {}
}
}

fn issue3728() {
let foo = |
(c,)
| c;
foo((1,));
}
2 changes: 1 addition & 1 deletion tests/target/issue-1021.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
(true, ..) => (),
(.., true) => (),
(..) => (),
(_) => (),
(_,) => (),
(/* .. */ ..) => (),
(/* .. */ .., true) => (),
}
Expand Down
5 changes: 5 additions & 0 deletions tests/target/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,8 @@ fn slice_patterns() {
_ => {}
}
}

fn issue3728() {
let foo = |(c,)| c;
foo((1,));
}

0 comments on commit c0cb5eb

Please sign in to comment.