]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/methods/map_identity.rs
New upstream version 1.55.0+dfsg1
[rustc.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 _map_span: Span,
17 ) {
18 let caller_ty = cx.typeck_results().expr_ty(caller);
19
20 if_chain! {
21 if is_trait_method(cx, expr, sym::Iterator)
22 || is_type_diagnostic_item(cx, caller_ty, sym::result_type)
23 || is_type_diagnostic_item(cx, caller_ty, sym::option_type);
24 if is_expr_identity_function(cx, map_arg);
25 if let Some(sugg_span) = expr.span.trim_start(caller.span);
26 then {
27 span_lint_and_sugg(
28 cx,
29 MAP_IDENTITY,
30 sugg_span,
31 "unnecessary map of the identity function",
32 "remove the call to `map`",
33 String::new(),
34 Applicability::MachineApplicable,
35 )
36 }
37 }
38 }