Skip to content

Commit 4bcc552

Browse files
committed
Auto merge of #6991 - matthiaskrgr:5396, r=giraffate
redundant_pattern_matching: look inside Refs look inside refs and detect if let &None = ... Fixes #5396 changelog: redundant_pattern_matching: look inside Refs to fix FNs with "if let &None = .. "
2 parents 8e56a2b + e006c77 commit 4bcc552

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

clippy_lints/src/matches.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1723,7 +1723,13 @@ mod redundant_pattern_match {
17231723
arms: &[Arm<'_>],
17241724
keyword: &'static str,
17251725
) {
1726-
let good_method = match arms[0].pat.kind {
1726+
// also look inside refs
1727+
let mut kind = &arms[0].pat.kind;
1728+
// if we have &None for example, peel it so we can detect "if let None = x"
1729+
if let PatKind::Ref(inner, _mutability) = kind {
1730+
kind = &inner.kind;
1731+
}
1732+
let good_method = match kind {
17271733
PatKind::TupleStruct(ref path, ref patterns, _) if patterns.len() == 1 => {
17281734
if let PatKind::Wild = patterns[0].kind {
17291735
if match_qpath(path, &paths::RESULT_OK) {

tests/ui/match_ref_pats.stderr

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ LL | Some(v) => println!("{:?}", v),
4646
LL | None => println!("none"),
4747
|
4848

49+
error: redundant pattern matching, consider using `is_none()`
50+
--> $DIR/match_ref_pats.rs:35:12
51+
|
52+
LL | if let &None = a {
53+
| -------^^^^^---- help: try this: `if a.is_none()`
54+
|
55+
= note: `-D clippy::redundant-pattern-matching` implied by `-D warnings`
56+
4957
error: you don't need to add `&` to all patterns
5058
--> $DIR/match_ref_pats.rs:35:5
5159
|
@@ -59,6 +67,12 @@ help: instead of prefixing all patterns with `&`, you can dereference the expres
5967
LL | if let None = *a {
6068
| ^^^^ ^^
6169

70+
error: redundant pattern matching, consider using `is_none()`
71+
--> $DIR/match_ref_pats.rs:40:12
72+
|
73+
LL | if let &None = &b {
74+
| -------^^^^^----- help: try this: `if b.is_none()`
75+
6276
error: you don't need to add `&` to both the expression and the patterns
6377
--> $DIR/match_ref_pats.rs:40:5
6478
|
@@ -87,5 +101,5 @@ LL | match *foo_variant!(0) {
87101
LL | Foo::A => println!("A"),
88102
|
89103

90-
error: aborting due to 6 previous errors
104+
error: aborting due to 8 previous errors
91105

0 commit comments

Comments
 (0)