diff --git a/clippy_lints/src/matches/match_like_matches.rs b/clippy_lints/src/matches/match_like_matches.rs index a68eec842ab..0da4833f1df 100644 --- a/clippy_lints/src/matches/match_like_matches.rs +++ b/clippy_lints/src/matches/match_like_matches.rs @@ -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 { +fn find_bool_lit(ex: &ExprKind<'_>) -> Option { match ex { ExprKind::Lit(Spanned { node: LitKind::Bool(b), .. @@ -156,7 +156,7 @@ fn find_bool_lit(ex: &ExprKind<'_>, is_if_let: bool) -> Option { .. }, _, - ) if is_if_let => { + ) => { if let ExprKind::Lit(Spanned { node: LitKind::Bool(b), .. }) = exp.kind diff --git a/tests/ui/match_expr_like_matches_macro.fixed b/tests/ui/match_expr_like_matches_macro.fixed index 36f233f3346..1ccbfda64b7 100644 --- a/tests/ui/match_expr_like_matches_macro.fixed +++ b/tests/ui/match_expr_like_matches_macro.fixed @@ -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); diff --git a/tests/ui/match_expr_like_matches_macro.rs b/tests/ui/match_expr_like_matches_macro.rs index 750f69fa508..a49991f5941 100644 --- a/tests/ui/match_expr_like_matches_macro.rs +++ b/tests/ui/match_expr_like_matches_macro.rs @@ -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 { diff --git a/tests/ui/match_expr_like_matches_macro.stderr b/tests/ui/match_expr_like_matches_macro.stderr index d7cedf9f9f1..e94555e2744 100644 --- a/tests/ui/match_expr_like_matches_macro.stderr +++ b/tests/ui/match_expr_like_matches_macro.stderr @@ -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