2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::is_trait_method;
|
|
|
|
use clippy_utils::source::snippet;
|
|
|
|
use clippy_utils::ty::is_type_diagnostic_item;
|
2021-03-12 08:30:50 -06:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_lint::LateContext;
|
|
|
|
use rustc_middle::ty;
|
|
|
|
use rustc_span::symbol::sym;
|
|
|
|
|
|
|
|
use super::MAP_FLATTEN;
|
|
|
|
|
|
|
|
/// lint use of `map().flatten()` for `Iterators` and 'Options'
|
2021-04-08 10:50:13 -05:00
|
|
|
pub(super) fn check<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
expr: &'tcx hir::Expr<'_>,
|
|
|
|
recv: &'tcx hir::Expr<'_>,
|
|
|
|
map_arg: &'tcx hir::Expr<'_>,
|
|
|
|
) {
|
2021-03-12 08:30:50 -06:00
|
|
|
// lint if caller of `.map().flatten()` is an Iterator
|
2021-03-25 13:29:11 -05:00
|
|
|
if is_trait_method(cx, expr, sym::Iterator) {
|
2021-04-08 10:50:13 -05:00
|
|
|
let map_closure_ty = cx.typeck_results().expr_ty(map_arg);
|
2021-03-12 08:30:50 -06:00
|
|
|
let is_map_to_option = match map_closure_ty.kind() {
|
|
|
|
ty::Closure(_, _) | ty::FnDef(_, _) | ty::FnPtr(_) => {
|
|
|
|
let map_closure_sig = match map_closure_ty.kind() {
|
|
|
|
ty::Closure(_, substs) => substs.as_closure().sig(),
|
|
|
|
_ => map_closure_ty.fn_sig(cx.tcx),
|
|
|
|
};
|
|
|
|
let map_closure_return_ty = cx.tcx.erase_late_bound_regions(map_closure_sig.output());
|
2021-10-02 18:51:01 -05:00
|
|
|
is_type_diagnostic_item(cx, map_closure_return_ty, sym::Option)
|
2021-03-12 08:30:50 -06:00
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
let method_to_use = if is_map_to_option {
|
|
|
|
// `(...).map(...)` has type `impl Iterator<Item=Option<...>>
|
|
|
|
"filter_map"
|
|
|
|
} else {
|
|
|
|
// `(...).map(...)` has type `impl Iterator<Item=impl Iterator<...>>
|
|
|
|
"flat_map"
|
|
|
|
};
|
2021-04-08 10:50:13 -05:00
|
|
|
let func_snippet = snippet(cx, map_arg.span, "..");
|
2021-03-12 08:30:50 -06:00
|
|
|
let hint = format!(".{0}({1})", method_to_use, func_snippet);
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
MAP_FLATTEN,
|
2021-04-08 10:50:13 -05:00
|
|
|
expr.span.with_lo(recv.span.hi()),
|
2021-03-12 08:30:50 -06:00
|
|
|
"called `map(..).flatten()` on an `Iterator`",
|
|
|
|
&format!("try using `{}` instead", method_to_use),
|
|
|
|
hint,
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-12 04:16:25 -05:00
|
|
|
// lint if caller of `.map().flatten()` is an Option or Result
|
|
|
|
let caller_type = match cx.typeck_results().expr_ty(recv).kind() {
|
|
|
|
ty::Adt(adt, _) => {
|
2022-03-04 14:28:41 -06:00
|
|
|
if cx.tcx.is_diagnostic_item(sym::Option, adt.did()) {
|
2021-08-12 04:16:25 -05:00
|
|
|
"Option"
|
2022-03-04 14:28:41 -06:00
|
|
|
} else if cx.tcx.is_diagnostic_item(sym::Result, adt.did()) {
|
2021-08-12 04:16:25 -05:00
|
|
|
"Result"
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let func_snippet = snippet(cx, map_arg.span, "..");
|
|
|
|
let hint = format!(".and_then({})", func_snippet);
|
|
|
|
let lint_info = format!("called `map(..).flatten()` on an `{}`", caller_type);
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
MAP_FLATTEN,
|
|
|
|
expr.span.with_lo(recv.span.hi()),
|
|
|
|
&lint_info,
|
|
|
|
"try using `and_then` instead",
|
|
|
|
hint,
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
2021-03-12 08:30:50 -06:00
|
|
|
}
|