Skip to content

Commit

Permalink
Fix suggestion for deref expressions in redundant_pattern_matching
Browse files Browse the repository at this point in the history
  • Loading branch information
Serial-ATA committed Nov 8, 2021
1 parent 07f4f7c commit 7b94826
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
4 changes: 3 additions & 1 deletion clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1795,7 +1795,7 @@ mod redundant_pattern_match {
use rustc_hir::LangItem::{OptionNone, OptionSome, PollPending, PollReady, ResultErr, ResultOk};
use rustc_hir::{
intravisit::{walk_expr, ErasedMap, NestedVisitorMap, Visitor},
Arm, Block, Expr, ExprKind, LangItem, MatchSource, Node, Pat, PatKind, QPath,
Arm, Block, Expr, ExprKind, LangItem, MatchSource, Node, Pat, PatKind, QPath, UnOp,
};
use rustc_lint::LateContext;
use rustc_middle::ty::{self, subst::GenericArgKind, Ty};
Expand Down Expand Up @@ -2049,8 +2049,10 @@ mod redundant_pattern_match {

let result_expr = match &let_expr.kind {
ExprKind::AddrOf(_, _, borrowed) => borrowed,
ExprKind::Unary(UnOp::Deref, deref) => deref,
_ => let_expr,
};

span_lint_and_then(
cx,
REDUNDANT_PATTERN_MATCHING,
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/redundant_pattern_matching_option.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ fn main() {
} else {
3
};

issue7921();
}

fn gen_opt() -> Option<()> {
Expand All @@ -80,3 +82,8 @@ const fn issue6067() {

None::<()>.is_none();
}

#[allow(clippy::deref_addrof)]
fn issue7921() {
if (&None::<()>).is_none() {}
}
7 changes: 7 additions & 0 deletions tests/ui/redundant_pattern_matching_option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ fn main() {
} else {
3
};

issue7921();
}

fn gen_opt() -> Option<()> {
Expand Down Expand Up @@ -95,3 +97,8 @@ const fn issue6067() {
None => true,
};
}

#[allow(clippy::deref_addrof)]
fn issue7921() {
if let None = *(&None::<()>) {}
}
20 changes: 13 additions & 7 deletions tests/ui/redundant_pattern_matching_option.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -89,31 +89,31 @@ LL | } else if let None = gen_opt() {
| -------^^^^------------ help: try this: `if gen_opt().is_none()`

error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching_option.rs:80:12
--> $DIR/redundant_pattern_matching_option.rs:82:12
|
LL | if let Some(_) = Some(42) {}
| -------^^^^^^^----------- help: try this: `if Some(42).is_some()`

error: redundant pattern matching, consider using `is_none()`
--> $DIR/redundant_pattern_matching_option.rs:82:12
--> $DIR/redundant_pattern_matching_option.rs:84:12
|
LL | if let None = None::<()> {}
| -------^^^^------------- help: try this: `if None::<()>.is_none()`

error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching_option.rs:84:15
--> $DIR/redundant_pattern_matching_option.rs:86:15
|
LL | while let Some(_) = Some(42) {}
| ----------^^^^^^^----------- help: try this: `while Some(42).is_some()`

error: redundant pattern matching, consider using `is_none()`
--> $DIR/redundant_pattern_matching_option.rs:86:15
--> $DIR/redundant_pattern_matching_option.rs:88:15
|
LL | while let None = None::<()> {}
| ----------^^^^------------- help: try this: `while None::<()>.is_none()`

error: redundant pattern matching, consider using `is_some()`
--> $DIR/redundant_pattern_matching_option.rs:88:5
--> $DIR/redundant_pattern_matching_option.rs:90:5
|
LL | / match Some(42) {
LL | | Some(_) => true,
Expand All @@ -122,13 +122,19 @@ LL | | };
| |_____^ help: try this: `Some(42).is_some()`

error: redundant pattern matching, consider using `is_none()`
--> $DIR/redundant_pattern_matching_option.rs:93:5
--> $DIR/redundant_pattern_matching_option.rs:95:5
|
LL | / match None::<()> {
LL | | Some(_) => false,
LL | | None => true,
LL | | };
| |_____^ help: try this: `None::<()>.is_none()`

error: aborting due to 19 previous errors
error: redundant pattern matching, consider using `is_none()`
--> $DIR/redundant_pattern_matching_option.rs:103:12
|
LL | if let None = *(&None::<()>) {}
| -------^^^^----------------- help: try this: `if (&None::<()>).is_none()`

error: aborting due to 20 previous errors

0 comments on commit 7b94826

Please sign in to comment.