Skip to content

Commit

Permalink
Remove one-liner treatment from filter_map_next lint
Browse files Browse the repository at this point in the history
  • Loading branch information
andrehjr committed Apr 30, 2019
1 parent a53682a commit 2683884
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
20 changes: 8 additions & 12 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1965,18 +1965,14 @@ fn lint_filter_map_next<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::E
let msg = "called `filter_map(p).next()` on an `Iterator`. This is more succinctly expressed by calling \
`.find_map(p)` instead.";
let filter_snippet = snippet(cx, filter_args[1].span, "..");
if filter_snippet.lines().count() <= 1 {
span_note_and_lint(
cx,
FILTER_MAP_NEXT,
expr.span,
msg,
expr.span,
&format!("replace `filter_map({0}).next()` with `find_map({0})`", filter_snippet),
);
} else {
span_lint(cx, FILTER_MAP_NEXT, expr.span, msg);
}
span_note_and_lint(
cx,
FILTER_MAP_NEXT,
expr.span,
msg,
expr.span,
&format!("replace `filter_map({0}).next()` with `find_map({0})`", filter_snippet),
);
}
}

Expand Down
5 changes: 5 additions & 0 deletions tests/ui/filter_map_next.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ fn main() {

let element: Option<i32> = a.iter().filter_map(|s| s.parse().ok()).next();
assert_eq!(element, Some(1));

let _: Option<u32> = vec![1, 2, 3, 4, 5, 6]
.into_iter()
.filter_map(|x| if x == 2 { Some(x * 2) } else { None })
.next();
}
14 changes: 13 additions & 1 deletion tests/ui/filter_map_next.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,17 @@ LL | let element: Option<i32> = a.iter().filter_map(|s| s.parse().ok()).next
= note: `-D clippy::filter-map-next` implied by `-D warnings`
= note: replace `filter_map(|s| s.parse().ok()).next()` with `find_map(|s| s.parse().ok())`

error: aborting due to previous error
error: called `filter_map(p).next()` on an `Iterator`. This is more succinctly expressed by calling `.find_map(p)` instead.
--> $DIR/filter_map_next.rs:9:26
|
LL | let _: Option<u32> = vec![1, 2, 3, 4, 5, 6]
| __________________________^
LL | | .into_iter()
LL | | .filter_map(|x| if x == 2 { Some(x * 2) } else { None })
LL | | .next();
| |_______________^
|
= 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 })`

error: aborting due to 2 previous errors

0 comments on commit 2683884

Please sign in to comment.