Skip to content

Invalid suggestion: consider creating fresh reborrow in pattern #80913

Closed
@meithecatte

Description

@meithecatte
fn test() {
    let mut x = 42_i32;
    let opt = Some(&mut x);
    loop {
        if let Some(_x) = opt {}
    }
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0382]: use of moved value
 --> src/lib.rs:5:21
  |
5 |         if let Some(_x) = opt {}
  |                     ^^ value moved here, in previous iteration of loop
  |
  = note: move occurs because value has type `&mut i32`, which does not implement the `Copy` trait
help: borrow this field in the pattern to avoid moving `opt.0`
  |
5 |         if let Some(ref _x) = opt {}
  |                     ^^^
help: consider creating a fresh reborrow of `opt.0` here
  |
5 |         if let Some(&mut *_x) = opt {}
  |                     ^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0382`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

if let Some(&mut *_x) = opt {} is not valid syntax.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-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