rust-lang / rust-lang/rust-analyzer

Does "Extract…" not work within macros?

Open
#14,126 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Steps to reproduce:

  1. cargo new <project>

  2. open <project>/src/main.rs in VS Code with rust-analyzer:

    fn main() {
        println!("Hello, world!");
    }
    
  3. select the string "Hello, world!"

  4. right-click and select Refactor… from the context menu

Expected behavior:

  • "Extract…" assists appear ("Extract into variable", "Extract into function")

Actual behavior:

  • Only "Rewrite…" assists appear ("Rewrite as raw string", "Inline macro").

Notes:

  1. I've verified that:

    • "extract" works fine for strings in other contexts (e.g. let s = "foo";)
    • the same behavior happens for non-strings in macros (e.g. I can't extract 2 from let v = vec![1, 2, 3];)
  2. I tried using "Inline macro" in hopes of expanding till I was able to extract the string, but this didn't work; the println! call expands to

    {$crate::io::_print($crate::format_args_nl!("Hello, world!"));};
    

    which rust-analyzer isn't happy with ($ produces Syntax error: expected expression) — so I can't go on to expand format_args_nl! as I would assume I'd need to to get to the string.

I'm new to both Rust and VS Code, so I may be missing something. Coming from CLion, though, where Rust plugin handles it just fine, it seems a little weird.

I'm guessing CLion's doing something a bit hacky, while rust-analyzer's extract assist is trying to guarantee correctness, but the std macros must be common enough, I wonder if it isn't worth a little special-casing.

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 report from a new Cargo project by opening src/main.rs in VS Code with rust-analyzer and trying Refactor… on the string or a value inside a macro. Start by tracing rust-analyzer's extract-assist handling for macro arguments; done means the expected extraction assists appear without breaking the existing rewrite assists.

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.