]>
Commit | Line | Data |
---|---|---|
cdc7bbd5 XL |
1 | use clippy_utils::diagnostics::span_lint_and_sugg; |
2 | use clippy_utils::is_trait_method; | |
3 | use rustc_errors::Applicability; | |
4 | use rustc_hir as hir; | |
5 | use rustc_lint::LateContext; | |
6 | use rustc_middle::ty; | |
7 | use rustc_span::{source_map::Span, sym}; | |
8 | ||
9 | use super::FLAT_MAP_OPTION; | |
10 | use clippy_utils::ty::is_type_diagnostic_item; | |
11 | ||
12 | pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, arg: &'tcx hir::Expr<'_>, span: Span) { | |
13 | if !is_trait_method(cx, expr, sym::Iterator) { | |
14 | return; | |
15 | } | |
16 | let arg_ty = cx.typeck_results().expr_ty_adjusted(arg); | |
17 | let sig = match arg_ty.kind() { | |
18 | ty::Closure(_, substs) => substs.as_closure().sig(), | |
19 | _ if arg_ty.is_fn() => arg_ty.fn_sig(cx.tcx), | |
20 | _ => return, | |
21 | }; | |
22 | if !is_type_diagnostic_item(cx, sig.output().skip_binder(), sym::option_type) { | |
23 | return; | |
24 | } | |
25 | span_lint_and_sugg( | |
26 | cx, | |
27 | FLAT_MAP_OPTION, | |
28 | span, | |
29 | "used `flat_map` where `filter_map` could be used instead", | |
30 | "try", | |
31 | "filter_map".into(), | |
32 | Applicability::MachineApplicable, | |
17df50a5 | 33 | ); |
cdc7bbd5 | 34 | } |