From 43a4c45ede74f04ec4c86363f9158e6b6f1efc00 Mon Sep 17 00:00:00 2001 From: Ole Strohm Date: Tue, 22 Feb 2022 13:59:30 +0000 Subject: [PATCH 1/4] fix: Make match_arms assist handle doc(hidden) and non_exhaustive --- .../src/handlers/add_missing_match_arms.rs | 390 ++++++++++++++++-- 1 file changed, 362 insertions(+), 28 deletions(-) diff --git a/crates/ide_assists/src/handlers/add_missing_match_arms.rs b/crates/ide_assists/src/handlers/add_missing_match_arms.rs index eeed0386ad6a..7d151f9efecc 100644 --- a/crates/ide_assists/src/handlers/add_missing_match_arms.rs +++ b/crates/ide_assists/src/handlers/add_missing_match_arms.rs @@ -1,11 +1,11 @@ use std::iter::{self, Peekable}; use either::Either; -use hir::{Adt, HasSource, ModuleDef, Semantics}; +use hir::{Adt, HasAttrs, HasSource, ModuleDef, Semantics}; use ide_db::helpers::{mod_path_to_ast, FamousDefs}; use ide_db::RootDatabase; use itertools::Itertools; -use syntax::ast::{self, make, AstNode, HasName, MatchArm, MatchArmList, MatchExpr, Pat}; +use syntax::ast::{self, make, AstNode, HasName, MatchArmList, MatchExpr, Pat}; use crate::{ utils::{self, render_snippet, Cursor}, @@ -52,20 +52,22 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) -> let expr = match_expr.expr()?; - let mut arms: Vec = match_arm_list.arms().collect(); - if let [arm] = arms.as_slice() { - if let Some(Pat::WildcardPat(..)) = arm.pat() { - arms.clear(); - } - } + let mut has_catch_all_arm = false; - let top_lvl_pats: Vec<_> = arms - .iter() - .filter_map(ast::MatchArm::pat) - .flat_map(|pat| match pat { - // Special case OrPat as separate top-level pats - Pat::OrPat(or_pat) => Either::Left(or_pat.pats()), - _ => Either::Right(iter::once(pat)), + let top_lvl_pats: Vec<_> = match_arm_list + .arms() + .filter_map(|arm| Some((arm.pat()?, arm.guard().is_some()))) + .flat_map(|(pat, has_guard)| { + match pat { + // Special case OrPat as separate top-level pats + Pat::OrPat(or_pat) => Either::Left(or_pat.pats()), + _ => Either::Right(iter::once(pat)), + } + .map(move |pat| (pat, has_guard)) + }) + .map(|(pat, has_guard)| { + has_catch_all_arm |= !has_guard && matches!(pat, Pat::WildcardPat(_)); + pat }) // Exclude top level wildcards so that they are expanded by this assist, retains status quo in #8129. .filter(|pat| !matches!(pat, Pat::WildcardPat(_))) @@ -73,15 +75,27 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) -> let module = ctx.sema.scope(expr.syntax()).module()?; - let mut missing_pats: Peekable>> = if let Some(enum_def) = - resolve_enum_def(&ctx.sema, &expr) - { + let (mut missing_pats, is_non_exhaustive): ( + Peekable>>, + bool, + ) = if let Some(enum_def) = resolve_enum_def(&ctx.sema, &expr) { let variants = enum_def.variants(ctx.db()); + let is_non_exhaustive = match enum_def { + ExtendedEnum::Enum(e) => e.attrs(ctx.db()).by_key("non_exhaustive").exists(), + _ => false, + }; + let missing_pats = variants .into_iter() - .filter_map(|variant| build_pat(ctx.db(), module, variant)) - .filter(|variant_pat| is_variant_missing(&top_lvl_pats, variant_pat)); + .filter_map(|variant| { + let is_hidden = match variant { + ExtendedVariant::Variant(var) => var.attrs(ctx.db()).has_doc_hidden(), + _ => false, + }; + Some((build_pat(ctx.db(), module, variant)?, is_hidden)) + }) + .filter(|(variant_pat, _)| is_variant_missing(&top_lvl_pats, variant_pat)); let option_enum = FamousDefs(&ctx.sema, Some(module.krate())).core_option_Option().map(lift_enum); @@ -92,8 +106,13 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) -> } else { Box::new(missing_pats) }; - missing_pats.peekable() + (missing_pats.peekable(), is_non_exhaustive) } else if let Some(enum_defs) = resolve_tuple_of_enum_def(&ctx.sema, &expr) { + let is_non_exhaustive = enum_defs.iter().any(|enum_def| match enum_def { + ExtendedEnum::Enum(e) => e.attrs(ctx.db()).by_key("non_exhaustive").exists(), + _ => false, + }); + let mut n_arms = 1; let variants_of_enums: Vec> = enum_defs .into_iter() @@ -117,17 +136,24 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) -> .multi_cartesian_product() .inspect(|_| cov_mark::hit!(add_missing_match_arms_lazy_computation)) .map(|variants| { + let is_hidden = variants.iter().any(|variant| match variant { + ExtendedVariant::Variant(var) => var.attrs(ctx.db()).has_doc_hidden(), + _ => false, + }); let patterns = variants.into_iter().filter_map(|variant| build_pat(ctx.db(), module, variant)); - ast::Pat::from(make::tuple_pat(patterns)) + + (ast::Pat::from(make::tuple_pat(patterns)), is_hidden) }) - .filter(|variant_pat| is_variant_missing(&top_lvl_pats, variant_pat)); - (Box::new(missing_pats) as Box>).peekable() + .filter(|(variant_pat, _)| is_variant_missing(&top_lvl_pats, variant_pat)); + ((Box::new(missing_pats) as Box>).peekable(), is_non_exhaustive) } else { return None; }; - if missing_pats.peek().is_none() { + let mut needs_catch_all_arm = is_non_exhaustive && !has_catch_all_arm; + + if !needs_catch_all_arm && missing_pats.peek().is_none() { return None; } @@ -138,8 +164,10 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) -> |builder| { let new_match_arm_list = match_arm_list.clone_for_update(); let missing_arms = missing_pats - .map(|pat| make::match_arm(iter::once(pat), None, make::ext::expr_todo())) - .map(|it| it.clone_for_update()); + .map(|(pat, hidden)| { + (make::match_arm(iter::once(pat), None, make::ext::expr_todo()), hidden) + }) + .map(|(it, hidden)| (it.clone_for_update(), hidden)); let catch_all_arm = new_match_arm_list .arms() @@ -159,7 +187,22 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) -> } } let mut first_new_arm = None; - for arm in missing_arms { + for (arm, hidden) in missing_arms { + if hidden { + needs_catch_all_arm = !has_catch_all_arm; + } else { + first_new_arm.get_or_insert_with(|| arm.clone()); + new_match_arm_list.add_arm(arm); + } + } + if needs_catch_all_arm && !has_catch_all_arm { + cov_mark::hit!(added_wildcard_pattern); + let arm = make::match_arm( + iter::once(make::wildcard_pat().into()), + None, + make::ext::expr_todo(), + ) + .clone_for_update(); first_new_arm.get_or_insert_with(|| arm.clone()); new_match_arm_list.add_arm(arm); } @@ -1280,6 +1323,297 @@ fn foo(t: bool) { $0true => todo!(), false => todo!(), } +}"#, + ); + } + + #[test] + fn does_not_fill_hidden_variants() { + cov_mark::check!(added_wildcard_pattern); + check_assist( + add_missing_match_arms, + r#" +enum E { + A, + #[doc(hidden)] + C, +} + +fn foo(t: E) { + match $0t { + } +}"#, + r#" +enum E { + A, + #[doc(hidden)] + C, +} + +fn foo(t: E) { + match t { + $0E::A => todo!(), + _ => todo!(), + } +}"#, + ); + } + + #[test] + fn does_not_fill_hidden_variants_tuple() { + cov_mark::check!(added_wildcard_pattern); + check_assist( + add_missing_match_arms, + r#" +enum E { + A, + #[doc(hidden)] + C, +} + +fn foo(t: (bool, E)) { + match $0t { + } +}"#, + r#" +enum E { + A, + #[doc(hidden)] + C, +} + +fn foo(t: (bool, E)) { + match t { + $0(true, E::A) => todo!(), + (false, E::A) => todo!(), + _ => todo!(), + } +}"#, + ); + } + + #[test] + fn fills_wildcard_with_only_hidden_variants() { + cov_mark::check!(added_wildcard_pattern); + check_assist( + add_missing_match_arms, + r#" +enum E { + #[doc(hidden)] + A, +} + +fn foo(t: E) { + match $0t { + } +}"#, + r#" +enum E { + #[doc(hidden)] + A, +} + +fn foo(t: E) { + match t { + ${0:_} => todo!(), + } +}"#, + ); + } + + #[test] + fn does_not_fill_wildcard_when_hidden_variants_are_explicit() { + check_assist_not_applicable( + add_missing_match_arms, + r#" +enum E { + #[doc(hidden)] + A, +} + +fn foo(t: E) { + match $0t { + E::A => todo!(), + } +}"#, + ); + } + + // FIXME: I don't think the assist should be applicable in this case + #[test] + fn does_not_fill_wildcard_with_wildcard() { + check_assist( + add_missing_match_arms, + r#" +enum E { #[doc(hidden)] A, } + +fn foo(t: E) { + match $0t { + _ => todo!(), + } +}"#, + r#" +enum E { #[doc(hidden)] A, } + +fn foo(t: E) { + match t { + _ => todo!(), + } +}"#, + ); + } + + #[test] + fn fills_wildcard_on_non_exhaustive_with_explicit_matches() { + cov_mark::check!(added_wildcard_pattern); + check_assist( + add_missing_match_arms, + r#" +#[non_exhaustive] +enum E { A, } + +fn foo(t: E) { + match $0t { + E::A => todo!(), + } +}"#, + r#" +#[non_exhaustive] +enum E { A, } + +fn foo(t: E) { + match t { + E::A => todo!(), + ${0:_} => todo!(), + } +}"#, + ); + } + + #[test] + fn fills_wildcard_on_non_exhaustive_without_matches() { + cov_mark::check!(added_wildcard_pattern); + check_assist( + add_missing_match_arms, + r#" +#[non_exhaustive] +enum E { A, } + +fn foo(t: E) { + match $0t { + } +}"#, + r#" +#[non_exhaustive] +enum E { A, } + +fn foo(t: E) { + match t { + $0E::A => todo!(), + _ => todo!(), + } +}"#, + ); + } + + #[test] + fn fills_wildcard_on_non_exhaustive_with_doc_hidden() { + cov_mark::check!(added_wildcard_pattern); + check_assist( + add_missing_match_arms, + r#" +#[non_exhaustive] +enum E { A, #[doc(hidden)] B } + +fn foo(t: E) { + match $0t { + } +}"#, + r#" +#[non_exhaustive] +enum E { A, #[doc(hidden)] B } + +fn foo(t: E) { + match t { + $0E::A => todo!(), + _ => todo!(), + } +}"#, + ); + } + + #[test] + fn fills_wildcard_on_non_exhaustive_with_doc_hidden_with_explicit_arms() { + cov_mark::check!(added_wildcard_pattern); + check_assist( + add_missing_match_arms, + r#" +#[non_exhaustive] +enum E { #[doc(hidden)] A } + +fn foo(t: E) { + match $0t { + E::A => todo!(), + } +}"#, + r#" +#[non_exhaustive] +enum E { #[doc(hidden)] A } + +fn foo(t: E) { + match t { + E::A => todo!(), + ${0:_} => todo!(), + } +}"#, + ); + } + + #[test] + fn fill_wildcard_with_partial_wildcard() { + check_assist( + add_missing_match_arms, + r#" +enum E { #[doc(hidden)] A, } + +fn foo(t: E, b: bool) { + match $0t { + _ if b => todo!(), + } +}"#, + r#" +enum E { #[doc(hidden)] A, } + +fn foo(t: E, b: bool) { + match t { + _ if b => todo!(), + ${0:_} => todo!(), + } +}"#, + ); + } + + #[test] + fn does_notfill_wildcard_with_partial_wildcard_and_wildcard() { + check_assist( + add_missing_match_arms, + r#" +enum E { #[doc(hidden)] A, } + +fn foo(t: E, b: bool) { + match $0t { + _ if b => todo!(), + _ => todo!(), + } +}"#, + r#" +enum E { #[doc(hidden)] A, } + +fn foo(t: E, b: bool) { + match t { + _ if b => todo!(), + _ => todo!(), + } }"#, ); } From 94a221ae8df006d48909e26b0583d212d6892aef Mon Sep 17 00:00:00 2001 From: Ole Strohm Date: Tue, 22 Feb 2022 22:41:03 +0000 Subject: [PATCH 2/4] Dedup code --- .../src/handlers/add_missing_match_arms.rs | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/crates/ide_assists/src/handlers/add_missing_match_arms.rs b/crates/ide_assists/src/handlers/add_missing_match_arms.rs index 7d151f9efecc..202e79ec60a5 100644 --- a/crates/ide_assists/src/handlers/add_missing_match_arms.rs +++ b/crates/ide_assists/src/handlers/add_missing_match_arms.rs @@ -81,19 +81,12 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) -> ) = if let Some(enum_def) = resolve_enum_def(&ctx.sema, &expr) { let variants = enum_def.variants(ctx.db()); - let is_non_exhaustive = match enum_def { - ExtendedEnum::Enum(e) => e.attrs(ctx.db()).by_key("non_exhaustive").exists(), - _ => false, - }; + let is_non_exhaustive = enum_def.is_non_exhaustive(ctx.db()); let missing_pats = variants .into_iter() .filter_map(|variant| { - let is_hidden = match variant { - ExtendedVariant::Variant(var) => var.attrs(ctx.db()).has_doc_hidden(), - _ => false, - }; - Some((build_pat(ctx.db(), module, variant)?, is_hidden)) + Some((build_pat(ctx.db(), module, variant)?, variant.has_doc_hidden(ctx.db()))) }) .filter(|(variant_pat, _)| is_variant_missing(&top_lvl_pats, variant_pat)); @@ -108,10 +101,8 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) -> }; (missing_pats.peekable(), is_non_exhaustive) } else if let Some(enum_defs) = resolve_tuple_of_enum_def(&ctx.sema, &expr) { - let is_non_exhaustive = enum_defs.iter().any(|enum_def| match enum_def { - ExtendedEnum::Enum(e) => e.attrs(ctx.db()).by_key("non_exhaustive").exists(), - _ => false, - }); + let is_non_exhaustive = + enum_defs.iter().any(|enum_def| enum_def.is_non_exhaustive(ctx.db())); let mut n_arms = 1; let variants_of_enums: Vec> = enum_defs @@ -136,10 +127,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) -> .multi_cartesian_product() .inspect(|_| cov_mark::hit!(add_missing_match_arms_lazy_computation)) .map(|variants| { - let is_hidden = variants.iter().any(|variant| match variant { - ExtendedVariant::Variant(var) => var.attrs(ctx.db()).has_doc_hidden(), - _ => false, - }); + let is_hidden = variants.iter().any(|variant| variant.has_doc_hidden(ctx.db())); let patterns = variants.into_iter().filter_map(|variant| build_pat(ctx.db(), module, variant)); @@ -293,11 +281,27 @@ enum ExtendedVariant { Variant(hir::Variant), } +impl ExtendedVariant { + fn has_doc_hidden(self, db: &RootDatabase) -> bool { + match self { + ExtendedVariant::Variant(var) => var.attrs(db).has_doc_hidden(), + _ => false, + } + } +} + fn lift_enum(e: hir::Enum) -> ExtendedEnum { ExtendedEnum::Enum(e) } impl ExtendedEnum { + fn is_non_exhaustive(self, db: &RootDatabase) -> bool { + match self { + ExtendedEnum::Enum(e) => e.attrs(db).by_key("non_exhaustive").exists(), + _ => false, + } + } + fn variants(self, db: &RootDatabase) -> Vec { match self { ExtendedEnum::Enum(e) => { From 5cdbfa5b7021a3b06bf7b1a887221ae3645aea57 Mon Sep 17 00:00:00 2001 From: Ole Strohm Date: Tue, 22 Feb 2022 22:48:44 +0000 Subject: [PATCH 3/4] Added test --- .../src/handlers/add_missing_match_arms.rs | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/crates/ide_assists/src/handlers/add_missing_match_arms.rs b/crates/ide_assists/src/handlers/add_missing_match_arms.rs index 202e79ec60a5..e8309364ceaa 100644 --- a/crates/ide_assists/src/handlers/add_missing_match_arms.rs +++ b/crates/ide_assists/src/handlers/add_missing_match_arms.rs @@ -79,10 +79,10 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Peekable>>, bool, ) = if let Some(enum_def) = resolve_enum_def(&ctx.sema, &expr) { - let variants = enum_def.variants(ctx.db()); - let is_non_exhaustive = enum_def.is_non_exhaustive(ctx.db()); + let variants = enum_def.variants(ctx.db()); + let missing_pats = variants .into_iter() .filter_map(|variant| { @@ -1598,7 +1598,7 @@ fn foo(t: E, b: bool) { } #[test] - fn does_notfill_wildcard_with_partial_wildcard_and_wildcard() { + fn does_not_fill_wildcard_with_partial_wildcard_and_wildcard() { check_assist( add_missing_match_arms, r#" @@ -1618,6 +1618,30 @@ fn foo(t: E, b: bool) { _ if b => todo!(), _ => todo!(), } +}"#, + ); + } + + #[test] + fn non_exhaustive_doc_hidden_tuple_fills_wildcard() { + check_assist( + add_missing_match_arms, + r#" +enum E { A, #[doc(hidden)] B, } + +fn foo(t: E, b: bool) { + match $0(t, b) { + } +}"#, + r#" +enum E { A, #[doc(hidden)] B, } + +fn foo(t: E, b: bool) { + match (t, b) { + $0(E::A, true) => todo!(), + (E::A, false) => todo!(), + _ => todo!(), + } }"#, ); } From f1ba7465c6a24aa80dda6b6f55f847e4b56c3275 Mon Sep 17 00:00:00 2001 From: Ole Strohm Date: Wed, 23 Feb 2022 18:08:18 +0000 Subject: [PATCH 4/4] Ignore doc(hidden) for crate-local enums --- .../src/handlers/add_missing_match_arms.rs | 315 ++++++++++-------- 1 file changed, 176 insertions(+), 139 deletions(-) diff --git a/crates/ide_assists/src/handlers/add_missing_match_arms.rs b/crates/ide_assists/src/handlers/add_missing_match_arms.rs index e8309364ceaa..ec8fbb497451 100644 --- a/crates/ide_assists/src/handlers/add_missing_match_arms.rs +++ b/crates/ide_assists/src/handlers/add_missing_match_arms.rs @@ -1,7 +1,7 @@ use std::iter::{self, Peekable}; use either::Either; -use hir::{Adt, HasAttrs, HasSource, ModuleDef, Semantics}; +use hir::{Adt, Crate, HasAttrs, HasSource, ModuleDef, Semantics}; use ide_db::helpers::{mod_path_to_ast, FamousDefs}; use ide_db::RootDatabase; use itertools::Itertools; @@ -74,7 +74,6 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) -> .collect(); let module = ctx.sema.scope(expr.syntax()).module()?; - let (mut missing_pats, is_non_exhaustive): ( Peekable>>, bool, @@ -86,7 +85,10 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) -> let missing_pats = variants .into_iter() .filter_map(|variant| { - Some((build_pat(ctx.db(), module, variant)?, variant.has_doc_hidden(ctx.db()))) + Some(( + build_pat(ctx.db(), module, variant)?, + variant.should_be_hidden(ctx.db(), module.krate()), + )) }) .filter(|(variant_pat, _)| is_variant_missing(&top_lvl_pats, variant_pat)); @@ -127,7 +129,9 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) -> .multi_cartesian_product() .inspect(|_| cov_mark::hit!(add_missing_match_arms_lazy_computation)) .map(|variants| { - let is_hidden = variants.iter().any(|variant| variant.has_doc_hidden(ctx.db())); + let is_hidden = variants + .iter() + .any(|variant| variant.should_be_hidden(ctx.db(), module.krate())); let patterns = variants.into_iter().filter_map(|variant| build_pat(ctx.db(), module, variant)); @@ -282,9 +286,11 @@ enum ExtendedVariant { } impl ExtendedVariant { - fn has_doc_hidden(self, db: &RootDatabase) -> bool { + fn should_be_hidden(self, db: &RootDatabase, krate: Crate) -> bool { match self { - ExtendedVariant::Variant(var) => var.attrs(db).has_doc_hidden(), + ExtendedVariant::Variant(var) => { + var.attrs(db).has_doc_hidden() && var.module(db).krate() != krate + } _ => false, } } @@ -1337,29 +1343,22 @@ fn foo(t: bool) { check_assist( add_missing_match_arms, r#" -enum E { - A, - #[doc(hidden)] - C, -} - -fn foo(t: E) { +//- /main.rs crate:main deps:e +fn foo(t: ::e::E) { match $0t { } -}"#, - r#" -enum E { - A, - #[doc(hidden)] - C, } - -fn foo(t: E) { +//- /e.rs crate:e +pub enum E { A, #[doc(hidden)] B, } +"#, + r#" +fn foo(t: ::e::E) { match t { - $0E::A => todo!(), + $0e::E::A => todo!(), _ => todo!(), } -}"#, +} +"#, ); } @@ -1369,30 +1368,23 @@ fn foo(t: E) { check_assist( add_missing_match_arms, r#" -enum E { - A, - #[doc(hidden)] - C, -} - -fn foo(t: (bool, E)) { +//- /main.rs crate:main deps:e +fn foo(t: (bool, ::e::E)) { match $0t { } -}"#, - r#" -enum E { - A, - #[doc(hidden)] - C, } - -fn foo(t: (bool, E)) { +//- /e.rs crate:e +pub enum E { A, #[doc(hidden)] B, } +"#, + r#" +fn foo(t: (bool, ::e::E)) { match t { - $0(true, E::A) => todo!(), - (false, E::A) => todo!(), + $0(true, e::E::A) => todo!(), + (false, e::E::A) => todo!(), _ => todo!(), } -}"#, +} +"#, ); } @@ -1402,26 +1394,21 @@ fn foo(t: (bool, E)) { check_assist( add_missing_match_arms, r#" -enum E { - #[doc(hidden)] - A, -} - -fn foo(t: E) { +//- /main.rs crate:main deps:e +fn foo(t: ::e::E) { match $0t { } -}"#, - r#" -enum E { - #[doc(hidden)] - A, } - -fn foo(t: E) { +//- /e.rs crate:e +pub enum E { #[doc(hidden)] A, } +"#, + r#" +fn foo(t: ::e::E) { match t { ${0:_} => todo!(), } -}"#, +} +"#, ); } @@ -1430,16 +1417,15 @@ fn foo(t: E) { check_assist_not_applicable( add_missing_match_arms, r#" -enum E { - #[doc(hidden)] - A, -} - -fn foo(t: E) { +//- /main.rs crate:main deps:e +fn foo(t: ::e::E) { match $0t { - E::A => todo!(), + e::E::A => todo!(), } -}"#, +} +//- /e.rs crate:e +pub enum E { #[doc(hidden)] A, } +"#, ); } @@ -1449,21 +1435,22 @@ fn foo(t: E) { check_assist( add_missing_match_arms, r#" -enum E { #[doc(hidden)] A, } - -fn foo(t: E) { +//- /main.rs crate:main deps:e +fn foo(t: ::e::E) { match $0t { _ => todo!(), } -}"#, +} +//- /e.rs crate:e +pub enum E { #[doc(hidden)] A, } +"#, r#" -enum E { #[doc(hidden)] A, } - -fn foo(t: E) { +fn foo(t: ::e::E) { match t { _ => todo!(), } -}"#, +} +"#, ); } @@ -1473,24 +1460,24 @@ fn foo(t: E) { check_assist( add_missing_match_arms, r#" -#[non_exhaustive] -enum E { A, } - -fn foo(t: E) { +//- /main.rs crate:main deps:e +fn foo(t: ::e::E) { match $0t { - E::A => todo!(), + e::E::A => todo!(), } -}"#, - r#" +} +//- /e.rs crate:e #[non_exhaustive] -enum E { A, } - -fn foo(t: E) { +pub enum E { A, } +"#, + r#" +fn foo(t: ::e::E) { match t { - E::A => todo!(), + e::E::A => todo!(), ${0:_} => todo!(), } -}"#, +} +"#, ); } @@ -1500,23 +1487,23 @@ fn foo(t: E) { check_assist( add_missing_match_arms, r#" -#[non_exhaustive] -enum E { A, } - -fn foo(t: E) { +//- /main.rs crate:main deps:e +fn foo(t: ::e::E) { match $0t { } -}"#, - r#" +} +//- /e.rs crate:e #[non_exhaustive] -enum E { A, } - -fn foo(t: E) { +pub enum E { A, } +"#, + r#" +fn foo(t: ::e::E) { match t { - $0E::A => todo!(), + $0e::E::A => todo!(), _ => todo!(), } -}"#, +} +"#, ); } @@ -1526,23 +1513,22 @@ fn foo(t: E) { check_assist( add_missing_match_arms, r#" -#[non_exhaustive] -enum E { A, #[doc(hidden)] B } - -fn foo(t: E) { +//- /main.rs crate:main deps:e +fn foo(t: ::e::E) { match $0t { } -}"#, - r#" +} +//- /e.rs crate:e #[non_exhaustive] -enum E { A, #[doc(hidden)] B } - -fn foo(t: E) { +pub enum E { A, #[doc(hidden)] B }"#, + r#" +fn foo(t: ::e::E) { match t { - $0E::A => todo!(), + $0e::E::A => todo!(), _ => todo!(), } -}"#, +} +"#, ); } @@ -1552,48 +1538,48 @@ fn foo(t: E) { check_assist( add_missing_match_arms, r#" -#[non_exhaustive] -enum E { #[doc(hidden)] A } - -fn foo(t: E) { +//- /main.rs crate:main deps:e +fn foo(t: ::e::E) { match $0t { - E::A => todo!(), + e::E::A => todo!(), } -}"#, - r#" +} +//- /e.rs crate:e #[non_exhaustive] -enum E { #[doc(hidden)] A } - -fn foo(t: E) { +pub enum E { A, #[doc(hidden)] B }"#, + r#" +fn foo(t: ::e::E) { match t { - E::A => todo!(), + e::E::A => todo!(), ${0:_} => todo!(), } -}"#, +} +"#, ); } #[test] fn fill_wildcard_with_partial_wildcard() { + cov_mark::check!(added_wildcard_pattern); check_assist( add_missing_match_arms, r#" -enum E { #[doc(hidden)] A, } - -fn foo(t: E, b: bool) { +//- /main.rs crate:main deps:e +fn foo(t: ::e::E, b: bool) { match $0t { _ if b => todo!(), } -}"#, +} +//- /e.rs crate:e +pub enum E { #[doc(hidden)] A, }"#, r#" -enum E { #[doc(hidden)] A, } - -fn foo(t: E, b: bool) { +fn foo(t: ::e::E, b: bool) { match t { _ if b => todo!(), ${0:_} => todo!(), } -}"#, +} +"#, ); } @@ -1602,44 +1588,95 @@ fn foo(t: E, b: bool) { check_assist( add_missing_match_arms, r#" -enum E { #[doc(hidden)] A, } - -fn foo(t: E, b: bool) { +//- /main.rs crate:main deps:e +fn foo(t: ::e::E, b: bool) { match $0t { _ if b => todo!(), _ => todo!(), } -}"#, +} +//- /e.rs crate:e +pub enum E { #[doc(hidden)] A, }"#, r#" -enum E { #[doc(hidden)] A, } - -fn foo(t: E, b: bool) { +fn foo(t: ::e::E, b: bool) { match t { _ if b => todo!(), _ => todo!(), } +} +"#, + ); + } + + #[test] + fn non_exhaustive_doc_hidden_tuple_fills_wildcard() { + cov_mark::check!(added_wildcard_pattern); + check_assist( + add_missing_match_arms, + r#" +//- /main.rs crate:main deps:e +fn foo(t: ::e::E) { + match $0t { + } +} +//- /e.rs crate:e +#[non_exhaustive] +pub enum E { A, #[doc(hidden)] B, }"#, + r#" +fn foo(t: ::e::E) { + match t { + $0e::E::A => todo!(), + _ => todo!(), + } +} +"#, + ); + } + + #[test] + fn ignores_doc_hidden_for_crate_local_enums() { + check_assist( + add_missing_match_arms, + r#" +enum E { A, #[doc(hidden)] B, } + +fn foo(t: E) { + match $0t { + } +}"#, + r#" +enum E { A, #[doc(hidden)] B, } + +fn foo(t: E) { + match t { + $0E::A => todo!(), + E::B => todo!(), + } }"#, ); } #[test] - fn non_exhaustive_doc_hidden_tuple_fills_wildcard() { + fn ignores_doc_hidden_for_crate_local_enums_but_not_non_exhaustive() { + cov_mark::check!(added_wildcard_pattern); check_assist( add_missing_match_arms, r#" +#[non_exhaustive] enum E { A, #[doc(hidden)] B, } -fn foo(t: E, b: bool) { - match $0(t, b) { +fn foo(t: E) { + match $0t { } }"#, r#" +#[non_exhaustive] enum E { A, #[doc(hidden)] B, } -fn foo(t: E, b: bool) { - match (t, b) { - $0(E::A, true) => todo!(), - (E::A, false) => todo!(), +fn foo(t: E) { + match t { + $0E::A => todo!(), + E::B => todo!(), _ => todo!(), } }"#,