Auto merge of #61143 - estebank:issue-61106, r=eddyb

When suggesting borrow, remove useless clones

Fix #61106.
This commit is contained in:
bors 2019-06-15 13:12:13 +00:00
commit 9f06855064
3 changed files with 39 additions and 1 deletions

View File

@ -379,7 +379,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
};
if self.can_coerce(ref_ty, expected) {
if let Ok(src) = cm.span_to_snippet(sp) {
let mut sugg_sp = sp;
if let hir::ExprKind::MethodCall(segment, _sp, args) = &expr.node {
let clone_trait = self.tcx.lang_items().clone_trait().unwrap();
if let ([arg], Some(true), "clone") = (
&args[..],
self.tables.borrow().type_dependent_def_id(expr.hir_id).map(|did| {
let ai = self.tcx.associated_item(did);
ai.container == ty::TraitContainer(clone_trait)
}),
&segment.ident.as_str()[..],
) {
// If this expression had a clone call when suggesting borrowing
// we want to suggest removing it because it'd now be unecessary.
sugg_sp = arg.span;
}
}
if let Ok(src) = cm.span_to_snippet(sugg_sp) {
let needs_parens = match expr.node {
// parenthesize if needed (Issue #46756)
hir::ExprKind::Cast(_, _) |
@ -425,6 +441,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}
}
return Some(match mutability {
hir::Mutability::MutMutable => (
sp,

View File

@ -0,0 +1,6 @@
fn main() {
let x = String::new();
foo(x.clone()); //~ ERROR mismatched types
}
fn foo(_: &str) {}

View File

@ -0,0 +1,15 @@
error[E0308]: mismatched types
--> $DIR/issue-61106.rs:3:9
|
LL | foo(x.clone());
| ^^^^^^^^^
| |
| expected &str, found struct `std::string::String`
| help: consider borrowing here: `&x`
|
= note: expected type `&str`
found type `std::string::String`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.