Skip to content

Add new matches_instead_of_eq lint#17018

Open
GuillaumeGomez wants to merge 4 commits into
rust-lang:masterfrom
GuillaumeGomez:matches_instead_of_eq
Open

Add new matches_instead_of_eq lint#17018
GuillaumeGomez wants to merge 4 commits into
rust-lang:masterfrom
GuillaumeGomez:matches_instead_of_eq

Conversation

@GuillaumeGomez
Copy link
Copy Markdown
Member

@GuillaumeGomez GuillaumeGomez commented May 16, 2026

Fixes #14688.

r? @samueltardieu

changelog: Add new pedantic matches_instead_of_eq lint

@rustbot rustbot added needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels May 16, 2026
@GuillaumeGomez GuillaumeGomez force-pushed the matches_instead_of_eq branch from d8ab39c to 97608f8 Compare May 16, 2026 00:51
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 16, 2026

Lintcheck changes for b5f52cd

Lint Added Removed Changed
clippy::matches_instead_of_eq 38 0 0

This comment will be updated if you push new changes

@GuillaumeGomez GuillaumeGomez force-pushed the matches_instead_of_eq branch from 97608f8 to 36f7572 Compare May 16, 2026 03:00
@GuillaumeGomez
Copy link
Copy Markdown
Member Author

GuillaumeGomez commented May 16, 2026

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) {

@lukaslueg
Copy link
Copy Markdown
Contributor

Maybe a nit: If the type being compared is PartialEq but manually implements that, there may be a subtle difference in behavior, as matches! only does the pattern-matching, while the custom PartialEq-impl may go beyond that; even if one might consider such a PartialEq-impl hazardous for exactly this reason, we may want to not lint if PartialEq is custom-made, as the lint might cause a change in behavior.

@GuillaumeGomez
Copy link
Copy Markdown
Member Author

Very good point. I'll handle this case as well, thanks!

@GuillaumeGomez GuillaumeGomez force-pushed the matches_instead_of_eq branch 2 times, most recently from 302fceb to b7af90a Compare May 17, 2026 00:31
@GuillaumeGomez GuillaumeGomez force-pushed the matches_instead_of_eq branch from b7af90a to b5f52cd Compare May 17, 2026 00:38
@GuillaumeGomez
Copy link
Copy Markdown
Member Author

Added suggested change.


declare_clippy_lint! {
/// ### What it does
/// Checks for the use of `matches!` with types that implement `PartialEq` (not manually).
Copy link
Copy Markdown
Member

@samueltardieu samueltardieu May 18, 2026

Choose a reason for hiding this comment

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

Suggested change
/// 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`.

View changes since the review

}

cx.tcx.for_each_relevant_impl(partial_eq_def_id, expr_ty, |impl_id| {
if !cx.tcx.is_automatically_derived(impl_id) {
Copy link
Copy Markdown
Member

@samueltardieu samueltardieu May 18, 2026

Choose a reason for hiding this comment

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

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 MaybeIncorrect and a note saying that E's PartialEq may not behave as pattern matching

While in other cases, I think we can lint with MachineApplicable.

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think we shouldn't link in this case to follow the "do not lint if manual PartialEq impl" rule.

@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 May 18, 2026
@samueltardieu
Copy link
Copy Markdown
Member

samueltardieu commented May 20, 2026 via email

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 22, 2026

☔ The latest upstream changes (possibly #17053) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp PRs that add, remove, or rename lints and need an FCP 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.

Suggest using == instead of matches!

4 participants