Extend map_clone lint to also work on non-explicit closures

This commit is contained in:
Guillaume Gomez 2024-01-06 16:56:24 +01:00
parent 7aa4624a8f
commit 28c133b4bc

View file

@ -2,7 +2,7 @@ use clippy_config::msrvs::{self, Msrv};
use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability; use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::{is_copy, is_type_diagnostic_item}; use clippy_utils::ty::{is_copy, is_type_diagnostic_item};
use clippy_utils::{is_diag_trait_item, peel_blocks}; use clippy_utils::{is_diag_trait_item, match_def_path, paths, peel_blocks};
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_lint::LateContext; use rustc_lint::LateContext;
@ -19,50 +19,63 @@ pub(super) fn check(cx: &LateContext<'_>, e: &hir::Expr<'_>, recv: &hir::Expr<'_
&& (cx.tcx.impl_of_method(method_id).map_or(false, |id| { && (cx.tcx.impl_of_method(method_id).map_or(false, |id| {
is_type_diagnostic_item(cx, cx.tcx.type_of(id).instantiate_identity(), sym::Option) is_type_diagnostic_item(cx, cx.tcx.type_of(id).instantiate_identity(), sym::Option)
}) || is_diag_trait_item(cx, method_id, sym::Iterator)) }) || is_diag_trait_item(cx, method_id, sym::Iterator))
&& let hir::ExprKind::Closure(&hir::Closure { body, .. }) = arg.kind
{ {
let closure_body = cx.tcx.hir().body(body); match arg.kind {
let closure_expr = peel_blocks(closure_body.value); hir::ExprKind::Closure(&hir::Closure { body, .. }) => {
match closure_body.params[0].pat.kind { let closure_body = cx.tcx.hir().body(body);
hir::PatKind::Ref(inner, hir::Mutability::Not) => { let closure_expr = peel_blocks(closure_body.value);
if let hir::PatKind::Binding(hir::BindingAnnotation::NONE, .., name, None) = inner.kind { match closure_body.params[0].pat.kind {
if ident_eq(name, closure_expr) { hir::PatKind::Ref(inner, hir::Mutability::Not) => {
lint_explicit_closure(cx, e.span, recv.span, true, msrv); if let hir::PatKind::Binding(hir::BindingAnnotation::NONE, .., name, None) = inner.kind {
} if ident_eq(name, closure_expr) {
}
},
hir::PatKind::Binding(hir::BindingAnnotation::NONE, .., name, None) => {
match closure_expr.kind {
hir::ExprKind::Unary(hir::UnOp::Deref, inner) => {
if ident_eq(name, inner) {
if let ty::Ref(.., Mutability::Not) = cx.typeck_results().expr_ty(inner).kind() {
lint_explicit_closure(cx, e.span, recv.span, true, msrv); lint_explicit_closure(cx, e.span, recv.span, true, msrv);
} }
} }
}, },
hir::ExprKind::MethodCall(method, obj, [], _) => { hir::PatKind::Binding(hir::BindingAnnotation::NONE, .., name, None) => {
if ident_eq(name, obj) && method.ident.name == sym::clone match closure_expr.kind {
&& let Some(fn_id) = cx.typeck_results().type_dependent_def_id(closure_expr.hir_id) hir::ExprKind::Unary(hir::UnOp::Deref, inner) => {
&& let Some(trait_id) = cx.tcx.trait_of_item(fn_id) if ident_eq(name, inner) {
&& cx.tcx.lang_items().clone_trait().map_or(false, |id| id == trait_id) if let ty::Ref(.., Mutability::Not) = cx.typeck_results().expr_ty(inner).kind() {
// no autoderefs lint_explicit_closure(cx, e.span, recv.span, true, msrv);
&& !cx.typeck_results().expr_adjustments(obj).iter() }
.any(|a| matches!(a.kind, Adjust::Deref(Some(..))))
{
let obj_ty = cx.typeck_results().expr_ty(obj);
if let ty::Ref(_, ty, mutability) = obj_ty.kind() {
if matches!(mutability, Mutability::Not) {
let copy = is_copy(cx, *ty);
lint_explicit_closure(cx, e.span, recv.span, copy, msrv);
} }
} else { },
lint_needless_cloning(cx, e.span, recv.span); hir::ExprKind::MethodCall(method, obj, [], _) => {
} if ident_eq(name, obj) && method.ident.name == sym::clone
&& let Some(fn_id) = cx.typeck_results().type_dependent_def_id(closure_expr.hir_id)
&& let Some(trait_id) = cx.tcx.trait_of_item(fn_id)
&& cx.tcx.lang_items().clone_trait().map_or(false, |id| id == trait_id)
// no autoderefs
&& !cx.typeck_results().expr_adjustments(obj).iter()
.any(|a| matches!(a.kind, Adjust::Deref(Some(..))))
{
let obj_ty = cx.typeck_results().expr_ty(obj);
if let ty::Ref(_, ty, mutability) = obj_ty.kind() {
if matches!(mutability, Mutability::Not) {
let copy = is_copy(cx, *ty);
lint_explicit_closure(cx, e.span, recv.span, copy, msrv);
}
} else {
lint_needless_cloning(cx, e.span, recv.span);
}
}
},
_ => {},
} }
}, },
_ => {}, _ => {},
} }
}, },
hir::ExprKind::Path(qpath) => {
if let Some(path_def_id) = cx.qpath_res(&qpath, arg.hir_id).opt_def_id()
&& match_def_path(cx, path_def_id, &paths::CLONE_TRAIT_METHOD)
{
// FIXME: It would be better to infer the type to check if it's copyable or not
// to suggest to use `.copied()` instead of `.cloned()` where applicable.
lint_path(cx, e.span, recv.span);
}
},
_ => {}, _ => {},
} }
} }
@ -88,6 +101,23 @@ fn lint_needless_cloning(cx: &LateContext<'_>, root: Span, receiver: Span) {
); );
} }
fn lint_path(cx: &LateContext<'_>, replace: Span, root: Span) {
let mut applicability = Applicability::MachineApplicable;
span_lint_and_sugg(
cx,
MAP_CLONE,
replace,
"you are explicitly cloning with `.map()`",
"consider calling the dedicated `cloned` method",
format!(
"{}.cloned()",
snippet_with_applicability(cx, root, "..", &mut applicability),
),
applicability,
);
}
fn lint_explicit_closure(cx: &LateContext<'_>, replace: Span, root: Span, is_copy: bool, msrv: &Msrv) { fn lint_explicit_closure(cx: &LateContext<'_>, replace: Span, root: Span, is_copy: bool, msrv: &Msrv) {
let mut applicability = Applicability::MachineApplicable; let mut applicability = Applicability::MachineApplicable;