Skip to content

redundant_pattern_matching: look inside Refs #6991

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

Merged
merged 1 commit into from
Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1723,7 +1723,13 @@ mod redundant_pattern_match {
arms: &[Arm<'_>],
keyword: &'static str,
) {
let good_method = match arms[0].pat.kind {
// also look inside refs
let mut kind = &arms[0].pat.kind;
// if we have &None for example, peel it so we can detect "if let None = x"
if let PatKind::Ref(inner, _mutability) = kind {
kind = &inner.kind;
}
let good_method = match kind {
PatKind::TupleStruct(ref path, ref patterns, _) if patterns.len() == 1 => {
if let PatKind::Wild = patterns[0].kind {
if match_qpath(path, &paths::RESULT_OK) {
Expand Down
16 changes: 15 additions & 1 deletion tests/ui/match_ref_pats.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ LL | Some(v) => println!("{:?}", v),
LL | None => println!("none"),
|

error: redundant pattern matching, consider using `is_none()`
--> $DIR/match_ref_pats.rs:35:12
|
LL | if let &None = a {
| -------^^^^^---- help: try this: `if a.is_none()`
|
= note: `-D clippy::redundant-pattern-matching` implied by `-D warnings`

error: you don't need to add `&` to all patterns
--> $DIR/match_ref_pats.rs:35:5
|
Expand All @@ -59,6 +67,12 @@ help: instead of prefixing all patterns with `&`, you can dereference the expres
LL | if let None = *a {
| ^^^^ ^^

error: redundant pattern matching, consider using `is_none()`
--> $DIR/match_ref_pats.rs:40:12
|
LL | if let &None = &b {
| -------^^^^^----- help: try this: `if b.is_none()`

error: you don't need to add `&` to both the expression and the patterns
--> $DIR/match_ref_pats.rs:40:5
|
Expand Down Expand Up @@ -87,5 +101,5 @@ LL | match *foo_variant!(0) {
LL | Foo::A => println!("A"),
|

error: aborting due to 6 previous errors
error: aborting due to 8 previous errors