rust-lang / rust-lang/rust-analyzer

VSCode plugin does not suggest auto-import quick fix for enum-variant when `cargo check` does

Open
#15,307 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I have a minimal-ish reproducible example:

enum Enum {
    One,
    Two,
}

fn main() {
    let a = One;
}

Results in no available quick-fixes:

Screenshot 2023-07-18 at 01 32 08

If there was a pub fn or pub struct named One in a pub module, a quick-fix to import it would appear

Expected behaviour: have a quick fix to add an import of the enum variant.

rust-analyzer version: 0.4.1590-standalone (d82451103 2023-07-16)

rustc version: rustc 1.71.0 (8ede3aae2 2023-07-12)

relevant settings: all other vscode extensions disabled, only default settings for rust-analyzer

I would also like to note that the plugin in general is very slow in suggesting quick-fixes, especially related to adding missing imports, but that is probably somewhat unrelated. In general I would expect the plugin to show a quick fix if it can show the import-help from cargo check.

This is extra confusing if it happens in an `impl Trait` with default trait impls, which has the wrong quick-fix available
mod Mod {
    pub enum Enum {
        One,
        Two,
    }
}

use Mod::Enum;

struct S();

impl A for S {
    const OPT: &'static Enum = &One;
}

trait A {
    const OPT: &'static Enum;

    fn funky() {
        println!("oh no");
    }
}

Here cargo check reports the following:

error[E0425]: cannot find value `One` in this scope
  --> src/main.rs:13:32
   |
13 |     const OPT: &'static Enum = One;
   |                                ^^^ not found in this scope
   |
help: consider importing this unit variant
   |
8  + use crate::Enum::One;
   |

For more information about this error, try `rustc --explain E0425`.

Which it appears the extension catches:

Screenshot 2023-07-18 at 00 52 26

But the quick fix it suggests is only related to implementing the default trait member funky. Applying the quick fix will correctly add a copy of the original implementation of the trait member funky to the impl for S.

Screenshot 2023-07-18 at 00 53 18

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

Start with the minimal Rust reproduction and compare the cargo check import suggestion with the VSCode plugin's available quick fixes. Trace how rust-analyzer handles the diagnostic and chooses fixes for the unresolved enum variant. Done means the enum-variant import quick fix is offered and the incorrect trait-member fix does not replace it.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, vscode
Domain
developer-experience, devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.