Fix broken tuple pattern (#3729)

This commit is contained in:
Seiichi Uchida 2019-08-06 11:09:45 +09:00 committed by GitHub
parent 127de25041
commit c0cb5eb535
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 2 deletions

View File

@ -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<'_>,
@ -321,7 +333,10 @@ 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,
@ -329,7 +344,11 @@ fn rewrite_tuple_pat(
shape,
span,
context.config.max_width(),
None,
if add_comma {
Some(SeparatorTactic::Always)
} else {
None
},
)
}

View File

@ -81,3 +81,10 @@ fn slice_patterns() {
_ => {}
}
}
fn issue3728() {
let foo = |
(c,)
| c;
foo((1,));
}

View File

@ -15,7 +15,7 @@ fn main() {
(true, ..) => (),
(.., true) => (),
(..) => (),
(_) => (),
(_,) => (),
(/* .. */ ..) => (),
(/* .. */ .., true) => (),
}

View File

@ -91,3 +91,8 @@ fn slice_patterns() {
_ => {}
}
}
fn issue3728() {
let foo = |(c,)| c;
foo((1,));
}