rust-lang / rust-lang/rust-analyzer

"Remove all the unused imports" doesn't detect item-like usage of imports correctly

Open
#15,413 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

main.rs

use sub1::Foo;

mod sub1;
mod sub2;

fn main() {
    sub2::do_something();
}

sub1.rs:

#[derive(Debug)]
pub struct Foo;

sub2.rs:

pub fn do_something() {
    let f = crate::Foo;
    dbg!(f);
}

The "remove all the unused imports" is suggested for use sub1::Foo; in main. If applied, it removes the import, invalidating the code in sub2.rs which tries to use crate::Foo, which is valid because imports are item-like, and can be imported as such.

This seems to only happen if the modules are in different files. The following code, which inlines all the modules does not cause this behavior. The assist is not suggested:

use sub1::Foo;

mod sub1 {
    #[derive(Debug)]
    pub struct Foo;
}
mod sub2 {
    pub fn do_something() {
        let f = crate::Foo;
        dbg!(f);
    }
}

fn main() {
    sub2::do_something();
}

rust-analyzer version 0.3.1615-standalone

rustc version: rustc 1.73.0-nightly (04abc370b 2023-07-28)

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 two-file Rust example in the issue and reproduce the suggested “remove all the unused imports” assist. Locate the implementation and its unused-import analysis, then trace cross-file item-like usage such as crate::Foo; done means the assist is not offered when applying it would invalidate that usage, with a regression test for the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.