Skip to content

Add a lint warning on bare PatIdent arms #19824

Closed
@Manishearth

Description

@Manishearth

At the moment, if an enum variant changes name, match arms containing it silently become wildcard matches. For example, the following:

enum Foo {
 Bar,
 Baz,
 Quux
}
match f {
 Bar => ...,
 Baz => ...
 Quux => ...
}

will silently have the second arm becoming a wildcard arm if the original enum is changed (instead of a compile error which usually is how you can track down uses of a renamed identifier). In some cases we may get an "unreachable pattern" error, but not always.

I'm proposing we add a lint to warn against arms which are a single identifier bound to a variable instead of a variant. Instead, we suggest that _ @ variable arms are used.

This doesn't handle patterns where enums are nested, i.e Some(Bar) when Bar gets renamed. If we want to catch these too we might want to instead lint uppercase PatIdents anywhere in a match where the corresponding type is an enum.

It's pretty easy to write these lints, but I'm not sure if Rust is currently accepting more internal plugins or if this lint is something we really want, hence this issue first.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions