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

Invalid suggestion from E0507 with & and ? #113547

Closed
sciguy16 opened this issue Jul 10, 2023 · 0 comments · Fixed by #112945
Closed

Invalid suggestion from E0507 with & and ? #113547

sciguy16 opened this issue Jul 10, 2023 · 0 comments · Fixed by #112945
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

@sciguy16
Copy link

Code

struct A {
    member: Option<String>,
}

fn main() {
    let a = A{ member: None};

    function(&a);
}

fn function(a: &A) -> Option<()> {
    let member = &a.member?;
    None
}

Current output

error[E0507]: cannot move out of `a.member` which is behind a shared reference
  --> src/main.rs:12:19
   |
12 |     let member = &a.member?;
   |                   ^^^^^^^^-
   |                   |
   |                   `a.member` moved due to this method call
   |                   move occurs because `a.member` has type `Option<String>`, which does not implement the `Copy` trait
   |
note: `branch` takes ownership of the receiver `self`, which moves `a.member`
  --> /rustc/1065d876cdbc34a872b9e17c78caaa59ea0c94d4/library/core/src/ops/try_trait.rs:217:15
help: you can `clone` the value and consume it, but this might not be your desired behavior
   |
12 |     let member = &clone().a.member?;
   |                   ++++++++

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

Desired output

12 |     let member = &a.member.clone()?;

or

12 |     let member = a.member.as_ref()?;

Rationale and extra context

The suggestion for cloning the member that cannot be moved puts the clone() in the wrong place, but only (in this example) when combined with the question mark operator. Without the question mark &a.member compiles with no errors.

Playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=0a72b96edb6cf4f2fcab3e2cbf4c877e

Other cases

No response

Anything else?

No response

@sciguy16 sciguy16 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 10, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 10, 2023
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 10, 2023
@bors bors closed this as completed in da1d099 Jul 12, 2023
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

Successfully merging a pull request may close this issue.

3 participants