blob: 043adb8434a0d9eec339191ace9786ff6b32778c [file] [log] [blame] [edit]
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_expr_untyped_identity_function;
use clippy_utils::res::{MaybeDef, MaybeTypeckRes};
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_span::{Span, sym};
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<'_>,
flat_map_arg: &'tcx hir::Expr<'_>,
flat_map_span: Span,
) {
if cx.ty_based_def(expr).opt_parent(cx).is_diag_item(cx, sym::Iterator)
&& is_expr_untyped_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,
);
}
}