Skip to content
Open
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
84 changes: 75 additions & 9 deletions crates/ide-assists/src/handlers/generate_deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use ide_db::{RootDatabase, famous_defs::FamousDefs};
use syntax::{
AstNode, Edition, SyntaxNode,
ast::{self, HasName},
ast::{self, HasGenericParams, HasName},
};

use crate::{
Expand Down Expand Up @@ -162,22 +162,66 @@
sema: &hir::Semantics<'_, RootDatabase>,
strukt: &ast::Struct,
) -> Option<DerefType> {
let strukt = sema.to_def(strukt)?;
let krate = strukt.module(sema.db).krate();
let strukt_def = sema.to_def(strukt)?;
let krate = strukt_def.module(sema.db).krate();

let deref_trait = FamousDefs(sema, krate).core_ops_Deref()?;
let deref_mut_trait = FamousDefs(sema, krate).core_ops_DerefMut()?;
let strukt_type = strukt.ty(sema.db);

if strukt_type.impls_trait(sema.db, deref_trait, &[]) {
if strukt_type.impls_trait(sema.db, deref_mut_trait, &[]) {
Some(DerefType::DerefMut)
let has_generics = strukt.generic_param_list().is_some();

if has_generics {
let has_deref_impl = has_actual_impl(sema, &strukt_def, deref_trait);
let has_deref_mut_impl = has_actual_impl(sema, &strukt_def, deref_mut_trait);

if has_deref_impl {
if has_deref_mut_impl { Some(DerefType::DerefMut) } else { Some(DerefType::Deref) }
} else {
Some(DerefType::Deref)
None
}
} else {
None
let strukt_type = strukt_def.ty(sema.db);

if strukt_type.impls_trait(sema.db, deref_trait, &[]) {
if strukt_type.impls_trait(sema.db, deref_mut_trait, &[]) {
Some(DerefType::DerefMut)
} else {
Some(DerefType::Deref)
}
} else {
None
}
}
}

fn has_actual_impl(
sema: &hir::Semantics<'_, RootDatabase>,
strukt_def: &hir::Struct,
trait_: hir::Trait,
) -> bool {
let module = strukt_def.module(sema.db);

let impls = module.impl_defs(sema.db);

for impl_def in impls {
if let Some(impl_trait) = impl_def.trait_(sema.db) {
if impl_trait != trait_ {
continue;
}
} else {
continue;
}

if let Some(impl_target) = impl_def.self_ty(sema.db).as_adt() {

Check failure on line 215 in crates/ide-assists/src/handlers/generate_deref.rs

View workflow job for this annotation

GitHub Actions / Rust (macos-latest)

this `if` statement can be collapsed
if let hir::Adt::Struct(impl_struct) = impl_target {

Check failure on line 216 in crates/ide-assists/src/handlers/generate_deref.rs

View workflow job for this annotation

GitHub Actions / Rust (macos-latest)

this `if` statement can be collapsed

Check failure on line 216 in crates/ide-assists/src/handlers/generate_deref.rs

View workflow job for this annotation

GitHub Actions / Rust (macos-latest)

this `if let` can be collapsed into the outer `if let`
if impl_struct == *strukt_def {
return true;
}
}
}
}

false
}

#[derive(Debug)]
Expand Down Expand Up @@ -371,4 +415,26 @@
"#,
)
}

#[test]
fn test_generate_deref_with_generic_no_existing_impl() {
check_assist(
generate_deref,
r#"
//- minicore: deref
struct Foo<T>($0T);
"#,
r#"
struct Foo<T>(T);

impl<T> core::ops::Deref for Foo<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.0
}
}
"#,
);
}
}
Loading