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

Don't suggest doc(hidden) or unstable variants in wildcard lint #7407

Merged
merged 4 commits into from
Jul 1, 2021

Conversation

m-ou-se
Copy link
Member

@m-ou-se m-ou-se commented Jun 26, 2021

Clippy's wildcard lint would suggest doc(hidden) and unstable variants for non_exhaustive enums, even though those aren't part of the public interface (yet) and should only be matched on using a _, just like potential future additions to the enum. There was already some logic to exclude a single doc(hidden) variant. This extends that to all hidden variants, and also hides #[unstable] variants.

See rust-lang/rust#85746 (comment)

This PR includes #7406 as the first commit.

Here's the diff that this PR adds on top of that PR: m-ou-se/fork@std-errorkind...m-ou-se:doc-hidden-variants


Please write a short comment explaining your change (or "none" for internal only changes)

changelog: No longer suggest unstable and doc(hidden) variants in wildcard lint. wildcard_enum_match_arm, match_wildcard_for_single_variants

Every time we add something to this enum in std, this test breaks.
@rust-highfive
Copy link

r? @phansch

(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 Jun 26, 2021
Copy link
Member

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

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

Could you update the changelog message to mention the specific lints that are effected by this change? That makes writing the change log easier in the end. In this case, it should be the lint wildcard_enum_match_arm

The rest looks good to me.


Hey @flip1995, I would take over the review of this PR if that works for you :)

@@ -1033,7 +1033,7 @@ fn check_wild_enum_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>])

// Accumulate the variants which should be put in place of the wildcard because they're not
// already covered.
let mut missing_variants: Vec<_> = adt_def.variants.iter().collect();
let mut missing_variants: Vec<_> = adt_def.variants.iter().filter(|x| !is_hidden(cx, x)).collect();
Copy link
Member

Choose a reason for hiding this comment

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

I think here it would be good to differentiate between the enums origin. It would make sense to me to also cover hidden/unstable variations if they originate from the same crate. (I couldn't find a check for that in the current implementation)

I'm also debating if that should be configurable, but I believe there is no real reason for that.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think this has to be done in this PR. I like this idea though. Can you open a good-first-issue issue for this, please?

Copy link
Member

Choose a reason for hiding this comment

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

cc: #7419

clippy_lints/src/matches.rs Outdated Show resolved Hide resolved
@flip1995 flip1995 assigned flip1995 and unassigned phansch Jun 29, 2021
@flip1995
Copy link
Member

(Un-assigning from phansch, since he's taking a break, thanks for taking over the review @xFrednet!)

@xFrednet
Copy link
Member

@rustbot label -S-waiting-on-review +S-waiting-on-author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jun 30, 2021
This fixes a bug where match_wildcard_for_single_variants produced a
bad suggestion where besides the missing variant, one or more hidden
variants were left.

This also adds tests to the ui-tests match_wildcard_for_single_variants
and wildcard_enum_match_arm to make sure that the correct suggestion is
produced.
@flip1995
Copy link
Member

flip1995 commented Jul 1, 2021

@xFrednet can you review my changes, please? If any changes should be required I'll do them in a follow-up PR, I want to get this merged, so I can sync Clippy to Rust.

@bors r+

@flip1995

This comment has been minimized.

@bors

This comment has been minimized.

@bors

This comment has been minimized.

@flip1995

This comment has been minimized.

@flip1995

This comment has been minimized.

@flip1995

This comment has been minimized.

@bors

This comment has been minimized.

@bors

This comment has been minimized.

bors added a commit that referenced this pull request Jul 1, 2021
Don't suggest doc(hidden) or unstable variants in wildcard lint

Clippy's wildcard lint would suggest doc(hidden) and unstable variants for non_exhaustive enums, even though those aren't part of the public interface (yet) and should only be matched on using a `_`, just like potential future additions to the enum. There was already some logic to exclude a *single* doc(hidden) variant. This extends that to all hidden variants, and also hides `#[unstable]` variants.

See rust-lang/rust#85746 (comment)

This PR includes #7406 as the first commit.

Here's the diff that this PR adds on top of that PR: m-ou-se/fork@std-errorkind...m-ou-se:doc-hidden-variants

---

*Please write a short comment explaining your change (or "none" for internal only changes)*

changelog:
 - No longer suggest unstable and doc(hidden) variants in wildcard lint.
@bors

This comment has been minimized.

@flip1995
Copy link
Member

flip1995 commented Jul 1, 2021

@bors retry

@bors
Copy link
Collaborator

bors commented Jul 1, 2021

⌛ Testing commit fae7a09 with merge 753bce3...

@bors
Copy link
Collaborator

bors commented Jul 1, 2021

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: flip1995
Pushing 753bce3 to master...

Copy link
Member

@xFrednet xFrednet left a comment

Choose a reason for hiding this comment

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

The rest looks good to me 👍

--> $DIR/wildcard_enum_match_arm.rs:101:13
|
LL | _ => (),
| ^ help: try this: `Enum::B | _`
Copy link
Member

Choose a reason for hiding this comment

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

I find it confusing that the lint suggests here to use the enum value in the same arm as the wildcard. That would mean that future additions would also run into this branch.

It would be good to either add a note to explain why a wildcard has to be present here or to make the suggestion to have two arms like this:

Enum::B => {},
_ => { /* Hidden values */ }

Adding a note that the enum includes hidden, or unstable values would still be good.

Copy link
Member

Choose a reason for hiding this comment

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

A X | _ pattern is already caught by another lint, so this shouldn't be an issue.

@m-ou-se m-ou-se deleted the doc-hidden-variants branch July 2, 2021 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants