Skip to content

Commit

Permalink
correct single_match lint suggestion (#13824)
Browse files Browse the repository at this point in the history
fix #12758

The `single_match` lint makes incorrect suggestions when comparing with
byte string literals. There seems to be an issue with the logic that
calculates the number of refs when creating suggestions.

changelog: [`single_match`]: No longer make incorrect suggestions when
comparing with byte string literals.
  • Loading branch information
blyxyas authored Dec 13, 2024
2 parents c607408 + e38fa00 commit 13463cb
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 @@ -303,6 +303,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 @@ -204,8 +204,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 @@ -214,7 +223,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 @@ -223,7 +232,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 @@ -243,7 +252,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 @@ -252,7 +261,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 @@ -261,13 +270,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 25 previous errors
error: aborting due to 26 previous errors

0 comments on commit 13463cb

Please sign in to comment.