Skip to content

Suggest using == instead of matches! #14688

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
emilk opened this issue Apr 24, 2025 · 3 comments · May be fixed by #14820
Open

Suggest using == instead of matches! #14688

emilk opened this issue Apr 24, 2025 · 3 comments · May be fixed by #14820
Assignees
Labels
A-lint Area: New lints

Comments

@emilk
Copy link

emilk commented Apr 24, 2025

What it does

Looks for unnecessary uses of matches! that can be replaced with == or !=

Advantage

Using matches! can produce unnecessarily complex code. When negated, you also get the "double-bang" confusion of !matches!(…), where tje twp ! means different things.

Drawbacks

No response

Example

if !matches!(value, Enum::Variant) {

Could be written as:

if value != Enum::Variant {
@emilk emilk added the A-lint Area: New lints label Apr 24, 2025
@lukaslueg
Copy link
Contributor

FWIW: Notice that value != Enum::Variant requires Enum: PartialEq, while matches! does not; this also means Enum<T: PartialEq>, even if Enum::Variant does not carry a T. We have a warn-by-default lint wrt value == None in place so people don't fall into this specific pattern. IMHO its not advisable to teach people to do by-value comparison if structural comparison is what they need.

@emilk
Copy link
Author

emilk commented Apr 25, 2025

Yes, we should of course only suggest using == or != for types that implement PartialEq

@ARandomDev99
Copy link
Contributor

@rustbot claim

@ARandomDev99 ARandomDev99 linked a pull request May 16, 2025 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants