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: match_wildcard_for_single_variants #5582

Merged
merged 8 commits into from
May 20, 2020

Conversation

vtmargaryan
Copy link
Contributor

@vtmargaryan vtmargaryan commented May 10, 2020

changelog: Added a new lint match_wildcard_for_single_variants to warn on enum matches where a wildcard is used to match a single variant

Closes #5556

@flip1995 flip1995 requested a review from phansch May 11, 2020 17:57
@@ -509,7 +509,7 @@ fn is_allowed<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) -> boo
Constant::F64(f) => *f == 0.0 || (*f).is_infinite(),
_ => false,
}),
_ => false,
Some(_) | None => false,
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't lint here, since Some(_) and None is left.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Current implementation only checks if a variant exists and doesn't check if it's exsaustive. I guess it was ok for wildcard_enum_match_arm, but it's not ok in case of the current lint. I'll try to implement it.
I don't know if there can be cases when it's not possible to check for exuastiveness, but if there is, what should I do then? Should I just skip that match?

I'm sorry, I guess I should have opened a WIP PR

Copy link
Contributor Author

@vtmargaryan vtmargaryan May 13, 2020

Choose a reason for hiding this comment

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

We could also implement the same logic as with skipping arm guards.
If a variant is appearing only in patterns with arm guards, then it is treated as missing.
https://github.com/rust-lang/rust-clippy/blob/master/clippy_lints/src/matches.rs#L722

if arm.guard.is_some() {
    // Guards mean that this case probably isn't exhaustively covered. Technically
    // this is incorrect, as we should really check whether each variant is exhaustively
    // covered by the set of guards that cover it, but that's really hard to do.
    continue;
}

We could check only for simple case Variant(wild), and if it doesn't match then don't count Variant as present.

Which way do you want to go?

Copy link
Member

Choose a reason for hiding this comment

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

I'm sorry, I guess I should have opened a WIP PR

Not a problem, doesn't really matter review wise.

Which way do you want to go?

The way you think is the easiest way to implement this. I see you put more thought into this, than I have, so I trust your judgement here.

In the end it should only suggest to replace it with the remaining variant, if we can be absolutely sure that there is only one left.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've fixed it. Currently variants with TupleStruct patterns are treated as missing unless they pass checks for exhaustiveness. The checks are simple. They check for wild and binding patterns.

clippy_lints/src/matches.rs Show resolved Hide resolved
@vtmargaryan vtmargaryan force-pushed the match_wildcard_for_single_variants branch from aae97ec to 1c59cd5 Compare May 14, 2020 19:42
@flip1995 flip1995 added the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label May 15, 2020
@vtmargaryan
Copy link
Contributor Author

There is a label S-waiting-on-author on this PR. I just wanted to clarify that I've applied all suggestions.
What should I usually do after applying suggestions if there is such label on the PR? :)

@ebroto
Copy link
Member

ebroto commented May 18, 2020

@vtmargaryan it will be re-reviewed eventually :) AFAIK you can't change the tags, but there are other ways the maintainers can realize that changes were done to the PR

@vtmargaryan
Copy link
Contributor Author

@ebroto, thanks, I just didn't know if I should do something else or is it enough to push the commits :) Maybe it's worth adding to contribution guide

@vtmargaryan vtmargaryan requested a review from flip1995 May 19, 2020 17:27
@flip1995
Copy link
Member

Nice solution for the exhaustive check!

currently waiting for #5621

@flip1995 flip1995 added S-waiting-on-bors Status: The marked PR was approved and is only waiting bors and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels May 20, 2020
@flip1995 flip1995 removed the request for review from phansch May 20, 2020 11:57
@flip1995
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented May 20, 2020

📌 Commit d906253 has been approved by flip1995

@bors
Copy link
Contributor

bors commented May 20, 2020

⌛ Testing commit d906253 with merge cafa946...

@bors
Copy link
Contributor

bors commented May 20, 2020

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

@bors bors merged commit cafa946 into rust-lang:master May 20, 2020
@vtmargaryan vtmargaryan deleted the match_wildcard_for_single_variants branch May 20, 2020 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: The marked PR was approved and is only waiting bors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New lint: suggest using the only remaining variant instead of a wildcard
4 participants