]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/methods/flat_map_identity.rs
New upstream version 1.55.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / methods / flat_map_identity.rs
CommitLineData
cdc7bbd5 1use clippy_utils::diagnostics::span_lint_and_sugg;
136023e0 2use clippy_utils::{is_expr_identity_function, is_trait_method};
f20569fa
XL
3use rustc_errors::Applicability;
4use rustc_hir as hir;
5use rustc_lint::LateContext;
cdc7bbd5 6use rustc_span::{source_map::Span, sym};
f20569fa
XL
7
8use super::FLAT_MAP_IDENTITY;
9
10/// lint use of `flat_map` for `Iterators` where `flatten` would be sufficient
11pub(super) fn check<'tcx>(
12 cx: &LateContext<'tcx>,
13 expr: &'tcx hir::Expr<'_>,
cdc7bbd5 14 flat_map_arg: &'tcx hir::Expr<'_>,
f20569fa
XL
15 flat_map_span: Span,
16) {
136023e0
XL
17 if is_trait_method(cx, expr, sym::Iterator) && is_expr_identity_function(cx, flat_map_arg) {
18 span_lint_and_sugg(
19 cx,
20 FLAT_MAP_IDENTITY,
21 flat_map_span.with_hi(expr.span.hi()),
22 "use of `flat_map` with an identity function",
23 "try",
24 "flatten()".to_string(),
25 Applicability::MachineApplicable,
26 );
f20569fa
XL
27 }
28}