Skip to content

Commit f430384

Browse files
committed
move filter_map_flat_map to its own module
1 parent 6d94161 commit f430384

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use crate::utils::{match_trait_method, paths, span_lint_and_help};
2+
use rustc_hir as hir;
3+
use rustc_lint::LateContext;
4+
5+
use super::FILTER_MAP;
6+
7+
/// lint use of `filter_map().flat_map()` for `Iterators`
8+
pub(super) fn check<'tcx>(
9+
cx: &LateContext<'tcx>,
10+
expr: &'tcx hir::Expr<'_>,
11+
_filter_args: &'tcx [hir::Expr<'_>],
12+
_map_args: &'tcx [hir::Expr<'_>],
13+
) {
14+
// lint if caller of `.filter_map().flat_map()` is an Iterator
15+
if match_trait_method(cx, expr, &paths::ITERATOR) {
16+
let msg = "called `filter_map(..).flat_map(..)` on an `Iterator`";
17+
let hint = "this is more succinctly expressed by calling `.flat_map(..)` \
18+
and filtering by returning `iter::empty()`";
19+
span_lint_and_help(cx, FILTER_MAP, expr.span, msg, None, hint);
20+
}
21+
}

clippy_lints/src/methods/mod.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod expect_used;
55
mod filetype_is_file;
66
mod filter_flat_map;
77
mod filter_map;
8+
mod filter_map_flat_map;
89
mod filter_map_identity;
910
mod filter_map_map;
1011
mod filter_map_next;
@@ -1707,7 +1708,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
17071708
["next", "filter_map"] => filter_map_next::check(cx, expr, arg_lists[1], self.msrv.as_ref()),
17081709
["map", "find"] => filter_map::check(cx, expr, true),
17091710
["flat_map", "filter"] => filter_flat_map::check(cx, expr, arg_lists[1], arg_lists[0]),
1710-
["flat_map", "filter_map"] => lint_filter_map_flat_map(cx, expr, arg_lists[1], arg_lists[0]),
1711+
["flat_map", "filter_map"] => filter_map_flat_map::check(cx, expr, arg_lists[1], arg_lists[0]),
17111712
["flat_map", ..] => lint_flat_map_identity(cx, expr, arg_lists[0], method_spans[0]),
17121713
["flatten", "map"] => lint_map_flatten(cx, expr, arg_lists[1]),
17131714
["is_some", "find"] => lint_search_is_some(cx, expr, "find", arg_lists[1], arg_lists[0], method_spans[1]),
@@ -2754,22 +2755,6 @@ fn lint_map_or_none<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, map
27542755
);
27552756
}
27562757

2757-
/// lint use of `filter_map().flat_map()` for `Iterators`
2758-
fn lint_filter_map_flat_map<'tcx>(
2759-
cx: &LateContext<'tcx>,
2760-
expr: &'tcx hir::Expr<'_>,
2761-
_filter_args: &'tcx [hir::Expr<'_>],
2762-
_map_args: &'tcx [hir::Expr<'_>],
2763-
) {
2764-
// lint if caller of `.filter_map().flat_map()` is an Iterator
2765-
if match_trait_method(cx, expr, &paths::ITERATOR) {
2766-
let msg = "called `filter_map(..).flat_map(..)` on an `Iterator`";
2767-
let hint = "this is more succinctly expressed by calling `.flat_map(..)` \
2768-
and filtering by returning `iter::empty()`";
2769-
span_lint_and_help(cx, FILTER_MAP, expr.span, msg, None, hint);
2770-
}
2771-
}
2772-
27732758
/// lint use of `flat_map` for `Iterators` where `flatten` would be sufficient
27742759
fn lint_flat_map_identity<'tcx>(
27752760
cx: &LateContext<'tcx>,

0 commit comments

Comments
 (0)