Skip to content
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

Merged
merged 9 commits into from
Jan 25, 2021

Conversation

Manishearth
Copy link
Member

@Manishearth Manishearth commented Jan 21, 2021

Fixes #6616

changelog: Added restriction lint: exhaustive_enums, exhaustive_structs

@rust-highfive
Copy link

r? @llogiq

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jan 21, 2021
@sffc
Copy link

sffc commented Jan 21, 2021

Thanks @Manishearth!

Can we limit the lint to only public enums?

And also structs?

@Manishearth
Copy link
Member Author

Yep. I'd make it two separate lints, since people match on structs less.

@Manishearth Manishearth changed the title New lint: exhaustive_enums New lint: exhaustive_enums, exhaustive_structs Jan 21, 2021
@Manishearth
Copy link
Member Author

@sffc updated!

bar: String,
}

// no warning, private
Copy link

@sffc sffc Jan 21, 2021

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.

Copy link
Member Author

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

@Manishearth
Copy link
Member Author

cc @camsteffen wanna review this one?

Copy link
Contributor

@camsteffen camsteffen left a 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.

if !item.attrs.iter().any(|a| a.has_name(sym::non_exhaustive));
then {
let lint = if let ItemKind::Enum(..) = item.kind {
EXHAUSTIVE_STRUCTS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
EXHAUSTIVE_STRUCTS
EXHAUSTIVE_ENUMS

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops

EXHAUSTIVE_STRUCTS
};

if let Some(snippet) = snippet_opt(cx, item.span) {
Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Contributor

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!

Copy link
Member Author

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

Copy link
Member Author

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

cx,
lint,
item.span,
"enums should not be exhaustive",
Copy link
Contributor

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

Copy link
Contributor

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"

Copy link
Member Author

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!

@Manishearth
Copy link
Member Author

@bors delegate+

@bors
Copy link
Contributor

bors commented Jan 22, 2021

✌️ @Manishearth can now approve this pull request

@Manishearth
Copy link
Member Author

@bors delegate=camsteffen

oops

@bors
Copy link
Contributor

bors commented Jan 22, 2021

✌️ @camsteffen can now approve this pull request

@llogiq
Copy link
Contributor

llogiq commented Jan 22, 2021

This looks good, but as @camsteffen has already done most of the review, I'll leave the r+ to him. 😄

@camsteffen
Copy link
Contributor

Nice!

@bors r+

@bors
Copy link
Contributor

bors commented Jan 25, 2021

📌 Commit 3e3dff7 has been approved by camsteffen

@bors
Copy link
Contributor

bors commented Jan 25, 2021

⌛ Testing commit 3e3dff7 with merge 8d57cee...

@bors
Copy link
Contributor

bors commented Jan 25, 2021

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: camsteffen
Pushing 8d57cee to master...

@bors bors merged commit 8d57cee into rust-lang:master Jan 25, 2021
@Manishearth Manishearth deleted the exhaustive_enums branch January 25, 2021 23:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New restriction lint: exhaustive_enums
6 participants