Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit c4d845f

Browse files
authored
Merge pull request rust-lang#2972 from crw5996/fix-optional-arg-condensing
Fix optional arg condensing
2 parents 4484609 + 8c7afaf commit c4d845f

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

src/patterns.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -336,29 +336,28 @@ fn rewrite_tuple_pat(
336336
));
337337
pat_vec.insert(pos, dotdot);
338338
}
339-
340339
if pat_vec.is_empty() {
341340
return Some(format!("{}()", path_str.unwrap_or_default()));
342341
}
343-
344342
let wildcard_suffix_len = count_wildcard_suffix_len(context, &pat_vec, span, shape);
345-
let (pat_vec, span) = if context.config.condense_wildcard_suffixes() && wildcard_suffix_len >= 2
346-
{
347-
let new_item_count = 1 + pat_vec.len() - wildcard_suffix_len;
348-
let sp = pat_vec[new_item_count - 1].span();
349-
let snippet = context.snippet(sp);
350-
let lo = sp.lo() + BytePos(snippet.find_uncommented("_").unwrap() as u32);
351-
pat_vec[new_item_count - 1] = TuplePatField::Dotdot(mk_sp(lo, lo + BytePos(1)));
352-
(
353-
&pat_vec[..new_item_count],
354-
mk_sp(span.lo(), lo + BytePos(1)),
355-
)
356-
} else {
357-
(&pat_vec[..], span)
358-
};
343+
let (pat_vec, span, condensed) =
344+
if context.config.condense_wildcard_suffixes() && wildcard_suffix_len >= 2 {
345+
let new_item_count = 1 + pat_vec.len() - wildcard_suffix_len;
346+
let sp = pat_vec[new_item_count - 1].span();
347+
let snippet = context.snippet(sp);
348+
let lo = sp.lo() + BytePos(snippet.find_uncommented("_").unwrap() as u32);
349+
pat_vec[new_item_count - 1] = TuplePatField::Dotdot(mk_sp(lo, lo + BytePos(1)));
350+
(
351+
&pat_vec[..new_item_count],
352+
mk_sp(span.lo(), lo + BytePos(1)),
353+
true,
354+
)
355+
} else {
356+
(&pat_vec[..], span, false)
357+
};
359358

360359
// add comma if `(x,)`
361-
let add_comma = path_str.is_none() && pat_vec.len() == 1 && dotdot_pos.is_none();
360+
let add_comma = path_str.is_none() && pat_vec.len() == 1 && dotdot_pos.is_none() && !condensed;
362361
let path_str = path_str.unwrap_or_default();
363362
let pat_ref_vec = pat_vec.iter().collect::<Vec<_>>();
364363

tests/source/issue-2955.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-condense_wildcard_suffixes: true
2+
fn main() {
3+
match (1, 2, 3) {
4+
(_, _, _) => (),
5+
}
6+
}

tests/target/issue-2955.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// rustfmt-condense_wildcard_suffixes: true
2+
fn main() {
3+
match (1, 2, 3) {
4+
(..) => (),
5+
}
6+
}

0 commit comments

Comments
 (0)