rust-lang / rust-lang/rust

Suggest `Some(&v)` rather than `Some(v.clone())` when calling generic owning methods

Open
#125,268 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
#[allow(unused)]
fn f() {
    let v = vec![0];
    Some(v).is_some_and(|_| true);
    drop(v);
}
Current output
Compiling playground v0.0.1 (/playground)
error[E0382]: use of moved value: `v`
 --> src/lib.rs:5:10
  |
3 |     let v = vec![0];
  |         - move occurs because `v` has type `Vec<i32>`, which does not implement the `Copy` trait
4 |     Some(v).is_some_and(|_| true);
  |          - value moved here
5 |     drop(v);
  |          ^ value used here after move
  |
help: consider cloning the value if the performance cost is acceptable
  |
4 |     Some(v.clone()).is_some_and(|_| true);
  |           ++++++++

For more information about this error, try `rustc --explain E0382`.
error: could not compile `playground` (lib) due to 1 previous error
Desired output
Compiling playground v0.0.1 (/playground)
error[E0382]: use of moved value: `v`
 --> src/lib.rs:5:10
  |
3 |     let v = vec![0];
  |         - move occurs because `v` has type `Vec<i32>`, which does not implement the `Copy` trait
4 |     Some(v).is_some_and(|_| true);
  |          - value moved here
5 |     drop(v);
  |          ^ value used here after move
  |
help: consider borrowing the value
  |
4 |     Some(&v).is_some_and(|_| true);
  |          +

For more information about this error, try `rustc --explain E0382`.
error: could not compile `playground` (lib) due to 1 previous error
Rationale and extra context

No response

Other cases

No response

Rust Version
1.80.0-nightly

(2024-05-18 b1ec1bd65f89c1375d2c)
Anything else?

No response

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 supplied Rust 1.80.0-nightly snippet and comparing the current E0382 help text with the desired output. Trace the compiler diagnostic suggestion for this generic owning-method case; done when the suggestion prefers borrowing with &v rather than cloning and the reported example is covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.