Skip to content

Warn when a match can be made more specific #75235

Closed as not planned
Closed as not planned
@jyn514

Description

@jyn514

Consider the following code (playground):

enum E {
    A(usize),
    B(usize),
}

impl E {
    fn as_usize(&self) -> usize {
        match *self {
            E::A(a) => a,
            E::B(b) => b,
        }
    }
}

fn main() {
    let _ = E::B(1); // ignore warn(dead_code) on the B variant
    let e = E::A(1);
    match e {
        E::A(x) => x,
        x => x.as_usize(),
    };
}

This code gives no warnings. However, the as_usize() method is entirely useless, because the match can be rewritten to be more specific:

    match e {
        E::A(x) => x,
        E::B(x) => x,
    };

The compiler should give a warning in such a case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-exhaustiveness-checkingRelating to exhaustiveness / usefulness checking of patternsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.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