Skip to content

Disallow non-exhaustive matches #3649

Closed
@Aehmlo

Description

@Aehmlo

I'm not sure if this is better suited as a language feature or a clippy lint (though I assume clippy is an easier route to pursue), but it would be really nice to be able to either mark an enum (lang?) or a region of code (clippy?) as disallowing non-exhaustive matches. I know this might seem weird and/or bad, but I have a project wherein I want to make sure that all possible variants are covered, no wildcards, and I would like to have clippy be able to take care of checking for this on PRs instead of having to check it myself (clippy's way less likely to miss it than I am!).

Let's say my library has an internal enum giving movement types, and a function that performs an action based on this.

enum Movement {
    Fly,
    Run,
}
// Somewhere else
fn respond(movement: Movement) {
    match movement {
        Movement::Fly => fly(),
        // Disallow _ as a pattern
        Movement::Run => run(),
    };
}

Then, if I (or someone else) later add(s) an enum variant, I can be sure, if the tests pass, that the new movement type is being appropriately handled (without the wildcard pattern as a crutch).

enum Movement {
    Dig,
    Fly,
    Run,
}
// Somewhere else
fn respond(movement: Movement) {
    match movement {
        Movement::Fly => fly(),
        Movement::Run => run(),
        // Since we disallowed the wildcard _, we know Dig is being handled without looking
        Movement::Dig => dig(),
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lintsgood first issueThese issues are a good way to get started with Clippy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions