Skip to content

Commit 1c14af8

Browse files
authored
clean-up single_match a bit (#15641)
changelog: none
2 parents cc5a01d + 05156f4 commit 1c14af8

File tree

4 files changed

+31
-26
lines changed

4 files changed

+31
-26
lines changed

clippy_lints/src/matches/single_match.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,16 @@ use super::{MATCH_BOOL, SINGLE_MATCH, SINGLE_MATCH_ELSE};
2222
/// span, e.g. a string literal `"//"`, but we know that this isn't the case for empty
2323
/// match arms.
2424
fn empty_arm_has_comment(cx: &LateContext<'_>, span: Span) -> bool {
25-
if let Some(ff) = span.get_source_range(cx)
26-
&& let Some(text) = ff.as_str()
27-
{
28-
text.as_bytes().windows(2).any(|w| w == b"//" || w == b"/*")
29-
} else {
30-
false
31-
}
25+
span.check_source_text(cx, |text| text.as_bytes().windows(2).any(|w| w == b"//" || w == b"/*"))
3226
}
3327

34-
#[rustfmt::skip]
35-
pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, ex: &'tcx Expr<'_>, arms: &'tcx [Arm<'_>], expr: &'tcx Expr<'_>, contains_comments: bool) {
28+
pub(crate) fn check<'tcx>(
29+
cx: &LateContext<'tcx>,
30+
ex: &'tcx Expr<'_>,
31+
arms: &'tcx [Arm<'_>],
32+
expr: &'tcx Expr<'_>,
33+
contains_comments: bool,
34+
) {
3635
if let [arm1, arm2] = arms
3736
&& !arms.iter().any(|arm| arm.guard.is_some() || arm.pat.span.from_expansion())
3837
&& !expr.span.from_expansion()
@@ -224,13 +223,13 @@ enum PatState<'a> {
224223
Wild,
225224
/// A std enum we know won't be extended. Tracks the states of each variant separately.
226225
///
227-
/// This is not used for `Option` since it uses the current pattern to track it's state.
226+
/// This is not used for `Option` since it uses the current pattern to track its state.
228227
StdEnum(&'a mut [PatState<'a>]),
229228
/// Either the initial state for a pattern or a non-std enum. There is currently no need to
230229
/// distinguish these cases.
231230
///
232231
/// For non-std enums there's no need to track the state of sub-patterns as the state of just
233-
/// this pattern on it's own is enough for linting. Consider two cases:
232+
/// this pattern on its own is enough for linting. Consider two cases:
234233
/// * This enum has no wild match. This case alone is enough to determine we can lint.
235234
/// * This enum has a wild match and therefore all sub-patterns also have a wild match.
236235
///
@@ -378,7 +377,11 @@ impl<'a> PatState<'a> {
378377
self.add_pat(cx, pat)
379378
},
380379
PatKind::Tuple([sub_pat], pos)
381-
if pos.as_opt_usize().is_none() || cx.typeck.pat_ty(pat).tuple_fields().len() == 1 =>
380+
// `pat` looks like `(sub_pat)`, without a `..` -- has only one sub-pattern
381+
if pos.as_opt_usize().is_none()
382+
// `pat` looks like `(sub_pat, ..)` or `(.., sub_pat)`, but its type is a unary tuple,
383+
// so it still only has one sub-pattern
384+
|| cx.typeck.pat_ty(pat).tuple_fields().len() == 1 =>
382385
{
383386
self.add_pat(cx, sub_pat)
384387
},

tests/ui/single_match.fixed

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ fn main() {
218218
};
219219
}
220220

221-
fn issue_10808(bar: Option<i32>) {
221+
fn issue10808(bar: Option<i32>) {
222222
if let Some(v) = bar { unsafe {
223223
let r = &v as *const i32;
224224
println!("{}", *r);
@@ -330,6 +330,7 @@ pub struct Data([u8; 4]);
330330
const DATA: Data = Data([1, 2, 3, 4]);
331331
const CONST_I32: i32 = 1;
332332

333+
// https://github.com/rust-lang/rust-clippy/issues/13012
333334
fn irrefutable_match() {
334335
println!();
335336
//~^^^^ single_match
@@ -367,7 +368,7 @@ fn irrefutable_match() {
367368
//~| NOTE: you might want to preserve the comments from inside the `match`
368369
}
369370

370-
fn issue_14493() {
371+
fn issue14493() {
371372
macro_rules! mac {
372373
(some) => {
373374
Some(42)

tests/ui/single_match.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ fn main() {
269269
};
270270
}
271271

272-
fn issue_10808(bar: Option<i32>) {
272+
fn issue10808(bar: Option<i32>) {
273273
match bar {
274274
Some(v) => unsafe {
275275
let r = &v as *const i32;
@@ -397,6 +397,7 @@ pub struct Data([u8; 4]);
397397
const DATA: Data = Data([1, 2, 3, 4]);
398398
const CONST_I32: i32 = 1;
399399

400+
// https://github.com/rust-lang/rust-clippy/issues/13012
400401
fn irrefutable_match() {
401402
match DATA {
402403
DATA => println!(),
@@ -462,7 +463,7 @@ fn irrefutable_match() {
462463
//~| NOTE: you might want to preserve the comments from inside the `match`
463464
}
464465

465-
fn issue_14493() {
466+
fn issue14493() {
466467
macro_rules! mac {
467468
(some) => {
468469
Some(42)

tests/ui/single_match.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ LL | | }
225225
| |_____^ help: try: `if &s[0..3] == b"foo" { println!() }`
226226

227227
error: this pattern is irrefutable, `match` is useless
228-
--> tests/ui/single_match.rs:401:5
228+
--> tests/ui/single_match.rs:402:5
229229
|
230230
LL | / match DATA {
231231
LL | | DATA => println!(),
@@ -234,7 +234,7 @@ LL | | }
234234
| |_____^ help: try: `println!();`
235235

236236
error: this pattern is irrefutable, `match` is useless
237-
--> tests/ui/single_match.rs:407:5
237+
--> tests/ui/single_match.rs:408:5
238238
|
239239
LL | / match CONST_I32 {
240240
LL | | CONST_I32 => println!(),
@@ -243,7 +243,7 @@ LL | | }
243243
| |_____^ help: try: `println!();`
244244

245245
error: this pattern is irrefutable, `match` is useless
246-
--> tests/ui/single_match.rs:414:5
246+
--> tests/ui/single_match.rs:415:5
247247
|
248248
LL | / match i {
249249
LL | | i => {
@@ -263,7 +263,7 @@ LL + }
263263
|
264264

265265
error: this pattern is irrefutable, `match` is useless
266-
--> tests/ui/single_match.rs:423:5
266+
--> tests/ui/single_match.rs:424:5
267267
|
268268
LL | / match i {
269269
LL | | i => {},
@@ -272,7 +272,7 @@ LL | | }
272272
| |_____^ help: `match` expression can be removed
273273

274274
error: this pattern is irrefutable, `match` is useless
275-
--> tests/ui/single_match.rs:429:5
275+
--> tests/ui/single_match.rs:430:5
276276
|
277277
LL | / match i {
278278
LL | | i => (),
@@ -281,7 +281,7 @@ LL | | }
281281
| |_____^ help: `match` expression can be removed
282282

283283
error: this pattern is irrefutable, `match` is useless
284-
--> tests/ui/single_match.rs:435:5
284+
--> tests/ui/single_match.rs:436:5
285285
|
286286
LL | / match CONST_I32 {
287287
LL | | CONST_I32 => println!(),
@@ -290,7 +290,7 @@ LL | | }
290290
| |_____^ help: try: `println!();`
291291

292292
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
293-
--> tests/ui/single_match.rs:443:5
293+
--> tests/ui/single_match.rs:444:5
294294
|
295295
LL | / match x.pop() {
296296
LL | | // bla
@@ -302,7 +302,7 @@ LL | | }
302302
= note: you might want to preserve the comments from inside the `match`
303303

304304
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
305-
--> tests/ui/single_match.rs:452:5
305+
--> tests/ui/single_match.rs:453:5
306306
|
307307
LL | / match x.pop() {
308308
LL | | // bla
@@ -322,7 +322,7 @@ LL + }
322322
|
323323

324324
error: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
325-
--> tests/ui/single_match.rs:478:5
325+
--> tests/ui/single_match.rs:479:5
326326
|
327327
LL | / match mac!(some) {
328328
LL | | Some(u) => println!("{u}"),
@@ -331,7 +331,7 @@ LL | | }
331331
| |_____^ help: try: `if let Some(u) = mac!(some) { println!("{u}") }`
332332

333333
error: you seem to be trying to use `match` for an equality check. Consider using `if`
334-
--> tests/ui/single_match.rs:486:5
334+
--> tests/ui/single_match.rs:487:5
335335
|
336336
LL | / match mac!(str) {
337337
LL | | "foo" => println!("eq"),

0 commit comments

Comments
 (0)