Skip to content

Incorrect help note for E0507 cannot move out of ... which is behind a shared reference with try operator #111016

Closed
@Veykril

Description

@Veykril

Code

#[derive(Clone)]
struct Cloneable;
fn f() -> Result<(), Cloneable> {
    let thing = &(Ok::<(), Cloneable>(()),);
    thing.0?;
    Ok(())
}

playground link

Current output

error[E0507]: cannot move out of `thing.0` which is behind a shared reference
 --> src/lib.rs:5:5
  |
5 |     thing.0?;
  |     ^^^^^^^-
  |     |
  |     `thing.0` moved due to this method call
  |     move occurs because `thing.0` has type `Result<(), Cloneable>`, which does not implement the `Copy` trait
  |
note: `branch` takes ownership of the receiver `self`, which moves `thing.0`
 --> /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/ops/try_trait.rs:218:15
help: you can `clone` the value and consume it, but this might not be your desired behavior
  |
5 |     clone().thing.0?;
  |     ++++++++

For more information about this error, try `rustc --explain E0507`.
error: could not compile `playground` due to previous error

Desired output

error[E0507]: cannot move out of `thing.0` which is behind a shared reference
 --> src/lib.rs:5:5
  |
5 |     thing.0?;
  |     ^^^^^^^-
  |     |
  |     `thing.0` moved due to this method call
  |     move occurs because `thing.0` has type `Result<(), Cloneable>`, which does not implement the `Copy` trait
  |
note: `branch` takes ownership of the receiver `self`, which moves `thing.0`
 --> /rustc/84c898d65adf2f39a5a98507f1fe0ce10a2b8dbc/library/core/src/ops/try_trait.rs:218:15
help: you can `clone` the value and consume it, but this might not be your desired behavior
  |
5 |     thing.0.clone()?;
  |            ++++++++

For more information about this error, try `rustc --explain E0507`.
error: could not compile `playground` due to previous error

Rationale and extra context

The .clone() call is being prepended to the expression instead of appended

Other cases

No response

Anything else?

No response

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions