rust-lang / rust-lang/rust-analyzer

VSCode "Remove all unused imports" can incorrectly remove nested use statement imports

Open
#21,678 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

rust-analyzer version: rust-analyzer 1.93.1 (01f6ddf 2026-02-11)

rustc version: rustc 1.93.1 (01f6ddf75 2026-02-11)

editor or extension: VSCode 0.3.2795

relevant settings: None

repository link (if public, optional): N/a

code snippet to reproduce:

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

fn nested_import() {
    use IpAddr::V4;

    let fake_ip = V4(Ipv4Addr::new(127, 0, 0, 1));
    println!("{fake_ip}");
}

fn main() {
    nested_import();
}

This code compiles cleanly. Having an unrelated unused import (Ipv6Addr in this example) causes VSCode to use a yellow-squiggle to underline it. Taking the VSCode action to Remove all unused imports then incorrectly removes imports that are later used within function scopes to create more specific imports. Here, IpAddr is used within the function to import use IpAddr::V4.

In this example, running the VSCode action on Ipv6Addr while remove IpAddr, causing a compile error on line 3 use IpAddr::V4.

The correct transformation is made with cargo fix --allow-dirty so I believe the issue is in this repo.

VSCode's Incorrect Transformation:

use std::net::{Ipv4Addr};

fn nested_import() {
    use IpAddr::V4;
    //   ^ Unresolved import error

    let fake_ip = V4(Ipv4Addr::new(127, 0, 0, 1));
    println!("{fake_ip}");
}

fn main() {
    nested_import();
}

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 by reproducing the nested use example in VSCode and compare the Remove all unused imports result with cargo fix --allow-dirty. Trace the unused-import removal entry point and add coverage for preserving imports referenced by nested use statements; done means the action removes only genuinely unused imports and the example still compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, vscode
Domain
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.