Skip to content

Commit 7886fee

Browse files
committed
address wrong suggestions when using comparing with byte literal string in single_match lint
1 parent c2d23ad commit 7886fee

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

Diff for: clippy_lints/src/matches/single_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ fn report_single_pattern(cx: &LateContext<'_>, ex: &Expr<'_>, arm: &Arm<'_>, exp
129129
PatKind::Lit(Expr {
130130
kind: ExprKind::Lit(lit),
131131
..
132-
}) if lit.node.is_str() => pat_ref_count + 1,
132+
}) if lit.node.is_str() || lit.node.is_bytestr() => pat_ref_count + 1,
133133
_ => pat_ref_count,
134134
};
135135
// References are only implicitly added to the pattern, so no overflow here.

Diff for: tests/ui/single_match.fixed

+4
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,10 @@ fn issue11365() {
297297
if let Some(A | B) = &Some(A) { println!() }
298298
}
299299

300+
fn issue12758(s: &[u8]) {
301+
if &s[0..3] == b"foo" { println!() }
302+
}
303+
300304
#[derive(Eq, PartialEq)]
301305
pub struct Data([u8; 4]);
302306

Diff for: tests/ui/single_match.rs

+7
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,13 @@ fn issue11365() {
361361
}
362362
}
363363

364+
fn issue12758(s: &[u8]) {
365+
match &s[0..3] {
366+
b"foo" => println!(),
367+
_ => {},
368+
}
369+
}
370+
364371
#[derive(Eq, PartialEq)]
365372
pub struct Data([u8; 4]);
366373

Diff for: tests/ui/single_match.stderr

+16-7
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,17 @@ LL | | None | Some(_) => {},
216216
LL | | }
217217
| |_____^ help: try: `if let Some(A | B) = &Some(A) { println!() }`
218218

219+
error: you seem to be trying to use `match` for an equality check. Consider using `if`
220+
--> tests/ui/single_match.rs:365:5
221+
|
222+
LL | / match &s[0..3] {
223+
LL | | b"foo" => println!(),
224+
LL | | _ => {},
225+
LL | | }
226+
| |_____^ help: try: `if &s[0..3] == b"foo" { println!() }`
227+
219228
error: this pattern is irrefutable, `match` is useless
220-
--> tests/ui/single_match.rs:371:5
229+
--> tests/ui/single_match.rs:378:5
221230
|
222231
LL | / match DATA {
223232
LL | | DATA => println!(),
@@ -226,7 +235,7 @@ LL | | }
226235
| |_____^ help: try: `println!();`
227236

228237
error: this pattern is irrefutable, `match` is useless
229-
--> tests/ui/single_match.rs:376:5
238+
--> tests/ui/single_match.rs:383:5
230239
|
231240
LL | / match CONST_I32 {
232241
LL | | CONST_I32 => println!(),
@@ -235,7 +244,7 @@ LL | | }
235244
| |_____^ help: try: `println!();`
236245

237246
error: this pattern is irrefutable, `match` is useless
238-
--> tests/ui/single_match.rs:382:5
247+
--> tests/ui/single_match.rs:389:5
239248
|
240249
LL | / match i {
241250
LL | | i => {
@@ -255,7 +264,7 @@ LL + }
255264
|
256265

257266
error: this pattern is irrefutable, `match` is useless
258-
--> tests/ui/single_match.rs:390:5
267+
--> tests/ui/single_match.rs:397:5
259268
|
260269
LL | / match i {
261270
LL | | i => {},
@@ -264,7 +273,7 @@ LL | | }
264273
| |_____^ help: `match` expression can be removed
265274

266275
error: this pattern is irrefutable, `match` is useless
267-
--> tests/ui/single_match.rs:395:5
276+
--> tests/ui/single_match.rs:402:5
268277
|
269278
LL | / match i {
270279
LL | | i => (),
@@ -273,13 +282,13 @@ LL | | }
273282
| |_____^ help: `match` expression can be removed
274283

275284
error: this pattern is irrefutable, `match` is useless
276-
--> tests/ui/single_match.rs:400:5
285+
--> tests/ui/single_match.rs:407:5
277286
|
278287
LL | / match CONST_I32 {
279288
LL | | CONST_I32 => println!(),
280289
LL | | _ => {},
281290
LL | | }
282291
| |_____^ help: try: `println!()`
283292

284-
error: aborting due to 26 previous errors
293+
error: aborting due to 27 previous errors
285294

0 commit comments

Comments
 (0)