-
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: match_wildcard_for_single_variants
#5582
New lint: match_wildcard_for_single_variants
#5582
Conversation
clippy_lints/src/misc.rs
Outdated
@@ -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, |
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.
This shouldn't lint here, since Some(_)
and None
is left.
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.
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
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.
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?
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'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.
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'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.
aae97ec
to
1c59cd5
Compare
…is spanned on Option" This reverts commit 4948307.
TupleStruct matches are checked for exhaustiveness
There is a label S-waiting-on-author on this PR. I just wanted to clarify that I've applied all suggestions. |
@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 |
@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 |
Nice solution for the exhaustive check! currently waiting for #5621 |
@bors r+ |
📌 Commit d906253 has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
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