Skip to content

Commit

Permalink
redundant_pattern_matching: look inside Refs
Browse files Browse the repository at this point in the history
look inside refs and detect if let &None = ...

Fixes rust-lang/rust-clippy#5396

changelog:  redundant_pattern_matching: look inside Refs to fix FNs with "if let &None = .. "
  • Loading branch information
matthiaskrgr committed Mar 27, 2021
1 parent 8e56a2b commit e006c77
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
8 changes: 7 additions & 1 deletion clippy_lints/src/matches.rs
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
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

0 comments on commit e006c77

Please sign in to comment.