Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 37ba779

Browse files
committed
move flat_map_identity to its own module
1 parent f430384 commit 37ba779

File tree

2 files changed

+59
-50
lines changed

2 files changed

+59
-50
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use crate::utils::{match_qpath, match_trait_method, paths, span_lint_and_sugg};
2+
use if_chain::if_chain;
3+
use rustc_errors::Applicability;
4+
use rustc_hir as hir;
5+
use rustc_lint::LateContext;
6+
use rustc_span::source_map::Span;
7+
8+
use super::FLAT_MAP_IDENTITY;
9+
10+
/// lint use of `flat_map` for `Iterators` where `flatten` would be sufficient
11+
pub(super) fn check<'tcx>(
12+
cx: &LateContext<'tcx>,
13+
expr: &'tcx hir::Expr<'_>,
14+
flat_map_args: &'tcx [hir::Expr<'_>],
15+
flat_map_span: Span,
16+
) {
17+
if match_trait_method(cx, expr, &paths::ITERATOR) {
18+
let arg_node = &flat_map_args[1].kind;
19+
20+
let apply_lint = |message: &str| {
21+
span_lint_and_sugg(
22+
cx,
23+
FLAT_MAP_IDENTITY,
24+
flat_map_span.with_hi(expr.span.hi()),
25+
message,
26+
"try",
27+
"flatten()".to_string(),
28+
Applicability::MachineApplicable,
29+
);
30+
};
31+
32+
if_chain! {
33+
if let hir::ExprKind::Closure(_, _, body_id, _, _) = arg_node;
34+
let body = cx.tcx.hir().body(*body_id);
35+
36+
if let hir::PatKind::Binding(_, _, binding_ident, _) = body.params[0].pat.kind;
37+
if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = body.value.kind;
38+
39+
if path.segments.len() == 1;
40+
if path.segments[0].ident.name == binding_ident.name;
41+
42+
then {
43+
apply_lint("called `flat_map(|x| x)` on an `Iterator`");
44+
}
45+
}
46+
47+
if_chain! {
48+
if let hir::ExprKind::Path(ref qpath) = arg_node;
49+
50+
if match_qpath(qpath, &paths::STD_CONVERT_IDENTITY);
51+
52+
then {
53+
apply_lint("called `flat_map(std::convert::identity)` on an `Iterator`");
54+
}
55+
}
56+
}
57+
}

clippy_lints/src/methods/mod.rs

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mod filter_map_identity;
1010
mod filter_map_map;
1111
mod filter_map_next;
1212
mod filter_next;
13+
mod flat_map_identity;
1314
mod from_iter_instead_of_collect;
1415
mod get_unwrap;
1516
mod implicit_clone;
@@ -1709,7 +1710,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
17091710
["map", "find"] => filter_map::check(cx, expr, true),
17101711
["flat_map", "filter"] => filter_flat_map::check(cx, expr, arg_lists[1], arg_lists[0]),
17111712
["flat_map", "filter_map"] => filter_map_flat_map::check(cx, expr, arg_lists[1], arg_lists[0]),
1712-
["flat_map", ..] => lint_flat_map_identity(cx, expr, arg_lists[0], method_spans[0]),
1713+
["flat_map", ..] => flat_map_identity::check(cx, expr, arg_lists[0], method_spans[0]),
17131714
["flatten", "map"] => lint_map_flatten(cx, expr, arg_lists[1]),
17141715
["is_some", "find"] => lint_search_is_some(cx, expr, "find", arg_lists[1], arg_lists[0], method_spans[1]),
17151716
["is_some", "position"] => {
@@ -2755,55 +2756,6 @@ fn lint_map_or_none<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, map
27552756
);
27562757
}
27572758

2758-
/// lint use of `flat_map` for `Iterators` where `flatten` would be sufficient
2759-
fn lint_flat_map_identity<'tcx>(
2760-
cx: &LateContext<'tcx>,
2761-
expr: &'tcx hir::Expr<'_>,
2762-
flat_map_args: &'tcx [hir::Expr<'_>],
2763-
flat_map_span: Span,
2764-
) {
2765-
if match_trait_method(cx, expr, &paths::ITERATOR) {
2766-
let arg_node = &flat_map_args[1].kind;
2767-
2768-
let apply_lint = |message: &str| {
2769-
span_lint_and_sugg(
2770-
cx,
2771-
FLAT_MAP_IDENTITY,
2772-
flat_map_span.with_hi(expr.span.hi()),
2773-
message,
2774-
"try",
2775-
"flatten()".to_string(),
2776-
Applicability::MachineApplicable,
2777-
);
2778-
};
2779-
2780-
if_chain! {
2781-
if let hir::ExprKind::Closure(_, _, body_id, _, _) = arg_node;
2782-
let body = cx.tcx.hir().body(*body_id);
2783-
2784-
if let hir::PatKind::Binding(_, _, binding_ident, _) = body.params[0].pat.kind;
2785-
if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = body.value.kind;
2786-
2787-
if path.segments.len() == 1;
2788-
if path.segments[0].ident.name == binding_ident.name;
2789-
2790-
then {
2791-
apply_lint("called `flat_map(|x| x)` on an `Iterator`");
2792-
}
2793-
}
2794-
2795-
if_chain! {
2796-
if let hir::ExprKind::Path(ref qpath) = arg_node;
2797-
2798-
if match_qpath(qpath, &paths::STD_CONVERT_IDENTITY);
2799-
2800-
then {
2801-
apply_lint("called `flat_map(std::convert::identity)` on an `Iterator`");
2802-
}
2803-
}
2804-
}
2805-
}
2806-
28072759
/// lint searching an Iterator followed by `is_some()`
28082760
/// or calling `find()` on a string followed by `is_some()`
28092761
fn lint_search_is_some<'tcx>(

0 commit comments

Comments
 (0)