Skip to content

Commit ae8d6a8

Browse files
committed
review comments: avoid string modification
1 parent 8ce063a commit ae8d6a8

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

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

+15-15
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,19 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
379379
}
380380
};
381381
if self.can_coerce(ref_ty, expected) {
382-
if let Ok(src) = cm.span_to_snippet(sp) {
382+
let mut sugg_sp = sp;
383+
if let hir::ExprKind::MethodCall(_segment, _sp, args) = &expr.node {
384+
let clone_path = "std::clone::Clone::clone";
385+
if let ([arg], Some(true)) = (&args[..], self.tables.borrow()
386+
.type_dependent_def_id(expr.hir_id)
387+
.map(|did| self.tcx.def_path_str(did).as_str() == clone_path))
388+
{
389+
// If this expression had a clone call when suggesting borrowing
390+
// we want to suggest removing it because it'd now be unecessary.
391+
sugg_sp = arg.span;
392+
}
393+
}
394+
if let Ok(src) = cm.span_to_snippet(sugg_sp) {
383395
let needs_parens = match expr.node {
384396
// parenthesize if needed (Issue #46756)
385397
hir::ExprKind::Cast(_, _) |
@@ -426,28 +438,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
426438
}
427439
}
428440

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-
}
441441
return Some(match mutability {
442442
hir::Mutability::MutMutable => (
443443
sp,
444444
"consider mutably borrowing here",
445-
format!("{}&mut {}", field_name, sugg),
445+
format!("{}&mut {}", field_name, sugg_expr),
446446
),
447447
hir::Mutability::MutImmutable => (
448448
sp,
449449
"consider borrowing here",
450-
format!("{}&{}", field_name, sugg),
450+
format!("{}&{}", field_name, sugg_expr),
451451
),
452452
});
453453
}

0 commit comments

Comments
 (0)