|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::source::snippet_with_applicability; |
| 3 | +use clippy_utils::ty::{is_copy, is_type_diagnostic_item}; |
| 4 | +use clippy_utils::{is_diag_trait_item, meets_msrv, msrvs, peel_blocks}; |
| 5 | +use if_chain::if_chain; |
| 6 | +use rustc_errors::Applicability; |
| 7 | +use rustc_hir as hir; |
| 8 | +use rustc_lint::LateContext; |
| 9 | +use rustc_middle::mir::Mutability; |
| 10 | +use rustc_middle::ty; |
| 11 | +use rustc_middle::ty::adjustment::Adjust; |
| 12 | +use rustc_semver::RustcVersion; |
| 13 | +use rustc_span::symbol::Ident; |
| 14 | +use rustc_span::{sym, Span}; |
| 15 | + |
| 16 | +use super::MAP_CLONE; |
| 17 | + |
| 18 | +pub(super) fn check<'tcx>( |
| 19 | + cx: &LateContext<'_>, |
| 20 | + e: &hir::Expr<'_>, |
| 21 | + recv: &hir::Expr<'_>, |
| 22 | + arg: &'tcx hir::Expr<'_>, |
| 23 | + msrv: Option<RustcVersion>, |
| 24 | +) { |
| 25 | + if_chain! { |
| 26 | + if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id); |
| 27 | + if cx.tcx.impl_of_method(method_id) |
| 28 | + .map_or(false, |id| is_type_diagnostic_item(cx, cx.tcx.type_of(id), sym::Option)) |
| 29 | + || is_diag_trait_item(cx, method_id, sym::Iterator); |
| 30 | + if let hir::ExprKind::Closure(&hir::Closure{ body, .. }) = arg.kind; |
| 31 | + then { |
| 32 | + let closure_body = cx.tcx.hir().body(body); |
| 33 | + let closure_expr = peel_blocks(&closure_body.value); |
| 34 | + match closure_body.params[0].pat.kind { |
| 35 | + hir::PatKind::Ref(inner, hir::Mutability::Not) => if let hir::PatKind::Binding( |
| 36 | + hir::BindingAnnotation::Unannotated, .., name, None |
| 37 | + ) = inner.kind { |
| 38 | + if ident_eq(name, closure_expr) { |
| 39 | + lint_explicit_closure(cx, e.span, recv.span, true, msrv); |
| 40 | + } |
| 41 | + }, |
| 42 | + hir::PatKind::Binding(hir::BindingAnnotation::Unannotated, .., name, None) => { |
| 43 | + match closure_expr.kind { |
| 44 | + hir::ExprKind::Unary(hir::UnOp::Deref, inner) => { |
| 45 | + if ident_eq(name, inner) { |
| 46 | + if let ty::Ref(.., Mutability::Not) = cx.typeck_results().expr_ty(inner).kind() { |
| 47 | + lint_explicit_closure(cx, e.span, recv.span, true, msrv); |
| 48 | + } |
| 49 | + } |
| 50 | + }, |
| 51 | + hir::ExprKind::MethodCall(method, [obj], _) => if_chain! { |
| 52 | + if ident_eq(name, obj) && method.ident.name == sym::clone; |
| 53 | + if let Some(fn_id) = cx.typeck_results().type_dependent_def_id(closure_expr.hir_id); |
| 54 | + if let Some(trait_id) = cx.tcx.trait_of_item(fn_id); |
| 55 | + if cx.tcx.lang_items().clone_trait().map_or(false, |id| id == trait_id); |
| 56 | + // no autoderefs |
| 57 | + if !cx.typeck_results().expr_adjustments(obj).iter() |
| 58 | + .any(|a| matches!(a.kind, Adjust::Deref(Some(..)))); |
| 59 | + then { |
| 60 | + let obj_ty = cx.typeck_results().expr_ty(obj); |
| 61 | + if let ty::Ref(_, ty, mutability) = obj_ty.kind() { |
| 62 | + if matches!(mutability, Mutability::Not) { |
| 63 | + let copy = is_copy(cx, *ty); |
| 64 | + lint_explicit_closure(cx, e.span, recv.span, copy, msrv); |
| 65 | + } |
| 66 | + } else { |
| 67 | + lint_needless_cloning(cx, e.span, recv.span); |
| 68 | + } |
| 69 | + } |
| 70 | + }, |
| 71 | + _ => {}, |
| 72 | + } |
| 73 | + }, |
| 74 | + _ => {}, |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +fn ident_eq(name: Ident, path: &hir::Expr<'_>) -> bool { |
| 81 | + if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = path.kind { |
| 82 | + path.segments.len() == 1 && path.segments[0].ident == name |
| 83 | + } else { |
| 84 | + false |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +fn lint_needless_cloning(cx: &LateContext<'_>, root: Span, receiver: Span) { |
| 89 | + span_lint_and_sugg( |
| 90 | + cx, |
| 91 | + MAP_CLONE, |
| 92 | + root.trim_start(receiver).unwrap(), |
| 93 | + "you are needlessly cloning iterator elements", |
| 94 | + "remove the `map` call", |
| 95 | + String::new(), |
| 96 | + Applicability::MachineApplicable, |
| 97 | + ); |
| 98 | +} |
| 99 | + |
| 100 | +fn lint_explicit_closure(cx: &LateContext<'_>, replace: Span, root: Span, is_copy: bool, msrv: Option<RustcVersion>) { |
| 101 | + let mut applicability = Applicability::MachineApplicable; |
| 102 | + |
| 103 | + let (message, sugg_method) = if is_copy && meets_msrv(msrv, msrvs::ITERATOR_COPIED) { |
| 104 | + ("you are using an explicit closure for copying elements", "copied") |
| 105 | + } else { |
| 106 | + ("you are using an explicit closure for cloning elements", "cloned") |
| 107 | + }; |
| 108 | + |
| 109 | + span_lint_and_sugg( |
| 110 | + cx, |
| 111 | + MAP_CLONE, |
| 112 | + replace, |
| 113 | + message, |
| 114 | + &format!("consider calling the dedicated `{}` method", sugg_method), |
| 115 | + format!( |
| 116 | + "{}.{}()", |
| 117 | + snippet_with_applicability(cx, root, "..", &mut applicability), |
| 118 | + sugg_method, |
| 119 | + ), |
| 120 | + applicability, |
| 121 | + ); |
| 122 | +} |
0 commit comments