Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Rudxain opened this issue May 19, 2024 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Rudxain
Copy link

Rudxain commented May 19, 2024

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

@Rudxain Rudxain added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 19, 2024
@Rudxain Rudxain changed the title Suggest Some(&v) rather than Some(v.clone()) when calling is_some_and Suggest Some(&v) rather than Some(v.clone()) when calling owning methods May 19, 2024
@Rudxain Rudxain changed the title Suggest Some(&v) rather than Some(v.clone()) when calling owning methods Suggest Some(&v) rather than Some(v.clone()) when calling generic owning methods May 19, 2024
@gimbling-away
Copy link
Contributor

This doesn't seem like the appropriate help suggestion? Why suggest referencing instead? References and owned objects are very different

I'm in favor of the already existing help suggestion of cloning if the performance cost is acceptable

@Rudxain
Copy link
Author

Rudxain commented May 27, 2024

In that case, I believe both alternatives should be suggested, then let the dev/user decide which is appropriate for the given use-case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants