rust-lang / rust-lang/rust-analyzer

[feature request] Assist for auto deriving traits if that trait is attempting to be used

Open
#13,585 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics C-feature
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

Apologies if this has been logged already in some other way, I've read through a few issues but not sure if they're talking about the same thing.

Given the following code:

struct NoDebug {
    num: i32,
}

fn main() {
    let no_db = NoDebug { num: 5 };
    println!("{:?}", no_db);
}

rustc will give compiler error E0277 saying that trait Debug is not implemented for struct NoDebug.

Similarly if we change println!("{:?}", no_db); to println!("{}", no_db); we will get E0277 again but with message trait std::fmt::Display is not implemented for NoDebug.

There are 2 possible fixes, first is to #[derive(Debug)] for the struct, and second is to impl Debug for NoDebug however both of these need to be done manually (although there are other assists to help with generating the impl code already)

It would be good if we could detect what trait impl is missing when we attempt to use the trait, and either derive it automatically on the struct or generate the impl for that trait on the struct then and there.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no file, test, or entry point, so first identify where rust-analyzer handles missing trait implementations and existing assists for deriving or generating impls. Done means offering an assist when a required trait such as Debug or Display is missing, with generated code or a derive that resolves the diagnostic.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.