rust-lang / rust-lang/rust-analyzer

Lint option: Highlighting copies

Open
#7,379 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-highlighting S-unactionable
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

It would be nice to have a lint feature that, when enabled, will highlight all instances where a variable is copied. This would be useful because it's not always clear when a copy would occur, and highlighting copies could point out areas where code compiles correctly, but could be written better.

For example:

#[derive(Debug, Clone, Copy)]
struct Test {
    num: [u8; 20],
}

fn func(_: Test) {
    // ...
}

fn main() {
    let t = Test{
        num:[0;20],
    };
    let t2 = t.clone();
    func(t);
    println!("{:?}", t2);
}

In this case, the author of the code is unaware that Test implements Copy, so they clone t to pass to func() in order to keep using t after they assume it was moved. When highlighting is enabled, the t in func(t) would be highlighted which would provide a visual indicator that the clone is unnecessary, as t will be copied instead of moved when passed to func.

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 examining rust-analyzer's existing lint and highlighting behavior, then trace how the Rust example's Copy semantics are represented. Define the enabled-option scope and how copied variable uses should be identified and highlighted, and add coverage for the provided Test example before verifying the resulting editor highlighting.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
developer-experience, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.