rust-lang / rust-lang/rust-analyzer

Suggestion: Add indicator to annotations for Copy types when an assignment/binding creates a copy

Open
#11,449 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

This example came from the Rust community discord, a user was having issues where their modifications were not being persisted back to the parent object because the child struct was Copy:

    fn move_player_entity(&mut self, id: usize) {
        let mut option = self.entities[id];
        if let Some(cell) = &mut option {
            if let Some(player) = &self.players[cell.get_owner_id().unwrap()] {
                let mut dx = player.protocol.mouse.x - cell.x;
                let mut dy = player.protocol.mouse.y - cell.y;
                let d = f64::sqrt(dx * dx + dy * dy);
                if d < 1.0 { return }
                dx /= d;
                dy /= d;
                let m = f64::min(cell.get_move_speed() , d) * 2.0; //TODO : setting cell speed
                cell.x += dx * m;
                cell.y += dy * m;
                cell.has_changed = true;
            }
        }
    }

In this example, cell is Copy, option is Option<Cell>. It's not immediately obvious with the indexing that this would automatically Copy the value. In this instance, line 2 should be let option = &mut self.entities[id]; to allow the original entity item to be modified.

Non-Copy values can be pretty obvious when they move things out, often careless use of that will result in errors. Copy types by contrast can sometimes be a little insidious in that it's not really clear sometimes that they're being copied like this.

I would suggest that for any non-primitive type that implements Copy, RA could add a small (Copied) note to its type annotation of the let binding to indicate to the user that the value will be copied by this operation. If there's a way to add additional information somehow, showing possible workarounds if the copy was not desired might be a good addition as well. (Usually, this will be adding & or &mut depending on whether mutation is needed.)

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 indexed Copy-value example in rust-analyzer and inspect how type annotations are currently presented for let bindings. Determine where a copied non-primitive Copy value could be identified and how an annotation should distinguish it from ordinary moves; done means the annotation is accurate, unobtrusive, and covered by tests for the shown assignment and reference-based alternatives.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.