Add new matches_instead_of_eq lint#17018
Conversation
d8ab39c to
97608f8
Compare
|
Lintcheck changes for b5f52cd
This comment will be updated if you push new changes |
97608f8 to
36f7572
Compare
|
With dogfood errors, I was able to strenghten quite a lot the lint and handle suggestions with dereferencing. One of the nice changes that came with this lint is: - if cx
- .tcx
- .crate_types()
- .iter()
- .any(|t: &CrateType| matches!(t, CrateType::ProcMacro))
- {
+ if cx.tcx.crate_types().contains(&CrateType::ProcMacro) { |
|
Maybe a nit: If the type being compared is |
|
Very good point. I'll handle this case as well, thanks! |
302fceb to
b7af90a
Compare
b7af90a to
b5f52cd
Compare
|
Added suggested change. |
|
|
||
| declare_clippy_lint! { | ||
| /// ### What it does | ||
| /// Checks for the use of `matches!` with types that implement `PartialEq` (not manually). |
There was a problem hiding this comment.
| /// Checks for the use of `matches!` with types that implement `PartialEq` (not manually). | |
| /// Checks for the use of `matches!` with types that automatically derive `PartialEq`. |
| } | ||
|
|
||
| cx.tcx.for_each_relevant_impl(partial_eq_def_id, expr_ty, |impl_id| { | ||
| if !cx.tcx.is_automatically_derived(impl_id) { |
There was a problem hiding this comment.
I think this won't catch types whose components have a non-automatically-derived PartialEq, for example here:
enum E { … }
impl PartialEq for E {
…
}
#[derive(PartialEq)]
struct S(E);
fn f(s: &S) {
if matches!(s, S(E::…)) { … }
}In this case, we have multiple choices:
- not lint
- lint with
MaybeIncorrectand a note saying thatE'sPartialEqmay not behave as pattern matching
While in other cases, I think we can lint with MachineApplicable.
There was a problem hiding this comment.
I think we shouldn't link in this case to follow the "do not lint if manual PartialEq impl" rule.
|
The check will have to be carefully placed as to not analyze all types
recursively every time there is a `matches!` macro call. You might also
want to cache the analysis results.
|
|
☔ The latest upstream changes (possibly #17053) made this pull request unmergeable. Please resolve the merge conflicts. |
Fixes #14688.
r? @samueltardieu
changelog: Add new pedantic
matches_instead_of_eqlint