Skip to content

Commit

Permalink
address wrong suggestions when using comparing with byte literal stri…
Browse files Browse the repository at this point in the history
…ng in single_match lint
  • Loading branch information
lapla-cogito committed Dec 13, 2024
1 parent c2d23ad commit 7886fee
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/matches/single_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/single_match.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down
7 changes: 7 additions & 0 deletions tests/ui/single_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down
23 changes: 16 additions & 7 deletions tests/ui/single_match.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
Expand All @@ -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!(),
Expand All @@ -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 => {
Expand All @@ -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 => {},
Expand All @@ -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 => (),
Expand All @@ -273,13 +282,13 @@ 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!(),
LL | | _ => {},
LL | | }
| |_____^ help: try: `println!()`

error: aborting due to 26 previous errors
error: aborting due to 27 previous errors

0 comments on commit 7886fee

Please sign in to comment.