diff options
| author | bors <bors@rust-lang.org> | 2022-07-15 13:57:46 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-07-15 13:57:46 +0000 |
| commit | aa0706bf20bd6f647c0132e0779e99b7c11208df (patch) | |
| tree | 747e99fc52fdcb8d779796b5c5cd6eaa118e7cb6 | |
| parent | fe6814508f283d2a4a7583eab2087b4a3356658a (diff) | |
| parent | 2894df34092ce5fb9bfcf74a22a397b17dfaf983 (diff) | |
| download | rust-aa0706bf20bd6f647c0132e0779e99b7c11208df.tar.gz rust-aa0706bf20bd6f647c0132e0779e99b7c11208df.zip | |
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
| -rw-r--r-- | clippy_lints/src/matches/match_like_matches.rs | 10 | ||||
| -rw-r--r-- | tests/ui/match_expr_like_matches_macro.fixed | 6 | ||||
| -rw-r--r-- | tests/ui/match_expr_like_matches_macro.rs | 12 | ||||
| -rw-r--r-- | tests/ui/match_expr_like_matches_macro.stderr | 27 |
4 files changed, 43 insertions, 12 deletions
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<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 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 @@ -47,6 +47,12 @@ fn main() { } { // 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 @@ -63,6 +63,18 @@ fn main() { } { // 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 { E::B(_) => false, E::C => false, 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 { | _____________________^ @@ -101,7 +114,7 @@ LL | | }; | |_____________^ help: try this: `matches!(&z, AnEnum::X)` error: match expression looks like `matches!` macro - --> $DIR/match_expr_like_matches_macro.rs:166:20 + --> $DIR/match_expr_like_matches_macro.rs:178:20 | LL | let _res = match &val { | ____________________^ @@ -111,7 +124,7 @@ 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 + --> $DIR/match_expr_like_matches_macro.rs:190:20 | LL | let _res = match &val { | ____________________^ @@ -120,5 +133,5 @@ LL | | _ => false, LL | | }; | |_________^ help: try this: `matches!(&val, &Some(ref _a))` -error: aborting due to 12 previous errors +error: aborting due to 13 previous errors |
