Skip to content

feat: In "Fill match arms", allow users to prefer Self to the enum name when possible #19939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/ide-assists/src/assist_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct AssistConfig {
pub term_search_borrowck: bool,
pub code_action_grouping: bool,
pub expr_fill_default: ExprFillDefaultMode,
pub prefer_self_ty: bool,
}

impl AssistConfig {
Expand Down
207 changes: 178 additions & 29 deletions crates/ide-assists/src/handlers/add_missing_match_arms.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::iter::{self, Peekable};

use either::Either;
use hir::{Adt, Crate, HasAttrs, ImportPathConfig, ModuleDef, Semantics, sym};
use hir::{Adt, AsAssocItem, Crate, HasAttrs, ImportPathConfig, ModuleDef, Semantics, sym};
use ide_db::RootDatabase;
use ide_db::assists::ExprFillDefaultMode;
use ide_db::syntax_helpers::suggest_name;
use ide_db::{famous_defs::FamousDefs, helpers::mod_path_to_ast};
use itertools::Itertools;
use syntax::ToSmolStr;
use syntax::ast::edit::IndentLevel;
use syntax::ast::edit_in_place::Indent;
use syntax::ast::syntax_factory::SyntaxFactory;
Expand Down Expand Up @@ -79,12 +80,20 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)

let make = SyntaxFactory::with_mappings();

let module = ctx.sema.scope(expr.syntax())?.module();
let scope = ctx.sema.scope(expr.syntax())?;
let module = scope.module();
let self_ty = if ctx.config.prefer_self_ty {
scope
.containing_function()
.and_then(|function| function.as_assoc_item(ctx.db())?.implementing_ty(ctx.db()))
} else {
None
};
let (mut missing_pats, is_non_exhaustive, has_hidden_variants): (
Peekable<Box<dyn Iterator<Item = (ast::Pat, bool)>>>,
bool,
bool,
) = if let Some(enum_def) = resolve_enum_def(&ctx.sema, &expr) {
) = if let Some(enum_def) = resolve_enum_def(&ctx.sema, &expr, self_ty.as_ref()) {
let is_non_exhaustive = enum_def.is_non_exhaustive(ctx.db(), module.krate());

let variants = enum_def.variants(ctx.db());
Expand All @@ -102,16 +111,17 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
})
.filter(|(variant_pat, _)| is_variant_missing(&top_lvl_pats, variant_pat));

let option_enum = FamousDefs(&ctx.sema, module.krate()).core_option_Option().map(lift_enum);
let missing_pats: Box<dyn Iterator<Item = _>> = if Some(enum_def) == option_enum {
let option_enum = FamousDefs(&ctx.sema, module.krate()).core_option_Option();
let missing_pats: Box<dyn Iterator<Item = _>> = if matches!(enum_def, ExtendedEnum::Enum { enum_: e, .. } if Some(e) == option_enum)
{
// Match `Some` variant first.
cov_mark::hit!(option_order);
Box::new(missing_pats.rev())
} else {
Box::new(missing_pats)
};
(missing_pats.peekable(), is_non_exhaustive, has_hidden_variants)
} else if let Some(enum_defs) = resolve_tuple_of_enum_def(&ctx.sema, &expr) {
} else if let Some(enum_defs) = resolve_tuple_of_enum_def(&ctx.sema, &expr, self_ty.as_ref()) {
let is_non_exhaustive =
enum_defs.iter().any(|enum_def| enum_def.is_non_exhaustive(ctx.db(), module.krate()));

Expand Down Expand Up @@ -159,7 +169,9 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
is_non_exhaustive,
has_hidden_variants,
)
} else if let Some((enum_def, len)) = resolve_array_of_enum_def(&ctx.sema, &expr) {
} else if let Some((enum_def, len)) =
resolve_array_of_enum_def(&ctx.sema, &expr, self_ty.as_ref())
{
let is_non_exhaustive = enum_def.is_non_exhaustive(ctx.db(), module.krate());
let variants = enum_def.variants(ctx.db());

Expand Down Expand Up @@ -373,66 +385,81 @@ fn does_pat_match_variant(pat: &Pat, var: &Pat) -> bool {
}
}

#[derive(Eq, PartialEq, Clone, Copy)]
#[derive(Eq, PartialEq, Clone)]
enum ExtendedEnum {
Bool,
Enum(hir::Enum),
Enum { enum_: hir::Enum, use_self: bool },
}

#[derive(Eq, PartialEq, Clone, Copy, Debug)]
enum ExtendedVariant {
True,
False,
Variant(hir::Variant),
Variant { variant: hir::Variant, use_self: bool },
}

impl ExtendedVariant {
fn should_be_hidden(self, db: &RootDatabase, krate: Crate) -> bool {
match self {
ExtendedVariant::Variant(var) => {
ExtendedVariant::Variant { variant: var, .. } => {
var.attrs(db).has_doc_hidden() && var.module(db).krate() != krate
}
_ => false,
}
}
}

fn lift_enum(e: hir::Enum) -> ExtendedEnum {
ExtendedEnum::Enum(e)
}

impl ExtendedEnum {
fn is_non_exhaustive(self, db: &RootDatabase, krate: Crate) -> bool {
fn enum_(
db: &RootDatabase,
enum_: hir::Enum,
enum_ty: &hir::Type,
self_ty: Option<&hir::Type>,
) -> Self {
ExtendedEnum::Enum {
enum_,
use_self: self_ty.is_some_and(|self_ty| self_ty.could_unify_with_deeply(db, enum_ty)),
}
}

fn is_non_exhaustive(&self, db: &RootDatabase, krate: Crate) -> bool {
match self {
ExtendedEnum::Enum(e) => {
ExtendedEnum::Enum { enum_: e, .. } => {
e.attrs(db).by_key(sym::non_exhaustive).exists() && e.module(db).krate() != krate
}
_ => false,
}
}

fn variants(self, db: &RootDatabase) -> Vec<ExtendedVariant> {
match self {
ExtendedEnum::Enum(e) => {
e.variants(db).into_iter().map(ExtendedVariant::Variant).collect::<Vec<_>>()
}
fn variants(&self, db: &RootDatabase) -> Vec<ExtendedVariant> {
match *self {
ExtendedEnum::Enum { enum_: e, use_self } => e
.variants(db)
.into_iter()
.map(|variant| ExtendedVariant::Variant { variant, use_self })
.collect::<Vec<_>>(),
ExtendedEnum::Bool => {
Vec::<ExtendedVariant>::from([ExtendedVariant::True, ExtendedVariant::False])
}
}
}
}

fn resolve_enum_def(sema: &Semantics<'_, RootDatabase>, expr: &ast::Expr) -> Option<ExtendedEnum> {
fn resolve_enum_def(
sema: &Semantics<'_, RootDatabase>,
expr: &ast::Expr,
self_ty: Option<&hir::Type>,
) -> Option<ExtendedEnum> {
sema.type_of_expr(expr)?.adjusted().autoderef(sema.db).find_map(|ty| match ty.as_adt() {
Some(Adt::Enum(e)) => Some(ExtendedEnum::Enum(e)),
Some(Adt::Enum(e)) => Some(ExtendedEnum::enum_(sema.db, e, &ty, self_ty)),
_ => ty.is_bool().then_some(ExtendedEnum::Bool),
})
}

fn resolve_tuple_of_enum_def(
sema: &Semantics<'_, RootDatabase>,
expr: &ast::Expr,
self_ty: Option<&hir::Type>,
) -> Option<Vec<ExtendedEnum>> {
sema.type_of_expr(expr)?
.adjusted()
Expand All @@ -441,7 +468,7 @@ fn resolve_tuple_of_enum_def(
.map(|ty| {
ty.autoderef(sema.db).find_map(|ty| {
match ty.as_adt() {
Some(Adt::Enum(e)) => Some(lift_enum(e)),
Some(Adt::Enum(e)) => Some(ExtendedEnum::enum_(sema.db, e, &ty, self_ty)),
// For now we only handle expansion for a tuple of enums. Here
// we map non-enum items to None and rely on `collect` to
// convert Vec<Option<hir::Enum>> into Option<Vec<hir::Enum>>.
Expand All @@ -456,10 +483,11 @@ fn resolve_tuple_of_enum_def(
fn resolve_array_of_enum_def(
sema: &Semantics<'_, RootDatabase>,
expr: &ast::Expr,
self_ty: Option<&hir::Type>,
) -> Option<(ExtendedEnum, usize)> {
sema.type_of_expr(expr)?.adjusted().as_array(sema.db).and_then(|(ty, len)| {
ty.autoderef(sema.db).find_map(|ty| match ty.as_adt() {
Some(Adt::Enum(e)) => Some((lift_enum(e), len)),
Some(Adt::Enum(e)) => Some((ExtendedEnum::enum_(sema.db, e, &ty, self_ty), len)),
_ => ty.is_bool().then_some((ExtendedEnum::Bool, len)),
})
})
Expand All @@ -474,9 +502,21 @@ fn build_pat(
) -> Option<ast::Pat> {
let db = ctx.db();
match var {
ExtendedVariant::Variant(var) => {
ExtendedVariant::Variant { variant: var, use_self } => {
let edition = module.krate().edition(db);
let path = mod_path_to_ast(&module.find_path(db, ModuleDef::from(var), cfg)?, edition);
let path = if use_self {
make::path_from_segments(
[
make::path_segment(make::name_ref_self_ty()),
make::path_segment(make::name_ref(
&var.name(db).display(db, edition).to_smolstr(),
)),
],
false,
)
} else {
mod_path_to_ast(&module.find_path(db, ModuleDef::from(var), cfg)?, edition)
};
let fields = var.fields(db);
let pat: ast::Pat = match var.kind(db) {
hir::StructKind::Tuple => {
Expand Down Expand Up @@ -509,8 +549,10 @@ fn build_pat(

#[cfg(test)]
mod tests {
use crate::AssistConfig;
use crate::tests::{
check_assist, check_assist_not_applicable, check_assist_target, check_assist_unresolved,
TEST_CONFIG, check_assist, check_assist_not_applicable, check_assist_target,
check_assist_unresolved, check_assist_with_config,
};

use super::add_missing_match_arms;
Expand Down Expand Up @@ -2095,4 +2137,111 @@ fn f() {
"#,
);
}

#[test]
fn prefer_self() {
check_assist_with_config(
add_missing_match_arms,
AssistConfig { prefer_self_ty: true, ..TEST_CONFIG },
r#"
enum Foo {
Bar,
Baz,
}

impl Foo {
fn qux(&self) {
match self {
$0_ => {}
}
}
}
"#,
r#"
enum Foo {
Bar,
Baz,
}

impl Foo {
fn qux(&self) {
match self {
Self::Bar => ${1:todo!()},
Self::Baz => ${2:todo!()},$0
}
}
}
"#,
);
}

#[test]
fn prefer_self_with_generics() {
check_assist_with_config(
add_missing_match_arms,
AssistConfig { prefer_self_ty: true, ..TEST_CONFIG },
r#"
enum Foo<T> {
Bar(T),
Baz,
}

impl<T> Foo<T> {
fn qux(&self) {
match self {
$0_ => {}
}
}
}
"#,
r#"
enum Foo<T> {
Bar(T),
Baz,
}

impl<T> Foo<T> {
fn qux(&self) {
match self {
Self::Bar(${1:_}) => ${2:todo!()},
Self::Baz => ${3:todo!()},$0
}
}
}
"#,
);
check_assist_with_config(
add_missing_match_arms,
AssistConfig { prefer_self_ty: true, ..TEST_CONFIG },
r#"
enum Foo<T> {
Bar(T),
Baz,
}

impl<T> Foo<T> {
fn qux(v: Foo<i32>) {
match v {
$0_ => {}
}
}
}
"#,
r#"
enum Foo<T> {
Bar(T),
Baz,
}

impl<T> Foo<T> {
fn qux(v: Foo<i32>) {
match v {
Foo::Bar(${1:_}) => ${2:todo!()},
Foo::Baz => ${3:todo!()},$0
}
}
}
"#,
);
}
}
Loading