Skip to content

redundant_pattern_match does not trigger on matches! macro #10803

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

Closed
disco07 opened this issue May 20, 2023 · 4 comments
Closed

redundant_pattern_match does not trigger on matches! macro #10803

disco07 opened this issue May 20, 2023 · 4 comments
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@disco07
Copy link
Contributor

disco07 commented May 20, 2023

Summary

Uses of matches macro with Result or Option in first argument and in the second argument is just here to check if the first argument is is_ok() or is_some(), do not lint. When I make cargo expand on this:

matches!(Ok::<i32, i32>(10), Ok(_));

I have:

match Ok::<i32, i32>(10) {
        Ok(_) => true,
        _ => false,
    };

Lint Name

redundant_pattern_match

Reproducer

I tried this code:

matches!(Ok::<i32, i32>(10), Ok(_));

I expected to see this happen:

Ok::<i32, i32>(10).is_ok()

Instead, this happened:

matches!(Ok::<i32, i32>(10), Ok(_));

Version

rustc 1.71.0-nightly (521f4dae1 2023-05-19)
binary: rustc
commit-hash: 521f4dae1bdf7fe9cf9436ecef9ee7c7442708bf
commit-date: 2023-05-19
host: x86_64-unknown-linux-gnu
release: 1.71.0-nightly
LLVM version: 16.0.4
@disco07 disco07 added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels May 20, 2023
@disco07
Copy link
Contributor Author

disco07 commented May 20, 2023

@flip1995 I want to work on this, have you some ideas (suggestions) to give me please to resolve this?

@flip1995 flip1995 changed the title redundant_pattern_match redundant_pattern_match does not trigger on matches! macro May 22, 2023
@flip1995
Copy link
Member

This is a bit complicated. The macro expands to a match expression. The redundant_pattern_match lint i probably has a span.from_expansion condition in it, that prevents it from triggering inside of (external) macros. We could loosen that restriction, by checking the expn_data if the expansion (directly) comes from the matches! macro. I think we have a utility function called is_expn_of of something similar for this.

I haven't looked into the code of this lint for quite some time. Let me know, if this explanation already helps you get started, or if you want some more detailed explanation. I'm happy to do that, if necessary :)

@disco07
Copy link
Contributor Author

disco07 commented May 24, 2023

@flip1995 Thanks I will explore this path

@disco07
Copy link
Contributor Author

disco07 commented May 24, 2023

@rustbot claim

bors added a commit that referenced this issue May 26, 2023
Fix redundant_pattern_match on matches! macro

This PR solve this issue #10803
r? `@flip1995`

changelog: none
@disco07 disco07 closed this as completed May 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

No branches or pull requests

2 participants