rust-lang / rust-lang/rust-clippy
Lint match statement matching #[non_exhaustive] enum
Open
@matthri is already working on this.
Since Nov 28, 2023.
A-lint
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
If a downstream crate/app matches on an enum with non_exhaustive attribute set, rustc doesn't fail when new field is added to that enum. Therefore, with each dependency update, the existence of new fields must be checked manually. Otherwise they're routed the default way.
This lint would warn if there's a field in an enum marked as #[non_exhaustive] that isn't handled by a match statement.
The lint is optional.
Advantage
- Saves checking codebases manually on crate updates - error prone.
- Makes it easy to spot new errors they potentially want to handle, instead of handling them the "default way".
Drawbacks
- Some code owners may prefer not to be warned. Must be either implemented as opt-in, or must be able to opt-out.
Example
Old code
#[non_exhaustive]
enum Error {
ValidationError,
ResponseError, // Newly added field
}
let error: Error = ... // obtain the error here;
match error { // Warning is trigger for this match statement
ValidationError => {...},
_ {
println!("This is the default path, newly added ResponseError is silently sent this way")
}
}
Improved match statement
let error: Error = ... // obtain the error here;
match error {
ValidationError => {...},
ResponseError => {...}, // The new case is handled, clippy no longer warns
_ {
println!("This is the default path")
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.