-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Opt in lint on reachable pattern #81657
Comments
That would be very easy to do. But conceptually it seems weird to lint for that ^^. Is there precedent for a lint like that? |
Is there a reason to cfg attr the deny? I think with cap lints it won't matter non-locally, though I suppose if you have a large workspace you may not be the maintainer of some of the proc macros. Generally speaking, this does seem desirable - providing a localized warning in cases where folks do want to know of upstream expansion feels on topic for non_exhaustive. |
If you make this lint I think I'd like an attribute on the wildcard, like: match expr {
Expr::Array(e) => {...}
Expr::Assign(e) => {...}
...
Expr::Yield(e) => {...}
#[non_exhaustive_fallback] _ => { /* some sane fallback */ }
} That would trigger a warn-by-default lint. The reason I want the attribute on the wildcard is in case the match is nested: match expr {
Foo { x: Expr::Array(e), y: SomethingElse::Foo } => {...}
...
Foo { x: #[non_exhaustive_fallback] _, y: SomethingElse::Foo } => { /* some sane fallback */ }
Foo { x: _, y: _ } => { /* not a fallback */ }
} |
I like the idea of an allow-by-default lint that's specific to hitting the |
Oh yeah I like this. It seems I had misread the initial proposal. |
This should now be closed because of #86809? |
I understand |
Filing to track #44109 (comment). Despite https://github.com/dtolnay/syn being an obvious use case for #[non_exhaustive] because we need to be able to add variants over time as new syntax is added to Rust, the thing that makes #[non_exhaustive] unusable is that there is no opt-in way for downstream to request test breakage when one of their wildcard arms becomes reachable.
Our requirements are:
Item 2 is important for the small fraction of use cases that want to update their code to take into account new syntax, such as a prettyprinter. Item 3 is important so that code downstream of the code that wants to update is never broken in the interim.
Instead of #[non_exhaustive], the idiom we are currently using is to have a hidden variant
__TestExhaustive
and documenting the supported pattern for exhaustive matches as being:This meets requirements 1 and 2, but not 3 because people by and large do not read documentation and will write a dumb exhaustive match that breaks on new variants.
Currently #[non_exhaustive] only meets requirements 1 and 3, but the only thing missing for it to meet 2 is the following lint:
where
reachable
is an allow-by-default lint on catch-all arms of a nonexhaustive match.@Nadrieril
The text was updated successfully, but these errors were encountered: