Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Enum unit variant changed kind #894

Merged

Conversation

PedroTurik
Copy link
Contributor

solves one lint from #884

Applied the same logic from enum_tuple_variant_field_added and similar lints for whether or not newly doc(hidden) and newly non_exhaustive variants should report for both.

Also, not sure about the reference link. Maybe https://doc.rust-lang.org/reference/attributes/type_system.html#the-non_exhaustive-attribute would be better?

@PedroTurik PedroTurik marked this pull request as ready for review August 29, 2024 18:45
Copy link
Owner

@obi1kenobi obi1kenobi left a comment

Choose a reason for hiding this comment

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

Nicely done!

I have a slightly different opinion on the edge cases where this lint does/doesn't fire. It would be great to make a quick adjustment there and add some more test cases to pin down the behavior.

In general, when we have non-obvious decisions, it'd be great to document them with a detailed comment inline, because in 6 months neither of us will remember the details of why the decision was made. So if you ever feel like a piece of code in your PR would be more obvious if you explained it with a comment on GitHub, write that comment into the file itself to make it easier to find!

Comment on lines 46 to 47
attrs @filter(op: "not_contains", value: ["$non_exhaustive"])
public_api_eligible @filter(op: "=", value: ["$true"])
Copy link
Owner

Choose a reason for hiding this comment

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

This is definitely a judgment call, but I think enum_tuple_variant_field_added operates under slightly different constraints: the variant existed previously and continues to exist, albeit with extra stuff added. In this lint, we don't have similar continuity in the variant — we have what is essentially a completely new variant which just happens to have the same name as the old one.

I could maybe be convinced about not linting if the variant became #[doc(hidden)]. But adding #[non_exhaustive] in the new version should definitely not prevent this lint, since "the struct isn't constructible" and "also the variant is a different kind" feel very orthogonal.

Copy link
Owner

Choose a reason for hiding this comment

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

Side note: it would be good to have a detailed comment similar to that other lint that lays out the reasoning for why these checks are included or omitted. They are non-obvious and depend on a judgment call, so it's good to be able to remind ourselves in the future why a certain decision made sense.

"true": true,
"non_exhaustive": "#[non_exhaustive]",
},
error_message: "A public enum's exhaustive unit variant has changed kind, breaking possible instantiations and patterns.",
Copy link
Owner

Choose a reason for hiding this comment

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

Using "kind" for variant kinds isn't entirely standard, commonly-used Rust terminology. This line appears only once regardless of how many instances of breakage the lint finds, so let's use a slightly longer description in the interest of clarity.

Suggested change
error_message: "A public enum's exhaustive unit variant has changed kind, breaking possible instantiations and patterns.",
error_message: "A public enum's exhaustive unit variant has changed to a different kind of enum variant, breaking possible instantiations and patterns.",

description: "A public enum unit variant that isn't #[non_exhaustive] changed kind",
required_update: Major,
lint_level: Deny,
reference_link: Some("https://doc.rust-lang.org/cargo/reference/semver.html#enum-variant-new"),
Copy link
Owner

Choose a reason for hiding this comment

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

I'm surprised that it really seems like there isn't a great reference link. I couldn't find anything that covers enum variants in detail. The focus here should be on enum variants, not #[non_exhaustive], so let's go with this as the "best we could find":

Suggested change
reference_link: Some("https://doc.rust-lang.org/cargo/reference/semver.html#enum-variant-new"),
// TODO: If the Rust reference gains a more detailed explanation of enum *variants*,
// switch the explanation to point there instead. The current link isn't great.
reference_link: Some("https://doc.rust-lang.org/reference/items/enumerations.html"),

Copy link
Owner

@obi1kenobi obi1kenobi left a comment

Choose a reason for hiding this comment

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

Awesome!

src/lints/enum_unit_variant_changed_kind.ron Outdated Show resolved Hide resolved
@obi1kenobi obi1kenobi enabled auto-merge (squash) September 2, 2024 18:24
@obi1kenobi obi1kenobi merged commit 924003e into obi1kenobi:main Sep 2, 2024
32 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants