Skip to content
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

redundant_pattern_matching does not check for match with single arm #10726

Closed
disco07 opened this issue Apr 30, 2023 · 6 comments
Closed

redundant_pattern_matching does not check for match with single arm #10726

disco07 opened this issue Apr 30, 2023 · 6 comments
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@disco07
Copy link
Contributor

disco07 commented Apr 30, 2023

Summary

Use of match, matching Result or Option; do not trigger clippy::redundant_pattern_matching

Reproducer

I tried this code:

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

I expected to see this happen:

Err::<i32, i32>(55).is_ok();

Instead, this happened:

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

Version

rustc 1.71.0-nightly (b628260df 2023-04-22)
binary: rustc
commit-hash: b628260df0587ae559253d8640ecb8738d3de613
commit-date: 2023-04-22
host: x86_64-unknown-linux-gnu
release: 1.71.0-nightly
LLVM version: 16.0.2

Additional Labels

No response

@disco07 disco07 added the C-bug Category: Clippy is not doing the correct thing label Apr 30, 2023
@disco07
Copy link
Contributor Author

disco07 commented Apr 30, 2023

@xFrednet

@xFrednet
Copy link
Member

xFrednet commented May 1, 2023

Hey @disco07, thank you for the ticket :). Do you have a specific question related to it :)

@disco07
Copy link
Contributor Author

disco07 commented May 1, 2023

@xFrednet Can I work on this ?

@xFrednet
Copy link
Member

xFrednet commented May 2, 2023

Yes, you're very welcome to! As a start, you can take a look at the lint code: https://github.com/rust-lang/rust-clippy/blob/master/clippy_lints/src/matches/redundant_pattern_match.rs

And look at the AST structure of the code you want to detect. For this, I recommend using #[clippy::dump] and running tool > clippy. See Playground. Example actually triggers the match_like_matches_macro lint.

I guess that the lint ignores the match, as it expects the Ok and Err branches, and not a wildcard. Maybe you just need to add a check for that :)

@disco07
Copy link
Contributor Author

disco07 commented May 2, 2023

@rustbot claim

1 similar comment
@disco07
Copy link
Contributor Author

disco07 commented May 4, 2023

@rustbot claim

bors added a commit that referenced this issue May 18, 2023
redundant_pattern_matching

This PR try to solve this issue #10726,
but it enter in conflict with another test.

Try to test this:
```
let _w = match x {
     Some(_) => true,
     _ => false,
};
```

this happen:
```
 error: match expression looks like `matches!` macro
   --> $DIR/match_expr_like_matches_macro.rs:21:14
    |
 LL |       let _w = match x {
    |  ______________^
 LL | |         Some(_) => true,
 LL | |         _ => false,
 LL | |     };
    | |_____^ help: try this: `matches!(x, Some(_))`

+error: redundant pattern matching, consider using `is_some()`
+  --> $DIR/match_expr_like_matches_macro.rs:21:14
+   |
+LL |       let _w = match x {
+   |  ______________^
+LL | |         Some(_) => true,
+LL | |         _ => false,
+LL | |     };
+   | |_____^ help: try this: `x.is_some()`
+   |
+   = note: `-D clippy::redundant-pattern-matching` implied by `-D warnings`
+
```
I need some help to fix this. Thanks
bors added a commit that referenced this issue May 18, 2023
redundant_pattern_matching

This PR try to solve this issue #10726,
but it enter in conflict with another test.

changelog: none

Try to test this:
```
let _w = match x {
     Some(_) => true,
     _ => false,
};
```

this happen:
```
 error: match expression looks like `matches!` macro
   --> $DIR/match_expr_like_matches_macro.rs:21:14
    |
 LL |       let _w = match x {
    |  ______________^
 LL | |         Some(_) => true,
 LL | |         _ => false,
 LL | |     };
    | |_____^ help: try this: `matches!(x, Some(_))`

+error: redundant pattern matching, consider using `is_some()`
+  --> $DIR/match_expr_like_matches_macro.rs:21:14
+   |
+LL |       let _w = match x {
+   |  ______________^
+LL | |         Some(_) => true,
+LL | |         _ => false,
+LL | |     };
+   | |_____^ help: try this: `x.is_some()`
+   |
+   = note: `-D clippy::redundant-pattern-matching` implied by `-D warnings`
+
```
I need some help to fix this. Thanks
@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
Projects
None yet
Development

No branches or pull requests

2 participants