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

Missed opportunity for the "consider borrowing" hint #103725

Open
Patryk27 opened this issue Oct 29, 2022 · 3 comments
Open

Missed opportunity for the "consider borrowing" hint #103725

Patryk27 opened this issue Oct 29, 2022 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Patryk27
Copy link
Contributor

Patryk27 commented Oct 29, 2022

Given the following code:

pub struct NotCopy;

trait AsMutWhatever {}

impl AsMutWhatever for &mut NotCopy {}

pub fn case_1() {
    let mut not_copy = NotCopy; 
    
    takes_mut_not_copy(&mut not_copy);
    takes_mut_not_copy(&mut not_copy);
}

pub fn case_2(not_copy: &mut NotCopy) {
    takes_mut_not_copy(not_copy);
    takes_mut_not_copy(not_copy);
}

fn takes_mut_not_copy(_not_copy: impl AsMutWhatever) {
   // do nothing 
}

(playground link)

... the current output is:

error [E0382]: use of moved value: `not_copy`
  --> src/lib.rs:16:24
   |
14 | pub fn case_2(not_copy: &mut NotCopy) {
   |               -------- move occurs because `not_copy` has type `&mut NotCopy`, which does not implement the `Copy` trait
15 |     takes_mut_not_copy(not_copy);
   |                        -------- value moved here
16 |     takes_mut_not_copy(not_copy);
   |                        ^^^^^^^^ value used here after move

Ideally, this should also display the consider mutably borrowing 'not_copy': &mut *not_copy hint, since that's the solution to problem 🙂

@Patryk27 Patryk27 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 Oct 29, 2022
@Patryk27 Patryk27 changed the title Missed opportunity for "consider borrowing" Missed opportunity for the "consider borrowing" hint Oct 29, 2022
@estebank estebank added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. labels Nov 11, 2022
@tgross35
Copy link
Contributor

Is it supposed to suggest cloning over borrowing for even simple non-mut stuff?

let mut tmp = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let paths = fs::read_dir(tmp).unwrap();
tmp.pop();

(playground)

help: consider cloning the value if the performance cost is acceptable
  |
6 |     let paths = fs::read_dir(tmp.clone()).unwrap();
  |                                 ++++++++

That message is kind of weird, "consider cloning the value if the performance cost is acceptable". I can imagine some contexts where it's useful, like returning owned values made from references. But this is a pretty trivial case when passing &tmp by reference works better.

@estebank
Copy link
Contributor

Current output:

error[E0382]: use of moved value: `not_copy`
  --> f75.rs:16:24
   |
14 | pub fn case_2(not_copy: &mut NotCopy) {
   |               -------- move occurs because `not_copy` has type `&mut NotCopy`, which does not implement the `Copy` trait
15 |     takes_mut_not_copy(not_copy);
   |                        -------- value moved here
16 |     takes_mut_not_copy(not_copy);
   |                        ^^^^^^^^ value used here after move
   |
note: consider changing this parameter type in function `takes_mut_not_copy` to borrow instead if owning the value isn't necessary
  --> f75.rs:19:34
   |
19 | fn takes_mut_not_copy(_not_copy: impl AsMutWhatever) {
   |    ------------------            ^^^^^^^^^^^^^^^^^^ this parameter takes ownership of the value
   |    |
   |    in this function

@tgross35 yes, borrowing would be a better suggestion here. That case is covered by #41708.

@estebank
Copy link
Contributor

estebank commented May 1, 2024

Current output:

error[E0382]: use of moved value: `not_copy`
  --> src/lib.rs:16:24
   |
14 | pub fn case_2(not_copy: &mut NotCopy) {
   |               -------- move occurs because `not_copy` has type `&mut NotCopy`, which does not implement the `Copy` trait
15 |     takes_mut_not_copy(not_copy);
   |                        -------- value moved here
16 |     takes_mut_not_copy(not_copy);
   |                        ^^^^^^^^ value used here after move
   |
note: consider changing this parameter type in function `takes_mut_not_copy` to borrow instead if owning the value isn't necessary
  --> src/lib.rs:19:34
   |
19 | fn takes_mut_not_copy(_not_copy: impl AsMutWhatever) {
   |    ------------------            ^^^^^^^^^^^^^^^^^^ this parameter takes ownership of the value
   |    |
   |    in this function
help: consider cloning the value if the performance cost is acceptable
   |
15 |     takes_mut_not_copy(not_copy).clone();
   |                                 ++++++++

I do not think that addressing the minimal solution to #41708 (which would be to explicitly look for an AsRef bound) would work for this (suggesting &mut that will continue working for the impl AsMutWhatever).

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 A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. 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

3 participants