Auto merge of #6991 - matthiaskrgr:5396, r=giraffate

redundant_pattern_matching: look inside Refs

look inside refs and detect if let &None = ...

Fixes https://github.com/rust-lang/rust-clippy/issues/5396

changelog:  redundant_pattern_matching: look inside Refs to fix FNs with "if let &None = .. "
This commit is contained in:
bors 2021-03-28 14:20:53 +00:00
commit 4bcc552587
2 changed files with 22 additions and 2 deletions

View File

@ -1723,7 +1723,13 @@ fn find_sugg_for_if_let<'tcx>(
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) {

View File

@ -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
|
@ -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
|
@ -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