rust-lang / rust-lang/rust-analyzer

When adding an import based on a completion to a unit test it becomes an unused import in the normal build

Open
#12,661 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

The problem is that if you have some code like

#[test]
fn test_all_room_ids_different() {
  let mut set = HashS
}

and then you press Ctrl+Space at the end of the line and pick std::collections::HashSet as the completion, it'll add the std::collections::HashSet to the top of the entire module. Except if nothing else in the module is using a HashSet then the non-test build now marks the import as an unused import.

I don't know what the good answer is here, but there are two options that come to mind:

  1. put the new import at the top of the test function
  2. put the new import at the top of the module behind #[cfg(test)]

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

Reproduce the completion-import behavior in a Rust unit test where HashSet is used only inside #[test]. Then inspect the completion import insertion entry point and determine whether the import should be scoped to the test or guarded with #[cfg(test)]. Done means the normal build no longer reports the generated import as unused.

Written by the indexing model from the issue text.

Assessment

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