Skip to content

Commit

Permalink
match_like_matches_macro: strip refs in suggestion
Browse files Browse the repository at this point in the history
fixes rust-lang#6503

changelog: match_like_matches_macro: strip refs in suggestion (rust-lang#6503)
  • Loading branch information
matthiaskrgr committed Jan 1, 2021
1 parent 60919e4 commit cd199de
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
5 changes: 5 additions & 0 deletions clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,11 @@ fn find_matches_sugg(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr
} else {
pat
};
// strip potential borrows (#6503)
let mut ex = ex;
if let ExprKind::AddrOf(BorrowKind::Ref, .., ex2) = ex.kind {
ex = ex2
}
span_lint_and_sugg(
cx,
MATCH_LIKE_MATCHES_MACRO,
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/match_expr_like_matches_macro.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,8 @@ fn main() {
_ => false,
};
}

// should print "z" in suggestion (#6503)
let z = Some(3);
let _z = matches!(z, Some(3));
}
7 changes: 7 additions & 0 deletions tests/ui/match_expr_like_matches_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,11 @@ fn main() {
_ => false,
};
}

// should print "z" in suggestion (#6503)
let z = Some(3);
let _z = match &z {
Some(3) => true,
_ => false,
};
}
12 changes: 11 additions & 1 deletion tests/ui/match_expr_like_matches_macro.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,15 @@ LL | | _ => true,
LL | | };
| |_________^ help: try this: `!matches!(x, E::B(_) | E::C)`

error: aborting due to 7 previous errors
error: match expression looks like `matches!` macro
--> $DIR/match_expr_like_matches_macro.rs:125:14
|
LL | let _z = match &z {
| ______________^
LL | | Some(3) => true,
LL | | _ => false,
LL | | };
| |_____^ help: try this: `matches!(z, Some(3))`

error: aborting due to 8 previous errors

0 comments on commit cd199de

Please sign in to comment.