Skip to content

Commit c0cb5eb

Browse files
authored
Fix broken tuple pattern (#3729)
1 parent 127de25 commit c0cb5eb

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

src/patterns.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,18 @@ impl<'a> Spanned for TuplePatField<'a> {
272272
}
273273
}
274274

275+
impl<'a> TuplePatField<'a> {
276+
fn is_dotdot(&self) -> bool {
277+
match self {
278+
TuplePatField::Pat(pat) => match pat.node {
279+
ast::PatKind::Rest => true,
280+
_ => false,
281+
},
282+
TuplePatField::Dotdot(_) => true,
283+
}
284+
}
285+
}
286+
275287
pub(crate) fn can_be_overflowed_pat(
276288
context: &RewriteContext<'_>,
277289
pat: &TuplePatField<'_>,
@@ -321,15 +333,22 @@ fn rewrite_tuple_pat(
321333
(&pat_vec[..], span)
322334
};
323335

336+
let is_last_pat_dotdot = pat_vec.last().map_or(false, |p| p.is_dotdot());
337+
let add_comma = path_str.is_none() && pat_vec.len() == 1 && !is_last_pat_dotdot;
324338
let path_str = path_str.unwrap_or_default();
339+
325340
overflow::rewrite_with_parens(
326341
&context,
327342
&path_str,
328343
pat_vec.iter(),
329344
shape,
330345
span,
331346
context.config.max_width(),
332-
None,
347+
if add_comma {
348+
Some(SeparatorTactic::Always)
349+
} else {
350+
None
351+
},
333352
)
334353
}
335354

tests/source/pattern.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,10 @@ fn slice_patterns() {
8181
_ => {}
8282
}
8383
}
84+
85+
fn issue3728() {
86+
let foo = |
87+
(c,)
88+
| c;
89+
foo((1,));
90+
}

tests/target/issue-1021.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn main() {
1515
(true, ..) => (),
1616
(.., true) => (),
1717
(..) => (),
18-
(_) => (),
18+
(_,) => (),
1919
(/* .. */ ..) => (),
2020
(/* .. */ .., true) => (),
2121
}

tests/target/pattern.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,8 @@ fn slice_patterns() {
9191
_ => {}
9292
}
9393
}
94+
95+
fn issue3728() {
96+
let foo = |(c,)| c;
97+
foo((1,));
98+
}

0 commit comments

Comments
 (0)