Skip to content

Commit 2683884

Browse files
committed
Remove one-liner treatment from filter_map_next lint
1 parent a53682a commit 2683884

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,18 +1965,14 @@ fn lint_filter_map_next<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::E
19651965
let msg = "called `filter_map(p).next()` on an `Iterator`. This is more succinctly expressed by calling \
19661966
`.find_map(p)` instead.";
19671967
let filter_snippet = snippet(cx, filter_args[1].span, "..");
1968-
if filter_snippet.lines().count() <= 1 {
1969-
span_note_and_lint(
1970-
cx,
1971-
FILTER_MAP_NEXT,
1972-
expr.span,
1973-
msg,
1974-
expr.span,
1975-
&format!("replace `filter_map({0}).next()` with `find_map({0})`", filter_snippet),
1976-
);
1977-
} else {
1978-
span_lint(cx, FILTER_MAP_NEXT, expr.span, msg);
1979-
}
1968+
span_note_and_lint(
1969+
cx,
1970+
FILTER_MAP_NEXT,
1971+
expr.span,
1972+
msg,
1973+
expr.span,
1974+
&format!("replace `filter_map({0}).next()` with `find_map({0})`", filter_snippet),
1975+
);
19801976
}
19811977
}
19821978

tests/ui/filter_map_next.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@ fn main() {
55

66
let element: Option<i32> = a.iter().filter_map(|s| s.parse().ok()).next();
77
assert_eq!(element, Some(1));
8+
9+
let _: Option<u32> = vec![1, 2, 3, 4, 5, 6]
10+
.into_iter()
11+
.filter_map(|x| if x == 2 { Some(x * 2) } else { None })
12+
.next();
813
}

tests/ui/filter_map_next.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,17 @@ LL | let element: Option<i32> = a.iter().filter_map(|s| s.parse().ok()).next
77
= note: `-D clippy::filter-map-next` implied by `-D warnings`
88
= note: replace `filter_map(|s| s.parse().ok()).next()` with `find_map(|s| s.parse().ok())`
99

10-
error: aborting due to previous error
10+
error: called `filter_map(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find_map(p)` instead.
11+
--> $DIR/filter_map_next.rs:9:26
12+
|
13+
LL | let _: Option<u32> = vec![1, 2, 3, 4, 5, 6]
14+
| __________________________^
15+
LL | | .into_iter()
16+
LL | | .filter_map(|x| if x == 2 { Some(x * 2) } else { None })
17+
LL | | .next();
18+
| |_______________^
19+
|
20+
= note: replace `filter_map(|x| if x == 2 { Some(x * 2) } else { None }).next()` with `find_map(|x| if x == 2 { Some(x * 2) } else { None })`
21+
22+
error: aborting due to 2 previous errors
1123

0 commit comments

Comments
 (0)