rust-lang / rust-lang/rust-analyzer

`merge imports` and `unmerge use` deletes `$0` from code

Open
#14,178 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

When invoking a merge imports code action on the following code removes the first occurrence of $0 from the file. The same thing happens regardless of whether the two characters are in a comment or a string literal.

If there is no $0 in the file, the cursor is placed at the end of the file.

Other code actions such as remove unnecessary braces (around a use item) do not trigger the delete.

#![allow(unused_imports)]
#![allow(dead_code)]

mod x {
    pub struct A;
    pub struct B;
}

use x::A;
use x::B;

// Comment containing $0 sequence

fn main() {
    _s = "This $0 will be deleted on the next merge/unmerge code action.";
}

I have found this while looking into the internals and as far as I understand $0 is used to model where a cursor is for examples and tests. I'll try to figure out what's happening inside but any pointers would be appreciated.

rust-analyzer version: rust-analyzer version: 0.4.1408-standalone (a6603fc21 2023-02-16)

rustc version: rustc 1.67.1 (d5a82bbd2 2023-02-07)

relevant settings: Windows, VS Code, no special settings

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 merge imports and unmerge use code actions in VS Code with the provided Rust snippet, checking both the comment and string cases. Trace how $0 is handled for code-action examples and tests; done means the code actions no longer delete $0 or move the cursor unexpectedly, while other actions remain unaffected.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.