Skip to content

E0507 suggests clone() in the wrong position when using the ? operator #113368

New issue

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

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

Already on GitHub? # to your account

Closed
NoobTracker opened this issue Jul 5, 2023 · 1 comment
Closed
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

@NoobTracker
Copy link

NoobTracker commented Jul 5, 2023

Code

#[derive(Clone)]
struct SomethingThatDoesntImplCopy{
    x: i32
}
struct MyStruct {
  field: SomethingThatDoesntImplCopy
}
struct MyWrapper{
  field: Option<SomethingThatDoesntImplCopy>
}
fn test(param: &MyWrapper) -> Option<()>{
  let _ = MyStruct{
    field: param.field?
  };
  None
}
fn main() {

}

Current output

error[E0507]: cannot move out of `param.field` which is behind a shared reference
   --> src/main.rs:13:12
    |
13  |     field: param.field?
    |            ^^^^^^^^^^^-
    |            |
    |            `param.field` moved due to this method call
    |            move occurs because `param.field` has type `Option<SomethingThatDoesntImplCopy>`, which does not implement the `Copy` trait
    |
note: `branch` takes ownership of the receiver `self`, which moves `param.field`
   --> /home/noobtracker/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/try_trait.rs:218:15
    |
218 |     fn branch(self) -> ControlFlow<Self::Residual, Self::Output>;
    |               ^^^^
help: you can `clone` the value and consume it, but this might not be your desired behavior
    |
13  |     field: clone().param.field?
    |            ++++++++

For more information about this error, try `rustc --explain E0507`.

Desired output

error[E0507]: cannot move out of `param.field` which is behind a shared reference
   --> src/main.rs:13:12
    |
13  |     field: param.field?
    |            ^^^^^^^^^^^-
    |            |
    |            `param.field` moved due to this method call
    |            move occurs because `param.field` has type `Option<SomethingThatDoesntImplCopy>`, which does not implement the `Copy` trait
    |
note: `branch` takes ownership of the receiver `self`, which moves `param.field`
   --> /home/noobtracker/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/try_trait.rs:218:15
    |
218 |     fn branch(self) -> ControlFlow<Self::Residual, Self::Output>;
    |               ^^^^
help: you can `clone` the value and consume it, but this might not be your desired behavior
    |
13  |     field: param.field.clone()?
    |                       ++++++++

For more information about this error, try `rustc --explain E0507`.

Rationale and extra context

Personally I would prefer it if the error always suggested adding .clone() instead of clone(). but in this case it is not a matter of preference, rustc makes a bad recommendation.

Other cases

No response

Anything else?

Was tested in the latest stable, nightly and beta.

@NoobTracker NoobTracker 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 Jul 5, 2023
@compiler-errors
Copy link
Member

Partly a duplicate of #112939, which I have a PR that's up to fix, but I just gotta rebase it and land it 😅 thanks for the error report!

@compiler-errors compiler-errors closed this as not planned Won't fix, can't repro, duplicate, stale Jul 5, 2023
# for free to join this conversation on GitHub. Already have an account? # 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