Skip to content

Commit d4b1e62

Browse files
committed
applicability: MachineApplicable if the suggestion is complete, otherwise MaybeIncorrect
1 parent f4242ee commit d4b1e62

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

clippy_lints/src/loops/manual_flatten.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ 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};
5+
use clippy_utils::source::{HasSession, indent_of, reindent_multiline, snippet_with_applicability};
66
use clippy_utils::visitors::is_local_used;
7-
use clippy_utils::{higher, path_to_local_id, peel_blocks_with_stmt};
7+
use clippy_utils::{higher, path_to_local_id, peel_blocks_with_stmt, span_contains_comment};
88
use rustc_errors::Applicability;
99
use rustc_hir::def::{DefKind, Res};
1010
use rustc_hir::{Expr, Pat, PatKind};
@@ -47,8 +47,11 @@ pub(super) fn check<'tcx>(
4747
let msg =
4848
format!("unnecessary `if let` since only the `{if_let_type}` variant of the iterator element is used");
4949

50-
// Prepare the help message
51-
let mut applicability = Applicability::MaybeIncorrect;
50+
let mut applicability = if span_contains_comment(cx.sess().source_map(), body.span) {
51+
Applicability::MaybeIncorrect
52+
} else {
53+
Applicability::MachineApplicable
54+
};
5255
let arg_snippet = make_iterator_snippet(cx, arg, &mut applicability);
5356
let copied = match cx.typeck_results().expr_ty(let_expr).kind() {
5457
ty::Ref(_, inner, _) => match inner.kind() {

tests/ui/manual_flatten.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@ fn main() {
117117
}
118118
}
119119

120+
// This should trigger the lint, but the applicability is `MaybeIncorrect`
121+
let z = vec![Some(1), Some(2), Some(3)];
122+
for n in z.into_iter().flatten() {
123+
println!("{:?}", n);
124+
}
125+
120126
run_unformatted_tests();
121127
}
122128

tests/ui/manual_flatten.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,17 @@ fn main() {
149149
}
150150
}
151151

152+
// This should trigger the lint, but the applicability is `MaybeIncorrect`
153+
let z = vec![Some(1), Some(2), Some(3)];
154+
for n in z {
155+
//~^ manual_flatten
156+
157+
if let Some(n) = n {
158+
println!("{:?}", n);
159+
}
160+
// foo
161+
}
162+
152163
run_unformatted_tests();
153164
}
154165

tests/ui/manual_flatten.stderr

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,32 @@ LL + }
202202
|
203203

204204
error: unnecessary `if let` since only the `Some` variant of the iterator element is used
205-
--> tests/ui/manual_flatten.rs:158:5
205+
--> tests/ui/manual_flatten.rs:154:5
206+
|
207+
LL | / for n in z {
208+
LL | |
209+
LL | |
210+
LL | | if let Some(n) = n {
211+
... |
212+
LL | | }
213+
| |_____^
214+
|
215+
help: try `.flatten()` and remove the `if let` statement in the for loop
216+
--> tests/ui/manual_flatten.rs:157:9
217+
|
218+
LL | / if let Some(n) = n {
219+
LL | | println!("{:?}", n);
220+
LL | | }
221+
| |_________^
222+
help: try
223+
|
224+
LL ~ for n in z.into_iter().flatten() {
225+
LL + println!("{:?}", n);
226+
LL + }
227+
|
228+
229+
error: unnecessary `if let` since only the `Some` variant of the iterator element is used
230+
--> tests/ui/manual_flatten.rs:169:5
206231
|
207232
LL | / for n in vec![
208233
LL | |
@@ -213,7 +238,7 @@ LL | | }
213238
| |_____^
214239
|
215240
help: try `.flatten()` and remove the `if let` statement in the for loop
216-
--> tests/ui/manual_flatten.rs:165:9
241+
--> tests/ui/manual_flatten.rs:176:9
217242
|
218243
LL | / if let Some(n) = n {
219244
LL | | println!("{:?}", n);
@@ -229,5 +254,5 @@ LL + println!("{:?}", n);
229254
LL + }
230255
|
231256

232-
error: aborting due to 9 previous errors
257+
error: aborting due to 10 previous errors
233258

0 commit comments

Comments
 (0)