Skip to content

emit lint when using ref with match ergonomics #105773

Description

@lcnr
fn foo(x: &Option<u32>) -> Option<&u32> {
    match x {
        Some(ref inner) => Some(inner),
        None => None,
    }
}

because of match ergonomics, every binding inside of Some is already matched by ref implicitly, so the additional explicit ref is completely redundant. Consider the following example where l and r have the exact same type.

#![feature(type_name_of_val)]
use std::any::type_name_of_val;
fn foo(x: &Option<(u32, u32)>) {
    match x {
        Some((l, ref r)) => assert_eq!(
            type_name_of_val(l),
            type_name_of_val(r),
        ),
        None => unreachable!(),
    }
}

fn main() {
    foo(&Some((3, 3)));
}

This lint should either suggest to disable match ergonomics by adding & and &mut patterns in the relevant places or to remove the ref.

There are a lot of places even inside of rustc which hit this pattern, so this lint feels really desirable to me.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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