Skip to content

Commit e53919a

Browse files
bors[bot]iDawer
andauthored
Merge #8543
8543: Assist fix: Fill match arms for a tuple of a single enum. r=Veykril a=iDawer This is rather a small fix addressing an issue mentioned in #8493 (comment) Co-authored-by: Dawer <[email protected]>
2 parents 75371eb + 8965be3 commit e53919a

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

crates/ide_assists/src/handlers/fill_match_arms.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,6 @@ pub(crate) fn fill_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option<
7171
return None;
7272
}
7373

74-
// We do not currently support filling match arms for a tuple
75-
// containing a single enum.
76-
if enum_defs.len() < 2 {
77-
return None;
78-
}
79-
8074
// When calculating the match arms for a tuple of enums, we want
8175
// to create a match arm for each possible combination of enum
8276
// values. The `multi_cartesian_product` method transforms
@@ -514,10 +508,7 @@ fn main() {
514508

515509
#[test]
516510
fn fill_match_arms_single_element_tuple_of_enum() {
517-
// For now we don't hande the case of a single element tuple, but
518-
// we could handle this in the future if `make::tuple_pat` allowed
519-
// creating a tuple with a single pattern.
520-
check_assist_not_applicable(
511+
check_assist(
521512
fill_match_arms,
522513
r#"
523514
enum A { One, Two }
@@ -528,6 +519,17 @@ fn main() {
528519
}
529520
}
530521
"#,
522+
r#"
523+
enum A { One, Two }
524+
525+
fn main() {
526+
let a = A::One;
527+
match (a, ) {
528+
$0(A::One,) => {}
529+
(A::Two,) => {}
530+
}
531+
}
532+
"#,
531533
);
532534
}
533535

crates/syntax/src/ast/make.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ pub fn ty(text: &str) -> ast::Type {
2929
pub fn ty_unit() -> ast::Type {
3030
ty("()")
3131
}
32-
// FIXME: handle types of length == 1
3332
pub fn ty_tuple(types: impl IntoIterator<Item = ast::Type>) -> ast::Type {
34-
let contents = types.into_iter().join(", ");
33+
let mut count: usize = 0;
34+
let mut contents = types.into_iter().inspect(|_| count += 1).join(", ");
35+
if count == 1 {
36+
contents.push(',');
37+
}
38+
3539
ty(&format!("({})", contents))
3640
}
3741
// FIXME: handle path to type
@@ -292,11 +296,13 @@ pub fn wildcard_pat() -> ast::WildcardPat {
292296

293297
/// Creates a tuple of patterns from an iterator of patterns.
294298
///
295-
/// Invariant: `pats` must be length > 1
296-
///
297-
/// FIXME handle `pats` length == 1
299+
/// Invariant: `pats` must be length > 0
298300
pub fn tuple_pat(pats: impl IntoIterator<Item = ast::Pat>) -> ast::TuplePat {
299-
let pats_str = pats.into_iter().map(|p| p.to_string()).join(", ");
301+
let mut count: usize = 0;
302+
let mut pats_str = pats.into_iter().inspect(|_| count += 1).join(", ");
303+
if count == 1 {
304+
pats_str.push(',');
305+
}
300306
return from_text(&format!("({})", pats_str));
301307

302308
fn from_text(text: &str) -> ast::TuplePat {

0 commit comments

Comments
 (0)