Skip to content

Commit 83ae5ed

Browse files
Don't lint with map_clone if current type is Option or Result and method call is as_ref.
1 parent e90eea7 commit 83ae5ed

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

clippy_lints/src/methods/map_clone.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,29 @@ use rustc_span::{sym, Span};
1515

1616
use super::MAP_CLONE;
1717

18-
fn should_run_lint(cx: &LateContext<'_>, method_id: DefId) -> bool {
18+
fn should_run_lint(cx: &LateContext<'_>, e: &hir::Expr<'_>, method_id: DefId) -> bool {
1919
if is_diag_trait_item(cx, method_id, sym::Iterator) {
2020
return true;
2121
}
2222
if !cx.tcx.impl_of_method(method_id).map_or(false, |id| {
23-
is_type_diagnostic_item(cx, cx.tcx.type_of(id).instantiate_identity(), sym::Option)
24-
|| is_type_diagnostic_item(cx, cx.tcx.type_of(id).instantiate_identity(), sym::Result)
23+
let identity = cx.tcx.type_of(id).instantiate_identity();
24+
is_type_diagnostic_item(cx, identity, sym::Option) || is_type_diagnostic_item(cx, identity, sym::Result)
2525
}) {
2626
return false;
2727
}
28+
// We check if the previous method call is `as_ref`.
29+
if let hir::ExprKind::MethodCall(path1, receiver, _, _) = &e.kind
30+
&& let hir::ExprKind::MethodCall(path2, _, _, _) = &receiver.kind
31+
{
32+
return path2.ident.name != sym::as_ref || path1.ident.name != sym::map;
33+
}
34+
2835
return true;
2936
}
3037

3138
pub(super) fn check(cx: &LateContext<'_>, e: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>, msrv: &Msrv) {
3239
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
33-
&& should_run_lint(cx, method_id)
40+
&& should_run_lint(cx, e, method_id)
3441
{
3542
match arg.kind {
3643
hir::ExprKind::Closure(&hir::Closure { body, .. }) => {

0 commit comments

Comments
 (0)