Skip to content

Invalid suggestion to consider borrowing Option's content in tuple #82528

Closed
@camsteffen

Description

@camsteffen

Given the following code: (playground)

fn foo(a: &Option<String>, b: &Option<String>) {
    match (a, b) {
        (None, &c) | (&c, None) => &c.unwrap(),
        (&Some(ref c), _) => c,
    };
}

The current output is:

error[E0507]: cannot move out of a shared reference
 --> src/lib.rs:2:11
  |
2 |     match (a, b) {
  |           ^^^^^^ help: consider borrowing the `Option`'s content: `(a, b).as_ref()`
3 |         (None, &c) | (&c, None) => &c.unwrap(),
  |                 -
  |                 |
  |                 data moved here
  |                 move occurs because `c` has type `Option<String>`, which does not implement the `Copy` trait

The suggestion does not compile. Presumably it should suggest (a.as_ref(), b.as_ref()).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-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