Skip to content

Commit e9606eb

Browse files
committed
flatten and remove the if let pat
1 parent f00c58b commit e9606eb

File tree

2 files changed

+72
-42
lines changed

2 files changed

+72
-42
lines changed

clippy_lints/src/loops/manual_flatten.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::MANUAL_FLATTEN;
22
use super::utils::make_iterator_snippet;
33
use clippy_utils::diagnostics::span_lint_and_then;
44
use clippy_utils::msrvs::{self, Msrv};
5+
use clippy_utils::source::{indent_of, reindent_multiline, snippet_with_applicability};
56
use clippy_utils::visitors::is_local_used;
67
use clippy_utils::{higher, path_to_local_id, peel_blocks_with_stmt};
78
use rustc_errors::Applicability;
@@ -54,20 +55,23 @@ pub(super) fn check<'tcx>(
5455
_ => "",
5556
};
5657

57-
let sugg = format!("{arg_snippet}{copied}.flatten()");
58+
let help_msg = "try `.flatten()` and remove the `if let` statement in the for loop";
5859

59-
// If suggestion is not a one-liner, it won't be shown inline within the error message. In that
60-
// case, it will be shown in the extra `help` message at the end, which is why the first
61-
// `help_msg` needs to refer to the correct relative position of the suggestion.
62-
let help_msg = if sugg.contains('\n') {
63-
"remove the `if let` statement in the for loop and then..."
64-
} else {
65-
"...and remove the `if let` statement in the for loop"
66-
};
60+
let body_snip =
61+
snippet_with_applicability(cx, if_then.span.source_callsite(), "[body]", &mut applicability).to_string();
62+
let suggestions = vec![
63+
// flatten the iterator
64+
(arg.span, format!("{arg_snippet}{copied}.flatten()")),
65+
// remove the `if let` statement
66+
(
67+
body.span,
68+
reindent_multiline(&body_snip, true, indent_of(cx, body.span)),
69+
),
70+
];
6771

6872
span_lint_and_then(cx, MANUAL_FLATTEN, span, msg, |diag| {
69-
diag.span_suggestion(arg.span, "try", sugg, applicability);
7073
diag.span_help(inner_expr.span, help_msg);
74+
diag.multipart_suggestion("try", suggestions, applicability);
7175
});
7276
}
7377
}

tests/ui/manual_flatten.stderr

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
error: unnecessary `if let` since only the `Some` variant of the iterator element is used
22
--> tests/ui/manual_flatten.rs:7:5
33
|
4-
LL | for n in x {
5-
| ^ - help: try: `x.into_iter().flatten()`
6-
| _____|
7-
| |
4+
LL | / for n in x {
85
LL | |
96
LL | |
107
LL | | if let Some(y) = n {
@@ -21,14 +18,17 @@ LL | | }
2118
| |_________^
2219
= note: `-D clippy::manual-flatten` implied by `-D warnings`
2320
= help: to override `-D warnings` add `#[allow(clippy::manual_flatten)]`
21+
help: try
22+
|
23+
LL ~ for n in x.into_iter().flatten() {
24+
LL + println!("{}", y);
25+
LL + }
26+
|
2427

2528
error: unnecessary `if let` since only the `Ok` variant of the iterator element is used
2629
--> tests/ui/manual_flatten.rs:17:5
2730
|
28-
LL | for n in y.clone() {
29-
| ^ --------- help: try: `y.clone().into_iter().flatten()`
30-
| _____|
31-
| |
31+
LL | / for n in y.clone() {
3232
LL | |
3333
LL | |
3434
LL | | if let Ok(n) = n {
@@ -44,14 +44,17 @@ LL | / if let Ok(n) = n {
4444
LL | | println!("{}", n);
4545
LL | | };
4646
| |_________^
47+
help: try
48+
|
49+
LL ~ for n in y.clone().into_iter().flatten() {
50+
LL + println!("{}", n);
51+
LL + }
52+
|
4753

4854
error: unnecessary `if let` since only the `Ok` variant of the iterator element is used
4955
--> tests/ui/manual_flatten.rs:26:5
5056
|
51-
LL | for n in &y {
52-
| ^ -- help: try: `y.iter().flatten()`
53-
| _____|
54-
| |
57+
LL | / for n in &y {
5558
LL | |
5659
LL | |
5760
LL | | if let Ok(n) = n {
@@ -66,14 +69,17 @@ LL | / if let Ok(n) = n {
6669
LL | | println!("{}", n);
6770
LL | | }
6871
| |_________^
72+
help: try
73+
|
74+
LL ~ for n in y.iter().flatten() {
75+
LL + println!("{}", n);
76+
LL + }
77+
|
6978

7079
error: unnecessary `if let` since only the `Ok` variant of the iterator element is used
7180
--> tests/ui/manual_flatten.rs:36:5
7281
|
73-
LL | for n in z {
74-
| ^ - help: try: `z.iter().flatten()`
75-
| _____|
76-
| |
82+
LL | / for n in z {
7783
LL | |
7884
LL | |
7985
LL | | if let Ok(n) = n {
@@ -88,14 +94,17 @@ LL | / if let Ok(n) = n {
8894
LL | | println!("{}", n);
8995
LL | | }
9096
| |_________^
97+
help: try
98+
|
99+
LL ~ for n in z.iter().flatten() {
100+
LL + println!("{}", n);
101+
LL + }
102+
|
91103

92104
error: unnecessary `if let` since only the `Some` variant of the iterator element is used
93105
--> tests/ui/manual_flatten.rs:47:5
94106
|
95-
LL | for n in z {
96-
| ^ - help: try: `z.flatten()`
97-
| _____|
98-
| |
107+
LL | / for n in z {
99108
LL | |
100109
LL | |
101110
LL | | if let Some(m) = n {
@@ -110,14 +119,17 @@ LL | / if let Some(m) = n {
110119
LL | | println!("{}", m);
111120
LL | | }
112121
| |_________^
122+
help: try
123+
|
124+
LL ~ for n in z.flatten() {
125+
LL + println!("{}", m);
126+
LL + }
127+
|
113128

114129
error: unnecessary `if let` since only the `Some` variant of the iterator element is used
115130
--> tests/ui/manual_flatten.rs:82:5
116131
|
117-
LL | for n in &vec_of_ref {
118-
| ^ ----------- help: try: `vec_of_ref.iter().copied().flatten()`
119-
| _____|
120-
| |
132+
LL | / for n in &vec_of_ref {
121133
LL | |
122134
LL | |
123135
LL | | if let Some(n) = n {
@@ -132,14 +144,17 @@ LL | / if let Some(n) = n {
132144
LL | | println!("{:?}", n);
133145
LL | | }
134146
| |_________^
147+
help: try
148+
|
149+
LL ~ for n in vec_of_ref.iter().copied().flatten() {
150+
LL + println!("{:?}", n);
151+
LL + }
152+
|
135153

136154
error: unnecessary `if let` since only the `Some` variant of the iterator element is used
137155
--> tests/ui/manual_flatten.rs:91:5
138156
|
139-
LL | for n in vec_of_ref {
140-
| ^ ---------- help: try: `vec_of_ref.iter().copied().flatten()`
141-
| _____|
142-
| |
157+
LL | / for n in vec_of_ref {
143158
LL | |
144159
LL | |
145160
LL | | if let Some(n) = n {
@@ -154,14 +169,17 @@ LL | / if let Some(n) = n {
154169
LL | | println!("{:?}", n);
155170
LL | | }
156171
| |_________^
172+
help: try
173+
|
174+
LL ~ for n in vec_of_ref.iter().copied().flatten() {
175+
LL + println!("{:?}", n);
176+
LL + }
177+
|
157178

158179
error: unnecessary `if let` since only the `Some` variant of the iterator element is used
159180
--> tests/ui/manual_flatten.rs:100:5
160181
|
161-
LL | for n in slice_of_ref {
162-
| ^ ------------ help: try: `slice_of_ref.iter().copied().flatten()`
163-
| _____|
164-
| |
182+
LL | / for n in slice_of_ref {
165183
LL | |
166184
LL | |
167185
LL | | if let Some(n) = n {
@@ -176,6 +194,12 @@ LL | / if let Some(n) = n {
176194
LL | | println!("{:?}", n);
177195
LL | | }
178196
| |_________^
197+
help: try
198+
|
199+
LL ~ for n in slice_of_ref.iter().copied().flatten() {
200+
LL + println!("{:?}", n);
201+
LL + }
202+
|
179203

180204
error: unnecessary `if let` since only the `Some` variant of the iterator element is used
181205
--> tests/ui/manual_flatten.rs:132:5
@@ -201,6 +225,8 @@ LL | for n in vec![
201225
...
202226
LL | Some(3)
203227
LL ~ ].iter().flatten() {
228+
LL + println!("{:?}", n);
229+
LL + }
204230
|
205231

206232
error: aborting due to 9 previous errors

0 commit comments

Comments
 (0)