Skip to content

Commit f25207a

Browse files
committed
replace for pat to if_let pat
1 parent ac06a73 commit f25207a

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

clippy_lints/src/loops/manual_flatten.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub(super) fn check<'tcx>(
2929
&& let PatKind::Binding(_, pat_hir_id, _, _) = pat.kind
3030
&& path_to_local_id(let_expr, pat_hir_id)
3131
// Ensure the `if let` statement is for the `Some` variant of `Option` or the `Ok` variant of `Result`
32-
&& let PatKind::TupleStruct(ref qpath, _, _) = let_pat.kind
32+
&& let PatKind::TupleStruct(ref qpath, let_pats, _) = let_pat.kind
3333
&& let Res::Def(DefKind::Ctor(..), ctor_id) = cx.qpath_res(qpath, let_pat.hir_id)
3434
&& let Some(variant_id) = cx.tcx.opt_parent(ctor_id)
3535
&& let some_ctor = cx.tcx.lang_items().option_some_variant() == Some(variant_id)
@@ -57,15 +57,27 @@ pub(super) fn check<'tcx>(
5757

5858
let help_msg = "try `.flatten()` and remove the `if let` statement in the for loop";
5959

60-
let body_snip =
60+
let pat_snippet = if let_pats.is_empty() {
61+
"_".to_string()
62+
} else {
63+
snippet_with_applicability(
64+
cx,
65+
let_pats.first().unwrap().span.source_callsite(),
66+
"_",
67+
&mut applicability,
68+
)
69+
.to_string()
70+
};
71+
let body_snippet =
6172
snippet_with_applicability(cx, if_then.span.source_callsite(), "[body]", &mut applicability).to_string();
6273
let suggestions = vec![
6374
// flatten the iterator
6475
(arg.span, format!("{arg_snippet}{copied}.flatten()")),
76+
(pat.span, pat_snippet),
6577
// remove the `if let` statement
6678
(
6779
body.span,
68-
reindent_multiline(&body_snip, true, indent_of(cx, body.span)),
80+
reindent_multiline(&body_snippet, true, indent_of(cx, body.span)),
6981
),
7082
];
7183

tests/ui/manual_flatten.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ LL | | }
2020
= help: to override `-D warnings` add `#[allow(clippy::manual_flatten)]`
2121
help: try
2222
|
23-
LL ~ for n in x.into_iter().flatten() {
23+
LL ~ for y in x.into_iter().flatten() {
2424
LL + println!("{}", y);
2525
LL + }
2626
|
@@ -121,7 +121,7 @@ LL | | }
121121
| |_________^
122122
help: try
123123
|
124-
LL ~ for n in z.flatten() {
124+
LL ~ for m in z.flatten() {
125125
LL + println!("{}", m);
126126
LL + }
127127
|

0 commit comments

Comments
 (0)