-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
New lint: exhaustive_enums, exhaustive_structs #6617
Conversation
r? @llogiq (rust-highfive has picked a reviewer for you, use r? to override) |
ffe5570
to
558cdcb
Compare
Thanks @Manishearth! Can we limit the lint to only public enums? And also structs? |
Yep. I'd make it two separate lints, since people match on structs less. |
558cdcb
to
09d4d49
Compare
@sffc updated! |
1e7a67c
to
5bd681b
Compare
bar: String, | ||
} | ||
|
||
// no warning, private |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also make sure that pub(crate)
structs and enums don't require #[non_exhaustive]
?
If there's a way to exclude pub
structs and enums that are inside a non-pub
module, that would be nice, too. But at least we should exclude pub(crate)
.
Basically, the check should only complain about truly public types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's already how this is implemented, it's for exported types only
5bd681b
to
4736986
Compare
cc @camsteffen wanna review this one? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure! A couple minor things.
clippy_lints/src/exhaustive_items.rs
Outdated
if !item.attrs.iter().any(|a| a.has_name(sym::non_exhaustive)); | ||
then { | ||
let lint = if let ItemKind::Enum(..) = item.kind { | ||
EXHAUSTIVE_STRUCTS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EXHAUSTIVE_STRUCTS | |
EXHAUSTIVE_ENUMS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops
clippy_lints/src/exhaustive_items.rs
Outdated
EXHAUSTIVE_STRUCTS | ||
}; | ||
|
||
if let Some(snippet) = snippet_opt(cx, item.span) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using span_lint_and_then
and span_suggestion(item.span.shrink_to_lo(), ..)
so that you don't need to include the snippet in the suggestion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm you didn't do the shrink_to_lo()
part... the point of using span_lint_and_then
was to use a different Span
for the suggestion. The suggestion would be an insertion rather than replacing the entire item. I'm not entirely certain this is possible but it would be cleaner!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh. I'm not sure that works so well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will try, r=you if it doesn't?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes
clippy_lints/src/exhaustive_items.rs
Outdated
cx, | ||
lint, | ||
item.span, | ||
"enums should not be exhaustive", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"enums" should be a variable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple nits. The message should be about "this enum" instead of "all enums". I think it would be good to mention that the item is "public" or "exported". So maybe: "declared a public, exhaustive enum"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I picked "Exported foo should not be exhaustive". Lmk if you prefer the other way!
4736986
to
65d003a
Compare
@bors delegate+ |
✌️ @Manishearth can now approve this pull request |
@bors delegate=camsteffen oops |
✌️ @camsteffen can now approve this pull request |
This looks good, but as @camsteffen has already done most of the review, I'll leave the r+ to him. 😄 |
Nice! @bors r+ |
📌 Commit 3e3dff7 has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
Fixes #6616
changelog: Added restriction lint:
exhaustive_enums
,exhaustive_structs