Skip to content

Commit bdb05a8

Browse files
committed
When suggesting to borrow, remove useless clones
1 parent 0e4a56b commit bdb05a8

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/librustc_typeck/check/demand.rs

+3
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
425425
}
426426
}
427427
}
428+
// If this expression had a clone call, when suggesting borrowing, we
429+
// want to suggest removing it
430+
let sugg_expr = sugg_expr.trim_end_matches(".clone()");
428431
return Some(match mutability {
429432
hir::Mutability::MutMutable => (
430433
sp,

src/test/ui/issues/issue-61106.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
let x = String::new();
3+
foo(x.clone()); //~ ERROR mismatched types
4+
}
5+
6+
fn foo(_: &str) {}

src/test/ui/issues/issue-61106.stderr

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-61106.rs:3:9
3+
|
4+
LL | foo(x.clone());
5+
| ^^^^^^^^^
6+
| |
7+
| expected &str, found struct `std::string::String`
8+
| help: consider borrowing here: `&x`
9+
|
10+
= note: expected type `&str`
11+
found type `std::string::String`
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)