Auto merge of #9178 - alex-semenyuk:match_like_matches_macro_fix, r=Jarcho

match_like_matches_macro does not trigger when one arm contains conta…

Close #9163
changelog: none
This commit is contained in:
bors 2022-07-15 13:57:46 +00:00
commit aa0706bf20
4 changed files with 51 additions and 20 deletions

View File

@ -81,14 +81,14 @@ where
if let Some((_, last_pat_opt, last_expr, _)) = iter.next_back();
let iter_without_last = iter.clone();
if let Some((first_attrs, _, first_expr, first_guard)) = iter.next();
if let Some(b0) = find_bool_lit(&first_expr.kind, is_if_let);
if let Some(b1) = find_bool_lit(&last_expr.kind, is_if_let);
if let Some(b0) = find_bool_lit(&first_expr.kind);
if let Some(b1) = find_bool_lit(&last_expr.kind);
if b0 != b1;
if first_guard.is_none() || iter.len() == 0;
if first_attrs.is_empty();
if iter
.all(|arm| {
find_bool_lit(&arm.2.kind, is_if_let).map_or(false, |b| b == b0) && arm.3.is_none() && arm.0.is_empty()
find_bool_lit(&arm.2.kind).map_or(false, |b| b == b0) && arm.3.is_none() && arm.0.is_empty()
});
then {
if let Some(last_pat) = last_pat_opt {
@ -144,7 +144,7 @@ where
}
/// Extract a `bool` or `{ bool }`
fn find_bool_lit(ex: &ExprKind<'_>, is_if_let: bool) -> Option<bool> {
fn find_bool_lit(ex: &ExprKind<'_>) -> Option<bool> {
match ex {
ExprKind::Lit(Spanned {
node: LitKind::Bool(b), ..
@ -156,7 +156,7 @@ fn find_bool_lit(ex: &ExprKind<'_>, is_if_let: bool) -> Option<bool> {
..
},
_,
) if is_if_let => {
) => {
if let ExprKind::Lit(Spanned {
node: LitKind::Bool(b), ..
}) = exp.kind

View File

@ -45,6 +45,12 @@ fn main() {
// lint
let _ans = matches!(x, E::A(_) | E::B(_));
}
{
// lint
// skip rustfmt to prevent removing block for first pattern
#[rustfmt::skip]
let _ans = matches!(x, E::A(_) | E::B(_));
}
{
// lint
let _ans = !matches!(x, E::B(_) | E::C);

View File

@ -61,6 +61,18 @@ fn main() {
_ => false,
};
}
{
// lint
// skip rustfmt to prevent removing block for first pattern
#[rustfmt::skip]
let _ans = match x {
E::A(_) => {
true
}
E::B(_) => true,
_ => false,
};
}
{
// lint
let _ans = match x {

View File

@ -60,7 +60,20 @@ LL | | };
| |_________^ help: try this: `matches!(x, E::A(_) | E::B(_))`
error: match expression looks like `matches!` macro
--> $DIR/match_expr_like_matches_macro.rs:66:20
--> $DIR/match_expr_like_matches_macro.rs:68:20
|
LL | let _ans = match x {
| ____________________^
LL | | E::A(_) => {
LL | | true
LL | | }
LL | | E::B(_) => true,
LL | | _ => false,
LL | | };
| |_________^ help: try this: `matches!(x, E::A(_) | E::B(_))`
error: match expression looks like `matches!` macro
--> $DIR/match_expr_like_matches_macro.rs:78:20
|
LL | let _ans = match x {
| ____________________^
@ -71,7 +84,7 @@ LL | | };
| |_________^ help: try this: `!matches!(x, E::B(_) | E::C)`
error: match expression looks like `matches!` macro
--> $DIR/match_expr_like_matches_macro.rs:126:18
--> $DIR/match_expr_like_matches_macro.rs:138:18
|
LL | let _z = match &z {
| __________________^
@ -81,7 +94,7 @@ LL | | };
| |_________^ help: try this: `matches!(z, Some(3))`
error: match expression looks like `matches!` macro
--> $DIR/match_expr_like_matches_macro.rs:135:18
--> $DIR/match_expr_like_matches_macro.rs:147:18
|
LL | let _z = match &z {
| __________________^
@ -91,7 +104,7 @@ LL | | };
| |_________^ help: try this: `matches!(&z, Some(3))`
error: match expression looks like `matches!` macro
--> $DIR/match_expr_like_matches_macro.rs:152:21
--> $DIR/match_expr_like_matches_macro.rs:164:21
|
LL | let _ = match &z {
| _____________________^
@ -100,16 +113,6 @@ LL | | _ => false,
LL | | };
| |_____________^ help: try this: `matches!(&z, AnEnum::X)`
error: match expression looks like `matches!` macro
--> $DIR/match_expr_like_matches_macro.rs:166:20
|
LL | let _res = match &val {
| ____________________^
LL | | &Some(ref _a) => true,
LL | | _ => false,
LL | | };
| |_________^ help: try this: `matches!(&val, &Some(ref _a))`
error: match expression looks like `matches!` macro
--> $DIR/match_expr_like_matches_macro.rs:178:20
|
@ -120,5 +123,15 @@ LL | | _ => false,
LL | | };
| |_________^ help: try this: `matches!(&val, &Some(ref _a))`
error: aborting due to 12 previous errors
error: match expression looks like `matches!` macro
--> $DIR/match_expr_like_matches_macro.rs:190:20
|
LL | let _res = match &val {
| ____________________^
LL | | &Some(ref _a) => true,
LL | | _ => false,
LL | | };
| |_________^ help: try this: `matches!(&val, &Some(ref _a))`
error: aborting due to 13 previous errors