diff options
| author | flip1995 <philipp.krones@embecosm.com> | 2021-04-22 11:31:13 +0200 |
|---|---|---|
| committer | flip1995 <philipp.krones@embecosm.com> | 2021-04-22 11:31:13 +0200 |
| commit | 02bf692169e1f2908169da8f870f8b324a4e6a17 (patch) | |
| tree | 10f25d9e6e1a95384d9f2a0150fc19626d968c0c /clippy_lints/src/methods/flat_map_option.rs | |
| parent | 419bf6bbd827f07406447419d84a154ce9294aa8 (diff) | |
Merge commit '98e2b9f25b6db4b2680a3d388456d9f95cb28344' into clippyup
Diffstat (limited to 'clippy_lints/src/methods/flat_map_option.rs')
| -rw-r--r-- | clippy_lints/src/methods/flat_map_option.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/clippy_lints/src/methods/flat_map_option.rs b/clippy_lints/src/methods/flat_map_option.rs new file mode 100644 index 00000000000..12d560653ed --- /dev/null +++ b/clippy_lints/src/methods/flat_map_option.rs @@ -0,0 +1,34 @@ +use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::is_trait_method; +use rustc_errors::Applicability; +use rustc_hir as hir; +use rustc_lint::LateContext; +use rustc_middle::ty; +use rustc_span::{source_map::Span, sym}; + +use super::FLAT_MAP_OPTION; +use clippy_utils::ty::is_type_diagnostic_item; + +pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, arg: &'tcx hir::Expr<'_>, span: Span) { + if !is_trait_method(cx, expr, sym::Iterator) { + return; + } + let arg_ty = cx.typeck_results().expr_ty_adjusted(arg); + let sig = match arg_ty.kind() { + ty::Closure(_, substs) => substs.as_closure().sig(), + _ if arg_ty.is_fn() => arg_ty.fn_sig(cx.tcx), + _ => return, + }; + if !is_type_diagnostic_item(cx, sig.output().skip_binder(), sym::option_type) { + return; + } + span_lint_and_sugg( + cx, + FLAT_MAP_OPTION, + span, + "used `flat_map` where `filter_map` could be used instead", + "try", + "filter_map".into(), + Applicability::MachineApplicable, + ) +} |
