Skip to content

Commit 13463cb

Browse files
authored
correct single_match lint suggestion (rust-lang#13824)
fix rust-lang#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.
2 parents c607408 + e38fa00 commit 13463cb

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

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.

tests/ui/single_match.fixed

+4
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ fn issue11365() {
303303
if let Some(A | B) = &Some(A) { println!() }
304304
}
305305

306+
fn issue12758(s: &[u8]) {
307+
if &s[0..3] == b"foo" { println!() }
308+
}
309+
306310
#[derive(Eq, PartialEq)]
307311
pub struct Data([u8; 4]);
308312

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

tests/ui/single_match.stderr

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

207+
error: you seem to be trying to use `match` for an equality check. Consider using `if`
208+
--> tests/ui/single_match.rs:365:5
209+
|
210+
LL | / match &s[0..3] {
211+
LL | | b"foo" => println!(),
212+
LL | | _ => {},
213+
LL | | }
214+
| |_____^ help: try: `if &s[0..3] == b"foo" { println!() }`
215+
207216
error: this pattern is irrefutable, `match` is useless
208-
--> tests/ui/single_match.rs:371:5
217+
--> tests/ui/single_match.rs:378:5
209218
|
210219
LL | / match DATA {
211220
LL | | DATA => println!(),
@@ -214,7 +223,7 @@ LL | | }
214223
| |_____^ help: try: `println!();`
215224

216225
error: this pattern is irrefutable, `match` is useless
217-
--> tests/ui/single_match.rs:376:5
226+
--> tests/ui/single_match.rs:383:5
218227
|
219228
LL | / match CONST_I32 {
220229
LL | | CONST_I32 => println!(),
@@ -223,7 +232,7 @@ LL | | }
223232
| |_____^ help: try: `println!();`
224233

225234
error: this pattern is irrefutable, `match` is useless
226-
--> tests/ui/single_match.rs:382:5
235+
--> tests/ui/single_match.rs:389:5
227236
|
228237
LL | / match i {
229238
LL | | i => {
@@ -243,7 +252,7 @@ LL + }
243252
|
244253

245254
error: this pattern is irrefutable, `match` is useless
246-
--> tests/ui/single_match.rs:390:5
255+
--> tests/ui/single_match.rs:397:5
247256
|
248257
LL | / match i {
249258
LL | | i => {},
@@ -252,7 +261,7 @@ LL | | }
252261
| |_____^ help: `match` expression can be removed
253262

254263
error: this pattern is irrefutable, `match` is useless
255-
--> tests/ui/single_match.rs:395:5
264+
--> tests/ui/single_match.rs:402:5
256265
|
257266
LL | / match i {
258267
LL | | i => (),
@@ -261,13 +270,13 @@ LL | | }
261270
| |_____^ help: `match` expression can be removed
262271

263272
error: this pattern is irrefutable, `match` is useless
264-
--> tests/ui/single_match.rs:400:5
273+
--> tests/ui/single_match.rs:407:5
265274
|
266275
LL | / match CONST_I32 {
267276
LL | | CONST_I32 => println!(),
268277
LL | | _ => {},
269278
LL | | }
270279
| |_____^ help: try: `println!();`
271280

272-
error: aborting due to 25 previous errors
281+
error: aborting due to 26 previous errors
273282

0 commit comments

Comments
 (0)