Skip to content

Commit f0428e5

Browse files
authored
Simplify redundant coercion
1 parent 97ecfe4 commit f0428e5

File tree

1 file changed

+8
-30
lines changed

1 file changed

+8
-30
lines changed

crates/ide-assists/src/utils.rs

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::ops;
44

55
pub(crate) use gen_trait_fn_body::gen_trait_fn_body;
6-
use hir::{db::HirDatabase, known, HasAttrs as HirHasAttrs, HirDisplay, InFile, Semantics};
6+
use hir::{db::HirDatabase, HasAttrs as HirHasAttrs, HirDisplay, InFile, Semantics};
77
use ide_db::{
88
famous_defs::FamousDefs, path_transform::PathTransform,
99
syntax_helpers::insert_whitespace_into_node::insert_ws_into, RootDatabase, SnippetCap,
@@ -685,32 +685,10 @@ pub(crate) fn convert_reference_type(
685685
.map(|(conversion, impls_deref)| ReferenceConversion { ty, conversion, impls_deref })
686686
}
687687

688-
fn impls_deref_for_target(
689-
ty: &hir::Type,
690-
target: hir::Type,
691-
db: &dyn HirDatabase,
692-
famous_defs: &FamousDefs<'_, '_>,
693-
) -> bool {
694-
if let Some(deref_trait) = famous_defs.core_ops_Deref() {
695-
if ty.impls_trait(db, deref_trait, &[]) {
696-
let assoc_type_item = deref_trait.items(db).into_iter().find_map(|item| match item {
697-
hir::AssocItem::TypeAlias(alias) if alias.name(db) == known::Target => Some(alias),
698-
_ => None,
699-
});
700-
if let Some(assoc_type_item) = assoc_type_item {
701-
matches!(
702-
ty.normalize_trait_assoc_type(db, &[], assoc_type_item),
703-
Some(ty) if ty.could_coerce_to(db, &target),
704-
)
705-
} else {
706-
false
707-
}
708-
} else {
709-
false
710-
}
711-
} else {
712-
false
713-
}
688+
fn could_deref_to_target(ty: &hir::Type, target: &hir::Type, db: &dyn HirDatabase) -> bool {
689+
let ty_ref = hir:Type::reference(ty, hir::Mutability::Shared);
690+
let target_ref = hir:Type::reference(target, hir::Mutability::Shared);
691+
ty_ref.could_coerece_to(db, &target_ref)
714692
}
715693

716694
fn handle_copy(ty: &hir::Type, db: &dyn HirDatabase) -> Option<(ReferenceConversionType, bool)> {
@@ -726,7 +704,7 @@ fn handle_as_ref_str(
726704

727705
ty.impls_trait(db, famous_defs.core_convert_AsRef()?, &[str_type.clone()]).then_some((
728706
ReferenceConversionType::AsRefStr,
729-
impls_deref_for_target(ty, str_type, db, famous_defs),
707+
could_deref_to_target(ty, &str_type, db),
730708
))
731709
}
732710

@@ -740,7 +718,7 @@ fn handle_as_ref_slice(
740718

741719
ty.impls_trait(db, famous_defs.core_convert_AsRef()?, &[slice_type.clone()]).then_some((
742720
ReferenceConversionType::AsRefSlice,
743-
impls_deref_for_target(ty, slice_type, db, famous_defs),
721+
could_deref_to_target(ty, &slice_type, db),
744722
))
745723
}
746724

@@ -753,7 +731,7 @@ fn handle_dereferenced(
753731

754732
ty.impls_trait(db, famous_defs.core_convert_AsRef()?, &[type_argument.clone()]).then_some((
755733
ReferenceConversionType::Dereferenced,
756-
impls_deref_for_target(ty, type_argument, db, famous_defs),
734+
could_deref_to_target(ty, &type_argument, db),
757735
))
758736
}
759737

0 commit comments

Comments
 (0)