Skip to content

Commit 85ae0cd

Browse files
committed
also suggest in the multi-line case
This used to be a workaround for a rustc diagnostics issue which has since been resolved
1 parent cadfdcb commit 85ae0cd

6 files changed

Lines changed: 62 additions & 51 deletions

File tree

clippy_lints/src/methods/filter_map_next.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,15 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, arg:
1818
expr.span,
1919
"called `filter_map(..).next()` on an `Iterator`",
2020
|diag| {
21-
let sugg_msg = "use `.find_map(..)` instead";
22-
2321
let mut app = Applicability::MachineApplicable;
2422
let filter_snippet = snippet_with_applicability(cx, arg.span, "..", &mut app);
25-
if filter_snippet.lines().count() <= 1 {
26-
let iter_snippet = snippet_with_applicability(cx, recv.span, "..", &mut app);
27-
diag.span_suggestion_verbose(
28-
expr.span,
29-
sugg_msg,
30-
format!("{iter_snippet}.find_map({filter_snippet})"),
31-
app,
32-
);
33-
} else {
34-
diag.help(sugg_msg);
35-
}
23+
let iter_snippet = snippet_with_applicability(cx, recv.span, "..", &mut app);
24+
diag.span_suggestion_verbose(
25+
expr.span,
26+
"use `.find_map(..)` instead",
27+
format!("{iter_snippet}.find_map({filter_snippet})"),
28+
app,
29+
);
3630
},
3731
);
3832
}

tests/ui/filter_map_next.fixed

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ fn main() {
66
let element: Option<i32> = a.iter().find_map(|s| s.parse().ok());
77
//~^ filter_map_next
88
assert_eq!(element, Some(1));
9+
10+
#[rustfmt::skip]
11+
let _: Option<u32> = vec![1, 2, 3, 4, 5, 6]
12+
//~^ filter_map_next
13+
14+
15+
.into_iter().find_map(|x| {
16+
if x == 2 {
17+
Some(x * 2)
18+
} else {
19+
None
20+
}
21+
});
922
}
1023

1124
#[clippy::msrv = "1.29"]

tests/ui/filter_map_next.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ fn main() {
66
let element: Option<i32> = a.iter().filter_map(|s| s.parse().ok()).next();
77
//~^ filter_map_next
88
assert_eq!(element, Some(1));
9+
10+
#[rustfmt::skip]
11+
let _: Option<u32> = vec![1, 2, 3, 4, 5, 6]
12+
//~^ filter_map_next
13+
14+
15+
.into_iter()
16+
.filter_map(|x| {
17+
if x == 2 {
18+
Some(x * 2)
19+
} else {
20+
None
21+
}
22+
})
23+
.next();
924
}
1025

1126
#[clippy::msrv = "1.29"]

tests/ui/filter_map_next.stderr

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,32 @@ LL + let element: Option<i32> = a.iter().find_map(|s| s.parse().ok());
1313
|
1414

1515
error: called `filter_map(..).next()` on an `Iterator`
16-
--> tests/ui/filter_map_next.rs:20:26
16+
--> tests/ui/filter_map_next.rs:11:26
17+
|
18+
LL | let _: Option<u32> = vec![1, 2, 3, 4, 5, 6]
19+
| __________________________^
20+
... |
21+
LL | | })
22+
LL | | .next();
23+
| |_______________^
24+
|
25+
help: use `.find_map(..)` instead
26+
|
27+
LL ~ let _: Option<u32> = vec![1, 2, 3, 4, 5, 6]
28+
LL +
29+
LL +
30+
LL +
31+
LL + .into_iter().find_map(|x| {
32+
LL + if x == 2 {
33+
LL + Some(x * 2)
34+
LL + } else {
35+
LL + None
36+
LL + }
37+
LL ~ });
38+
|
39+
40+
error: called `filter_map(..).next()` on an `Iterator`
41+
--> tests/ui/filter_map_next.rs:35:26
1742
|
1843
LL | let _: Option<i32> = a.iter().filter_map(|s| s.parse().ok()).next();
1944
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -24,5 +49,5 @@ LL - let _: Option<i32> = a.iter().filter_map(|s| s.parse().ok()).next();
2449
LL + let _: Option<i32> = a.iter().find_map(|s| s.parse().ok());
2550
|
2651

27-
error: aborting due to 2 previous errors
52+
error: aborting due to 3 previous errors
2853

tests/ui/filter_map_next_unfixable.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/ui/filter_map_next_unfixable.stderr

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)