Skip to content

Commit 8ce063a

Browse files
committed
Verify that the clone method call actually corresponds to std::clone::Clone::clone
1 parent bdb05a8 commit 8ce063a

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

Diff for: src/librustc_typeck/check/demand.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -425,19 +425,29 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
425425
}
426426
}
427427
}
428-
// If this expression had a clone call, when suggesting borrowing, we
429-
// want to suggest removing it
430-
let sugg_expr = sugg_expr.trim_end_matches(".clone()");
428+
429+
let mut sugg = sugg_expr.as_str();
430+
if let hir::ExprKind::MethodCall(_segment, _sp, _args) = &expr.node {
431+
let clone_path = "std::clone::Clone::clone";
432+
if let Some(true) = self.tables.borrow()
433+
.type_dependent_def_id(expr.hir_id)
434+
.map(|did| self.tcx.def_path_str(did).as_str() == clone_path)
435+
{
436+
// If this expression had a clone call when suggesting borrowing
437+
// we want to suggest removing it because it'd now be unecessary.
438+
sugg = sugg_expr.trim_end_matches(".clone()");
439+
}
440+
}
431441
return Some(match mutability {
432442
hir::Mutability::MutMutable => (
433443
sp,
434444
"consider mutably borrowing here",
435-
format!("{}&mut {}", field_name, sugg_expr),
445+
format!("{}&mut {}", field_name, sugg),
436446
),
437447
hir::Mutability::MutImmutable => (
438448
sp,
439449
"consider borrowing here",
440-
format!("{}&{}", field_name, sugg_expr),
450+
format!("{}&{}", field_name, sugg),
441451
),
442452
});
443453
}

0 commit comments

Comments
 (0)