]>
Commit | Line | Data |
---|---|---|
cdc7bbd5 | 1 | use clippy_utils::diagnostics::span_lint_and_sugg; |
136023e0 | 2 | use clippy_utils::{is_expr_identity_function, is_trait_method}; |
f20569fa XL |
3 | use rustc_errors::Applicability; |
4 | use rustc_hir as hir; | |
5 | use rustc_lint::LateContext; | |
cdc7bbd5 | 6 | use rustc_span::{source_map::Span, sym}; |
f20569fa XL |
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<'_>, | |
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 | } |