Skip to content

hir::Type::impls_trait false positive #20574

@A4-Tacks

Description

@A4-Tacks

rust-analyzer version: rust-analyzer 0.4.2595-standalone

rustc version: rustc 1.91.0-nightly (040a98af7 2025-08-20)

editor or extension: VIM9.1

code snippet to reproduce:

struct Foo<T>($0T);

Output assists:

╭Choose action─────────────────────╮
│1. Change visibility to pub(crate)│
│2. Add `#[derive]`                │
│3. Generate impl for `Foo`        │
│4. Generate trait impl for `Foo`  │
│5. Generate `new`                 │
╰──────────────────────────────────╯

Generate `Deref` not exist

Output assists (rust-analyzer 1.91.0-nightly (040a98a 2025-08-20):

╭Choose action─────────────────────╮
│1. Change visibility to pub(crate)│
│2. Generate `Deref` impl using `T`│
│3. Add `#[derive]`                │
│4. Generate impl for `Foo`        │
│5. Generate trait impl for `Foo`  │
│6. Generate `new`                 │
╰──────────────────────────────────╯

This problem from crates/ide-assists/src/handlers/generate_deref.rs line 172:

    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
    }

For has generic parameter type, impls_trait seems to have unexpectedly returned true

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions