From 7886fee6d9066375ed9eeb7c538381f69c1146e7 Mon Sep 17 00:00:00 2001 From: lapla-cogito Date: Fri, 13 Dec 2024 19:07:10 +0900 Subject: [PATCH] address wrong suggestions when using comparing with byte literal string in single_match lint --- clippy_lints/src/matches/single_match.rs | 2 +- tests/ui/single_match.fixed | 4 ++++ tests/ui/single_match.rs | 7 +++++++ tests/ui/single_match.stderr | 23 ++++++++++++++++------- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/clippy_lints/src/matches/single_match.rs b/clippy_lints/src/matches/single_match.rs index 95a4bf6f60de..3ca20479f8e0 100644 --- a/clippy_lints/src/matches/single_match.rs +++ b/clippy_lints/src/matches/single_match.rs @@ -129,7 +129,7 @@ fn report_single_pattern(cx: &LateContext<'_>, ex: &Expr<'_>, arm: &Arm<'_>, exp PatKind::Lit(Expr { kind: ExprKind::Lit(lit), .. - }) if lit.node.is_str() => pat_ref_count + 1, + }) if lit.node.is_str() || lit.node.is_bytestr() => pat_ref_count + 1, _ => pat_ref_count, }; // References are only implicitly added to the pattern, so no overflow here. diff --git a/tests/ui/single_match.fixed b/tests/ui/single_match.fixed index 4016b2699d6b..64d36e785afe 100644 --- a/tests/ui/single_match.fixed +++ b/tests/ui/single_match.fixed @@ -297,6 +297,10 @@ fn issue11365() { if let Some(A | B) = &Some(A) { println!() } } +fn issue12758(s: &[u8]) { + if &s[0..3] == b"foo" { println!() } +} + #[derive(Eq, PartialEq)] pub struct Data([u8; 4]); diff --git a/tests/ui/single_match.rs b/tests/ui/single_match.rs index 75edaa606053..a58b0bef5656 100644 --- a/tests/ui/single_match.rs +++ b/tests/ui/single_match.rs @@ -361,6 +361,13 @@ fn issue11365() { } } +fn issue12758(s: &[u8]) { + match &s[0..3] { + b"foo" => println!(), + _ => {}, + } +} + #[derive(Eq, PartialEq)] pub struct Data([u8; 4]); diff --git a/tests/ui/single_match.stderr b/tests/ui/single_match.stderr index 9240b09c50a9..5105e33c7c0b 100644 --- a/tests/ui/single_match.stderr +++ b/tests/ui/single_match.stderr @@ -216,8 +216,17 @@ LL | | None | Some(_) => {}, LL | | } | |_____^ help: try: `if let Some(A | B) = &Some(A) { println!() }` +error: you seem to be trying to use `match` for an equality check. Consider using `if` + --> tests/ui/single_match.rs:365:5 + | +LL | / match &s[0..3] { +LL | | b"foo" => println!(), +LL | | _ => {}, +LL | | } + | |_____^ help: try: `if &s[0..3] == b"foo" { println!() }` + error: this pattern is irrefutable, `match` is useless - --> tests/ui/single_match.rs:371:5 + --> tests/ui/single_match.rs:378:5 | LL | / match DATA { LL | | DATA => println!(), @@ -226,7 +235,7 @@ LL | | } | |_____^ help: try: `println!();` error: this pattern is irrefutable, `match` is useless - --> tests/ui/single_match.rs:376:5 + --> tests/ui/single_match.rs:383:5 | LL | / match CONST_I32 { LL | | CONST_I32 => println!(), @@ -235,7 +244,7 @@ LL | | } | |_____^ help: try: `println!();` error: this pattern is irrefutable, `match` is useless - --> tests/ui/single_match.rs:382:5 + --> tests/ui/single_match.rs:389:5 | LL | / match i { LL | | i => { @@ -255,7 +264,7 @@ LL + } | error: this pattern is irrefutable, `match` is useless - --> tests/ui/single_match.rs:390:5 + --> tests/ui/single_match.rs:397:5 | LL | / match i { LL | | i => {}, @@ -264,7 +273,7 @@ LL | | } | |_____^ help: `match` expression can be removed error: this pattern is irrefutable, `match` is useless - --> tests/ui/single_match.rs:395:5 + --> tests/ui/single_match.rs:402:5 | LL | / match i { LL | | i => (), @@ -273,7 +282,7 @@ LL | | } | |_____^ help: `match` expression can be removed error: this pattern is irrefutable, `match` is useless - --> tests/ui/single_match.rs:400:5 + --> tests/ui/single_match.rs:407:5 | LL | / match CONST_I32 { LL | | CONST_I32 => println!(), @@ -281,5 +290,5 @@ LL | | _ => {}, LL | | } | |_____^ help: try: `println!()` -error: aborting due to 26 previous errors +error: aborting due to 27 previous errors