Skip to content

Commit 018975b

Browse files
committed
fix: Fix incorrect expected type in completions for match arms
1 parent b6b17c7 commit 018975b

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

crates/ide-completion/src/context.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,9 +802,19 @@ impl<'a> CompletionContext<'a> {
802802
)
803803
}
804804
},
805+
// match foo { $0 }
806+
// match foo { ..., pat => $0 }
805807
ast::MatchExpr(it) => {
806-
cov_mark::hit!(expected_type_match_arm_without_leading_char);
807-
let ty = it.expr().and_then(|e| self.sema.type_of_expr(&e)).map(TypeInfo::original);
808+
let ty = if self.previous_token_is(T![=>]) {
809+
// match foo { ..., pat => $0 }
810+
cov_mark::hit!(expected_type_match_arm_body_without_leading_char);
811+
cov_mark::hit!(expected_type_match_arm_body_with_leading_char);
812+
self.sema.type_of_expr(&it.into())
813+
} else {
814+
// match foo { $0 }
815+
cov_mark::hit!(expected_type_match_arm_without_leading_char);
816+
it.expr().and_then(|e| self.sema.type_of_expr(&e))
817+
}.map(TypeInfo::original);
808818
(ty, None)
809819
},
810820
ast::IfExpr(it) => {
@@ -1589,6 +1599,36 @@ fn foo() {
15891599
);
15901600
}
15911601

1602+
#[test]
1603+
fn expected_type_match_arm_body_without_leading_char() {
1604+
cov_mark::check!(expected_type_match_arm_body_without_leading_char);
1605+
check_expected_type_and_name(
1606+
r#"
1607+
struct Foo;
1608+
enum E { X }
1609+
fn foo() -> Foo {
1610+
match E::X { E::X => $0 }
1611+
}
1612+
"#,
1613+
expect![[r#"ty: Foo, name: ?"#]],
1614+
);
1615+
}
1616+
1617+
#[test]
1618+
fn expected_type_match_body_arm_with_leading_char() {
1619+
cov_mark::check!(expected_type_match_arm_body_with_leading_char);
1620+
check_expected_type_and_name(
1621+
r#"
1622+
struct Foo;
1623+
enum E { X }
1624+
fn foo() -> Foo {
1625+
match E::X { E::X => c$0 }
1626+
}
1627+
"#,
1628+
expect![[r#"ty: Foo, name: ?"#]],
1629+
);
1630+
}
1631+
15921632
#[test]
15931633
fn expected_type_if_let_without_leading_char() {
15941634
cov_mark::check!(expected_type_if_let_without_leading_char);

0 commit comments

Comments
 (0)