Skip to content

WIP: New lint: duplicate_match_guards #14986

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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

ada4a
Copy link
Contributor

@ada4a ada4a commented Jun 5, 2025

Fixes #14971

WIP because:

  • is there a way to remove the block braces from the suggestion?
  • I guess the "Known problems" section is not really true, since eq_expr_value takes care of impure function calls?
  • it would be good to support the recently stabilized if-let guards

changelog: new lint: [duplicate_match_guards]

@rustbot
Copy link
Collaborator

rustbot commented Jun 5, 2025

r? @y21

rustbot has assigned @y21.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 5, 2025
@ada4a ada4a force-pushed the duplicate_match_guard branch 4 times, most recently from bd9de96 to c7f0188 Compare June 6, 2025 10:22
@ada4a ada4a force-pushed the duplicate_match_guard branch from c7f0188 to 71c4b42 Compare June 24, 2025 17:33
@ada4a ada4a force-pushed the duplicate_match_guard branch 2 times, most recently from 8bd7cbe to a86397c Compare July 2, 2025 17:20
@ada4a ada4a changed the title WIP: New lint: duplicate_match_guard WIP: New lint: duplicate_match_guards Jul 2, 2025
@ada4a ada4a force-pushed the duplicate_match_guard branch 5 times, most recently from fab6b13 to 1a5e31b Compare July 2, 2025 22:49
@ada4a ada4a force-pushed the duplicate_match_guard branch from 1a5e31b to d6e58a9 Compare August 7, 2025 21:52
};

if let ExprKind::If(cond, then, None) = arm_body_expr.kind
&& eq_expr_value(cx, guard, cond.peel_drop_temps())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since the lint is directed at copy-paste errors, maybe we should make this stricter, and make sure that the expr are "syntactically" equal, and don't just evaluate to the result.

}
declare_lint_pass!(DuplicateMatchGuards => [DUPLICATE_MATCH_GUARDS]);

impl<'tcx> LateLintPass<'tcx> for DuplicateMatchGuards {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now that I think of it, this lint doesn't really type information, so it could be made early-pass I think?

ada4a added 12 commits August 22, 2025 15:11
they are not really necessary, since the match body already has its own
This reverts commit 1b44da7.

it can be seen that things like comments are not included in this
reduced span
NB: this doesn't suggest removing the inner body's curlies since there
are no outer ones
required by the naming guidelines
previously, we suggested removing:
- everything from the start of `arm_body_expr` to the start of `then`
- and everything from the end of `then` to the end of `arm_body_expr`

but that should be the same as just replacing `arm_body_expr` with
`then`

we seem to get some more lines with trailing spaces, but that's probably
not that big of a deal...
@ada4a ada4a force-pushed the duplicate_match_guard branch from d6e58a9 to 3e4ace9 Compare August 22, 2025 13:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New lint: duplicate_match_guards
3 participants