]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/methods/map_identity.rs
Rollup merge of #105623 - compiler-errors:generator-type-size-fix, r=Nilstrieb
[rust.git] / src / tools / clippy / clippy_lints / src / methods / map_identity.rs
1 use clippy_utils::diagnostics::span_lint_and_sugg;
2 use clippy_utils::ty::is_type_diagnostic_item;
3 use clippy_utils::{is_expr_identity_function, is_trait_method};
4 use rustc_errors::Applicability;
5 use rustc_hir as hir;
6 use rustc_lint::LateContext;
7 use rustc_span::{source_map::Span, sym};
8
9 use super::MAP_IDENTITY;
10
11 pub(super) fn check(
12     cx: &LateContext<'_>,
13     expr: &hir::Expr<'_>,
14     caller: &hir::Expr<'_>,
15     map_arg: &hir::Expr<'_>,
16     name: &str,
17     _map_span: Span,
18 ) {
19     let caller_ty = cx.typeck_results().expr_ty(caller);
20
21     if_chain! {
22         if is_trait_method(cx, expr, sym::Iterator)
23             || is_type_diagnostic_item(cx, caller_ty, sym::Result)
24             || is_type_diagnostic_item(cx, caller_ty, sym::Option);
25         if is_expr_identity_function(cx, map_arg);
26         if let Some(sugg_span) = expr.span.trim_start(caller.span);
27         then {
28             span_lint_and_sugg(
29                 cx,
30                 MAP_IDENTITY,
31                 sugg_span,
32                 "unnecessary map of the identity function",
33                 &format!("remove the call to `{name}`"),
34                 String::new(),
35                 Applicability::MachineApplicable,
36             )
37         }
38     }
39 }