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,8 +19,9 @@ 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
{ {
match arg.kind {
hir::ExprKind::Closure(&hir::Closure { body, .. }) => {
let closure_body = cx.tcx.hir().body(body); let closure_body = cx.tcx.hir().body(body);
let closure_expr = peel_blocks(closure_body.value); let closure_expr = peel_blocks(closure_body.value);
match closure_body.params[0].pat.kind { match closure_body.params[0].pat.kind {
@ -65,6 +66,18 @@ pub(super) fn check(cx: &LateContext<'_>, e: &hir::Expr<'_>, recv: &hir::Expr<'_
}, },
_ => {}, _ => {},
} }
},
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;