Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
17 changes: 15 additions & 2 deletions src/librustc_typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,16 +425,29 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}
}
}

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
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`.