2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-07-01 11:17:38 -05:00
|
|
|
use clippy_utils::{is_expr_identity_function, is_trait_method};
|
2021-03-12 08:30:50 -06:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_lint::LateContext;
|
2021-03-25 13:29:11 -05:00
|
|
|
use rustc_span::{source_map::Span, sym};
|
2021-03-12 08:30:50 -06:00
|
|
|
|
|
|
|
use super::FLAT_MAP_IDENTITY;
|
|
|
|
|
|
|
|
/// lint use of `flat_map` for `Iterators` where `flatten` would be sufficient
|
|
|
|
pub(super) fn check<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
expr: &'tcx hir::Expr<'_>,
|
2021-04-08 10:50:13 -05:00
|
|
|
flat_map_arg: &'tcx hir::Expr<'_>,
|
2021-03-12 08:30:50 -06:00
|
|
|
flat_map_span: Span,
|
|
|
|
) {
|
2021-07-01 11:17:38 -05:00
|
|
|
if is_trait_method(cx, expr, sym::Iterator) && is_expr_identity_function(cx, flat_map_arg) {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
FLAT_MAP_IDENTITY,
|
|
|
|
flat_map_span.with_hi(expr.span.hi()),
|
|
|
|
"use of `flat_map` with an identity function",
|
|
|
|
"try",
|
|
|
|
"flatten()".to_string(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
2021-03-12 08:30:50 -06:00
|
|
|
}
|
|
|
|
}
|