Skip to content

Commit d254020

Browse files
committed
misc
1 parent 5ac9657 commit d254020

File tree

3 files changed

+32
-32
lines changed

3 files changed

+32
-32
lines changed

clippy_lints/src/matches/collapsible_match.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,17 @@ fn check_arm<'tcx>(
5050
if let Some(inner) = IfLetOrMatch::parse(cx, inner_expr)
5151
&& let Some((inner_scrutinee, inner_then_pat, inner_else_body)) = match inner {
5252
IfLetOrMatch::IfLet(scrutinee, pat, _, els, _) => Some((scrutinee, pat, els)),
53-
IfLetOrMatch::Match(scrutinee, arms, ..) => if arms.len() == 2 && arms.iter().all(|a| a.guard.is_none())
54-
// if there are more than two arms, collapsing would be non-trivial
55-
// one of the arms must be "wild-like"
56-
&& let Some(wild_idx) = arms.iter().rposition(|a| arm_is_wild_like(cx, a))
57-
{
58-
let (then, els) = (&arms[1 - wild_idx], &arms[wild_idx]);
59-
Some((scrutinee, then.pat, Some(els.body)))
60-
} else {
61-
None
53+
IfLetOrMatch::Match(scrutinee, arms, ..) => {
54+
if arms.len() == 2 && arms.iter().all(|a| a.guard.is_none())
55+
// if there are more than two arms, collapsing would be non-trivial
56+
// one of the arms must be "wild-like"
57+
&& let Some(wild_idx) = arms.iter().rposition(|a| arm_is_wild_like(cx, a))
58+
{
59+
let (then, els) = (&arms[1 - wild_idx], &arms[wild_idx]);
60+
Some((scrutinee, then.pat, Some(els.body)))
61+
} else {
62+
None
63+
}
6264
},
6365
}
6466
&& outer_pat.span.eq_ctxt(inner_scrutinee.span)
@@ -68,18 +70,16 @@ fn check_arm<'tcx>(
6870
&& !pat_contains_disallowed_or(cx, inner_then_pat, msrv)
6971
// the binding must come from the pattern of the containing match arm
7072
// ..<local>.. => match <local> { .. }
71-
&& let (Some(binding_span), is_innermost_parent_pat_struct)
72-
= find_pat_binding_and_is_innermost_parent_pat_struct(outer_pat, binding_id)
73+
&& let (Some(binding_span), is_innermost_parent_pat_struct) =
74+
find_pat_binding_and_is_innermost_parent_pat_struct(outer_pat, binding_id)
7375
// the "else" branches must be equal
7476
&& match (outer_else_body, inner_else_body) {
7577
(None, None) => true,
7678
(None, Some(e)) | (Some(e), None) => is_unit_expr(e),
7779
(Some(a), Some(b)) => SpanlessEq::new(cx).eq_expr(a, b),
7880
}
7981
// the binding must not be used in the if guard
80-
&& outer_guard.is_none_or(
81-
|e| !is_local_used(cx, e, binding_id)
82-
)
82+
&& outer_guard.is_none_or(|e| !is_local_used(cx, e, binding_id))
8383
// ...or anywhere in the inner expression
8484
&& match inner {
8585
IfLetOrMatch::IfLet(_, _, body, els, _) => {

tests/ui/collapsible_match.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -304,18 +304,6 @@ pub fn test_2(x: Issue9647) {
304304
}
305305
}
306306

307-
// https://github.com/rust-lang/rust-clippy/issues/14281
308-
fn lint_emitted_at_right_node(opt: Option<Result<u64, String>>) {
309-
let n = match opt {
310-
#[expect(clippy::collapsible_match)]
311-
Some(n) => match n {
312-
Ok(n) => n,
313-
_ => return,
314-
},
315-
None => return,
316-
};
317-
}
318-
319307
pub fn issue_14155() {
320308
let mut arr = ["a", "b", "c"];
321309
if let Some(last) = arr.last() {
@@ -357,6 +345,18 @@ pub fn issue_14155() {
357345
}
358346
}
359347

348+
// https://github.com/rust-lang/rust-clippy/issues/14281
349+
fn lint_emitted_at_right_node(opt: Option<Result<u64, String>>) {
350+
let n = match opt {
351+
#[expect(clippy::collapsible_match)]
352+
Some(n) => match n {
353+
Ok(n) => n,
354+
_ => return,
355+
},
356+
None => return,
357+
};
358+
}
359+
360360
fn make<T>() -> T {
361361
unimplemented!()
362362
}

tests/ui/collapsible_match.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ LL | if let Some(u) = a {
251251
| ^^^^^^^ with this pattern
252252

253253
error: this `match` can be collapsed into the outer `if let`
254-
--> tests/ui/collapsible_match.rs:322:9
254+
--> tests/ui/collapsible_match.rs:310:9
255255
|
256256
LL | / match *last {
257257
LL | |
@@ -263,7 +263,7 @@ LL | | }
263263
| |_________^
264264
|
265265
help: the outer pattern can be modified to include the inner pattern
266-
--> tests/ui/collapsible_match.rs:321:17
266+
--> tests/ui/collapsible_match.rs:309:17
267267
|
268268
LL | if let Some(last) = arr.last() {
269269
| ^^^^ ---------- use: `arr.last().copied()`
@@ -274,7 +274,7 @@ LL | "a" | "b" => {
274274
| ^^^^^^^^^ with this pattern
275275

276276
error: this `match` can be collapsed into the outer `if let`
277-
--> tests/ui/collapsible_match.rs:332:9
277+
--> tests/ui/collapsible_match.rs:320:9
278278
|
279279
LL | / match &last {
280280
LL | |
@@ -286,7 +286,7 @@ LL | | }
286286
| |_________^
287287
|
288288
help: the outer pattern can be modified to include the inner pattern
289-
--> tests/ui/collapsible_match.rs:331:17
289+
--> tests/ui/collapsible_match.rs:319:17
290290
|
291291
LL | if let Some(last) = arr.last() {
292292
| ^^^^ ---------- use: `arr.last().as_ref()`
@@ -297,7 +297,7 @@ LL | &&"a" | &&"b" => {
297297
| ^^^^^^^^^^^^^ with this pattern
298298

299299
error: this `match` can be collapsed into the outer `if let`
300-
--> tests/ui/collapsible_match.rs:342:9
300+
--> tests/ui/collapsible_match.rs:330:9
301301
|
302302
LL | / match &mut last {
303303
LL | |
@@ -309,7 +309,7 @@ LL | | }
309309
| |_________^
310310
|
311311
help: the outer pattern can be modified to include the inner pattern
312-
--> tests/ui/collapsible_match.rs:341:17
312+
--> tests/ui/collapsible_match.rs:329:17
313313
|
314314
LL | if let Some(mut last) = arr.last_mut() {
315315
| ^^^^^^^^ -------------- use: `arr.last_mut().as_mut()`

0 commit comments

Comments
 (0)