Skip to content

Lint to ensure all variants of an enum are used #9255

Open
@Nemo157

Description

@Nemo157

What it does

Check that all variants of an enum are used inside a function.

This is similar to standard exhaustiveness checking of a match, and the non-exhaustive-omitted-patterns lint for non-exhaustive enums; but generalized to usecases that are not using match.

Lint Name

No response

Category

restriction

Advantage

Ensures that when new variants are added to an enum (whether because it's an internal enum, public enum with a major version bump, or an external non-exhaustive enum) those variants are handled.

A major usecase I see for this is things that produce values of the enum and are expected to be able to produce every variant, such as parsers.

Drawbacks

How do you determine which enum to lint on?

Example

enum Intensity { Normal, Bold, Faint }
struct Unknown;
impl FromStr for Intensity {
    type Err = Unknown;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "normal" => Ok(Intensity::Normal),
            "bold" => Ok(Intensity::Bold),
            _ => Err(Unknown),
        }
    }
}

Could be written as:

enum Intensity { Normal, Bold, Faint }
struct Unknown;
impl FromStr for Intensity {
    type Err = Unknown;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "normal" => Ok(Intensity::Normal),
            "bold" => Ok(Intensity::Bold),
            "faint" => Ok(Intensity::Faint),
            _ => Err(Unknown),
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions