Skip to content

Matching on a place in borrowed context should no longer suggest ref #52423

Closed
@scottmcm

Description

@scottmcm

https://play.rust-lang.org/?gist=a88b996b2d1c86d1f6d32eee66c70cd4&version=stable&mode=debug&edition=2015

struct MyStruct<T> {
    x: Option<T>,
}

fn foo<T>(s: &MyStruct<T>) {
    match s.x {
        Some(_a) => {}
        None => {}
    }
}

This suggests using ref:

error[E0507]: cannot move out of borrowed content
 --> src/lib.rs:8:11
  |
8 |     match s.x {
  |           ^ cannot move out of borrowed content
9 |         Some(_a) => {}
  |              -- hint: to prevent move, use `ref _a` or `ref mut _a`

With match ergonomics now stable, it would be better to instead suggest borrowing the field:

error[E0507]: cannot move out of borrowed content
 --> src/lib.rs:8:11
  |
8 |     match s.x {
  |           ^ cannot move out of borrowed content
  |           - hint: to prevent move, use `&s.x` or `&mut s.x`

To help people move away from needing to know that ref exists.

(Context that lead me to open this: https://discordapp.com/channels/273534239310479360/274215136414400513/468303728986816512)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions