Skip to content

Add an allow-by-default lint that triggers on reachable catch-all / rest patterns #84332

Closed
@jplatte

Description

@jplatte

syn currently allows users to opt into having their tests fail if a match of one of the enums it defines is non-exhaustive:

match expr {
    Expr::Array(e) => {...}
    Expr::Assign(e) => {...}
    ...
    Expr::Yield(e) => {...}

    #[cfg(test)]
    Expr::__TestExhaustive(_) => unimplemented!(),
    #[cfg(not(test))]
    _ => { /* some sane fallback */ }
}

In the tracking issue for the #[non_exhaustive] attribute, it was discussed to allow this in a less hacky way, for truly non-exhaustive enums, with dtolnay suggesting a lint (#44109 (comment)) like this:

match expr {
    Expr::Array(e) => {...}
    Expr::Assign(e) => {...}
    ...
    Expr::Yield(e) => {...}

    #[cfg_attr(test, deny(reachable))]
    _ => { /* some sane fallback */ }
}

Later, a clippy issue was opened about the same thing and a PR opened to address it. However, that PR was closed because

the thing is that rustc already has exhaustiveness checks, and this is a lint that was proposed in a Rust RFC, so it would both be easy to implement in rustc and make sense. It feels weird to reimplement exhaustiveness checking in a partial way outside of rustc when we can have a perfect version of this lint in rustc.

rust-lang/rust-clippy#6328 (comment)

Metadata

Metadata

Assignees

Labels

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