Skip to content

Nightly Rust suggests .as_ref() on the left-hand side of the assignment #114925

Closed
@GoldsteinE

Description

@GoldsteinE

Code

struct Lol {
    x: Option<String>,
}

fn lmao(lol: &mut Lol, x: Option<&String>) {
    lol.x = x.clone();
}

Current output

error[E0308]: mismatched types
 --> src/lib.rs:6:13
  |
6 |     lol.x = x.clone();
  |     -----   ^^^^^^^^^ expected `Option<String>`, found `Option<&String>`
  |     |
  |     expected due to the type of this binding
  |
  = note: expected enum `Option<String>`
             found enum `Option<&String>`
help: use `Option::as_ref` to convert `Option<String>` to `Option<&String>`
  |
6 |     lol.x.as_ref() = x.clone();
  |          +++++++++

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

Desired output

error[E0308]: mismatched types
 --> src/lib.rs:6:13
  |
6 |     lol.x = x.clone();
  |     -----   ^^^^^^^^^ expected `Option<String>`, found `Option<&String>`
  |     |
  |     expected due to the type of this binding
  |
  = note: expected enum `Option<String>`
             found enum `Option<&String>`
help: use `Option::cloned` to clone the value inside the `Option`
  |
6 |     lol.x = x.clone().cloned();
  |                      +++++++++

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

Rationale and extra context

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

Anything else?

That’s a regression from stable to nightly. Playground’s stable (1.71.1) gives the desired output, but playground’s nightly (1.73.0-nightly (2023-08-16 07438b0)) does not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions