From c0cb5eb5358c38c07c0b57217e691bc3ebbb5e85 Mon Sep 17 00:00:00 2001 From: Seiichi Uchida Date: Tue, 6 Aug 2019 11:09:45 +0900 Subject: [PATCH] Fix broken tuple pattern (#3729) --- src/patterns.rs | 21 ++++++++++++++++++++- tests/source/pattern.rs | 7 +++++++ tests/target/issue-1021.rs | 2 +- tests/target/pattern.rs | 5 +++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/patterns.rs b/src/patterns.rs index 57051a286a7..b5032eca6d5 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -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 + }, ) } diff --git a/tests/source/pattern.rs b/tests/source/pattern.rs index fc3b1455e0d..f06d03cadf2 100644 --- a/tests/source/pattern.rs +++ b/tests/source/pattern.rs @@ -81,3 +81,10 @@ fn slice_patterns() { _ => {} } } + +fn issue3728() { + let foo = | + (c,) + | c; + foo((1,)); +} diff --git a/tests/target/issue-1021.rs b/tests/target/issue-1021.rs index 94911d1f8ee..ba1029d4e61 100644 --- a/tests/target/issue-1021.rs +++ b/tests/target/issue-1021.rs @@ -15,7 +15,7 @@ fn main() { (true, ..) => (), (.., true) => (), (..) => (), - (_) => (), + (_,) => (), (/* .. */ ..) => (), (/* .. */ .., true) => (), } diff --git a/tests/target/pattern.rs b/tests/target/pattern.rs index d8a96d64920..576018ac623 100644 --- a/tests/target/pattern.rs +++ b/tests/target/pattern.rs @@ -91,3 +91,8 @@ fn slice_patterns() { _ => {} } } + +fn issue3728() { + let foo = |(c,)| c; + foo((1,)); +}