Skip to content

Commit 8d917ab

Browse files
committed
extract suggestion out of the main message
1. In the singe-line case, we switch to verbose suggestions, otherwise the suggestion line gets too long due to the longer message. 2. In the multi-line case, we use span-less help, otherwise the whole multiline span gets highlighted the second time. 3. Also make the suggestion message more concise.
1 parent c38bf3d commit 8d917ab

3 files changed

Lines changed: 24 additions & 9 deletions

File tree

clippy_lints/src/methods/filter_map_next.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, arg:
1616
cx,
1717
FILTER_MAP_NEXT,
1818
expr.span,
19-
"called `filter_map(..).next()` on an `Iterator`. \
20-
This is more succinctly expressed by calling `.find_map(..)` instead",
19+
"called `filter_map(..).next()` on an `Iterator`",
2120
|diag| {
21+
let sugg_msg = "use `.find_map(..)` instead";
22+
2223
let filter_snippet = snippet(cx, arg.span, "..");
2324
if filter_snippet.lines().count() <= 1 {
2425
let iter_snippet = snippet(cx, recv.span, "..");
25-
diag.span_suggestion(
26+
diag.span_suggestion_verbose(
2627
expr.span,
27-
"try",
28+
sugg_msg,
2829
format!("{iter_snippet}.find_map({filter_snippet})"),
2930
Applicability::MachineApplicable,
3031
);
32+
} else {
33+
diag.help(sugg_msg);
3134
}
3235
},
3336
);

tests/ui/filter_map_next.stderr

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
1-
error: called `filter_map(..).next()` on an `Iterator`. This is more succinctly expressed by calling `.find_map(..)` instead
1+
error: called `filter_map(..).next()` on an `Iterator`
22
--> tests/ui/filter_map_next.rs:6:32
33
|
44
LL | let element: Option<i32> = a.iter().filter_map(|s| s.parse().ok()).next();
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `a.iter().find_map(|s| s.parse().ok())`
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::filter-map-next` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::filter_map_next)]`
9+
help: use `.find_map(..)` instead
10+
|
11+
LL - let element: Option<i32> = a.iter().filter_map(|s| s.parse().ok()).next();
12+
LL + let element: Option<i32> = a.iter().find_map(|s| s.parse().ok());
13+
|
914

10-
error: called `filter_map(..).next()` on an `Iterator`. This is more succinctly expressed by calling `.find_map(..)` instead
15+
error: called `filter_map(..).next()` on an `Iterator`
1116
--> tests/ui/filter_map_next.rs:20:26
1217
|
1318
LL | let _: Option<i32> = a.iter().filter_map(|s| s.parse().ok()).next();
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `a.iter().find_map(|s| s.parse().ok())`
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
|
21+
help: use `.find_map(..)` instead
22+
|
23+
LL - let _: Option<i32> = a.iter().filter_map(|s| s.parse().ok()).next();
24+
LL + let _: Option<i32> = a.iter().find_map(|s| s.parse().ok());
25+
|
1526

1627
error: aborting due to 2 previous errors
1728

tests/ui/filter_map_next_unfixable.stderr

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: called `filter_map(..).next()` on an `Iterator`. This is more succinctly expressed by calling `.find_map(..)` instead
1+
error: called `filter_map(..).next()` on an `Iterator`
22
--> tests/ui/filter_map_next_unfixable.rs:7:26
33
|
44
LL | let _: Option<u32> = vec![1, 2, 3, 4, 5, 6]
@@ -8,6 +8,7 @@ LL | | })
88
LL | | .next();
99
| |_______________^
1010
|
11+
= help: use `.find_map(..)` instead
1112
= note: `-D clippy::filter-map-next` implied by `-D warnings`
1213
= help: to override `-D warnings` add `#[allow(clippy::filter_map_next)]`
1314

0 commit comments

Comments
 (0)