Skip to content

Commit

Permalink
Rollup merge of rust-lang#61143 - estebank:issue-61106, r=eddyb
Browse files Browse the repository at this point in the history
When suggesting borrow, remove useless clones

Fix rust-lang#61106.
  • Loading branch information
Centril authored Jun 14, 2019
2 parents 9606f6f + 34c4117 commit 58a6259
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/librustc_typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(_, _) |
Expand Down Expand Up @@ -425,6 +441,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}
}
}

return Some(match mutability {
hir::Mutability::MutMutable => (
sp,
Expand Down
6 changes: 6 additions & 0 deletions src/test/ui/issues/issue-61106.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {
let x = String::new();
foo(x.clone()); //~ ERROR mismatched types
}

fn foo(_: &str) {}
15 changes: 15 additions & 0 deletions src/test/ui/issues/issue-61106.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 58a6259

Please sign in to comment.