Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Verify that the clone method call actually corresponds to std::clone:…
…:Clone::clone
  • Loading branch information
estebank committed Jun 14, 2019
commit 8ce063a216768ed125eb4245a027fef431d7d78c
20 changes: 15 additions & 5 deletions src/librustc_typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,19 +425,29 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
}
}
// If this expression had a clone call, when suggesting borrowing, we
// want to suggest removing it
let sugg_expr = sugg_expr.trim_end_matches(".clone()");

let mut sugg = sugg_expr.as_str();
if let hir::ExprKind::MethodCall(_segment, _sp, _args) = &expr.node {
let clone_path = "std::clone::Clone::clone";
if let Some(true) = self.tables.borrow()
.type_dependent_def_id(expr.hir_id)
.map(|did| self.tcx.def_path_str(did).as_str() == clone_path)
{
// If this expression had a clone call when suggesting borrowing
// we want to suggest removing it because it'd now be unecessary.
sugg = sugg_expr.trim_end_matches(".clone()");
}
}
return Some(match mutability {
hir::Mutability::MutMutable => (
sp,
"consider mutably borrowing here",
format!("{}&mut {}", field_name, sugg_expr),
format!("{}&mut {}", field_name, sugg),
),
hir::Mutability::MutImmutable => (
sp,
"consider borrowing here",
format!("{}&{}", field_name, sugg_expr),
format!("{}&{}", field_name, sugg),
),
});
}
Expand Down