]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/flat_map_identity.rs
Rollup merge of #105523 - estebank:suggest-collect-vec, r=compiler-errors
[rust.git] / src / tools / clippy / clippy_lints / src / methods / flat_map_identity.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::{is_expr_identity_function, is_trait_method};
3 use rustc_errors::Applicability;
4 use rustc_hir as hir;
5 use rustc_lint::LateContext;
6 use rustc_span::{source_map::Span, sym};
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_arg: &'tcx hir::Expr<'_>,
15     flat_map_span: Span,
16 ) {
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         );
27     }
28 }