Skip to content

Commit 94113b0

Browse files
committed
fix: fill_match_arms doesn't add wildcard pat for local enums
1 parent 754d868 commit 94113b0

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

crates/ide_assists/src/handlers/add_missing_match_arms.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) ->
7878
Peekable<Box<dyn Iterator<Item = (ast::Pat, bool)>>>,
7979
bool,
8080
) = if let Some(enum_def) = resolve_enum_def(&ctx.sema, &expr) {
81-
let is_non_exhaustive = enum_def.is_non_exhaustive(ctx.db());
81+
let is_non_exhaustive = enum_def.is_non_exhaustive(ctx.db(), module.krate());
8282

8383
let variants = enum_def.variants(ctx.db());
8484

@@ -104,7 +104,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) ->
104104
(missing_pats.peekable(), is_non_exhaustive)
105105
} else if let Some(enum_defs) = resolve_tuple_of_enum_def(&ctx.sema, &expr) {
106106
let is_non_exhaustive =
107-
enum_defs.iter().any(|enum_def| enum_def.is_non_exhaustive(ctx.db()));
107+
enum_defs.iter().any(|enum_def| enum_def.is_non_exhaustive(ctx.db(), module.krate()));
108108

109109
let mut n_arms = 1;
110110
let variants_of_enums: Vec<Vec<ExtendedVariant>> = enum_defs
@@ -301,9 +301,9 @@ fn lift_enum(e: hir::Enum) -> ExtendedEnum {
301301
}
302302

303303
impl ExtendedEnum {
304-
fn is_non_exhaustive(self, db: &RootDatabase) -> bool {
304+
fn is_non_exhaustive(self, db: &RootDatabase, krate: Crate) -> bool {
305305
match self {
306-
ExtendedEnum::Enum(e) => e.attrs(db).by_key("non_exhaustive").exists(),
306+
ExtendedEnum::Enum(e) => e.attrs(db).by_key("non_exhaustive").exists() && e.module(db).krate() != krate,
307307
_ => false,
308308
}
309309
}
@@ -1657,8 +1657,32 @@ fn foo(t: E) {
16571657
}
16581658

16591659
#[test]
1660-
fn ignores_doc_hidden_for_crate_local_enums_but_not_non_exhaustive() {
1661-
cov_mark::check!(added_wildcard_pattern);
1660+
fn ignores_non_exhaustive_for_crate_local_enums() {
1661+
check_assist(
1662+
add_missing_match_arms,
1663+
r#"
1664+
#[non_exhaustive]
1665+
enum E { A, B, }
1666+
1667+
fn foo(t: E) {
1668+
match $0t {
1669+
}
1670+
}"#,
1671+
r#"
1672+
#[non_exhaustive]
1673+
enum E { A, B, }
1674+
1675+
fn foo(t: E) {
1676+
match t {
1677+
$0E::A => todo!(),
1678+
E::B => todo!(),
1679+
}
1680+
}"#,
1681+
);
1682+
}
1683+
1684+
#[test]
1685+
fn ignores_doc_hidden_and_non_exhaustive_for_crate_local_enums() {
16621686
check_assist(
16631687
add_missing_match_arms,
16641688
r#"
@@ -1677,7 +1701,6 @@ fn foo(t: E) {
16771701
match t {
16781702
$0E::A => todo!(),
16791703
E::B => todo!(),
1680-
_ => todo!(),
16811704
}
16821705
}"#,
16831706
);

0 commit comments

Comments
 (0)