Skip to content

Commit 8895fb9

Browse files
committed
Account for &String + String
1 parent ee0bf5e commit 8895fb9

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/librustc_typeck/check/op.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -579,10 +579,17 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
579579
is_assign,
580580
) {
581581
(Ok(l), Ok(r), false) => {
582+
let to_string = if l.starts_with("&") {
583+
// let a = String::new(); let b = String::new();
584+
// let _ = &a + b;
585+
format!("{}", &l[1..])
586+
} else {
587+
format!("{}.to_owned()", l)
588+
};
582589
err.multipart_suggestion(
583590
msg,
584591
vec![
585-
(lhs_expr.span, format!("{}.to_owned()", l)),
592+
(lhs_expr.span, to_string),
586593
(rhs_expr.span, format!("&{}", r)),
587594
],
588595
Applicability::MachineApplicable,

src/test/ui/span/issue-39018.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ LL | let _ = &a + b;
5757
| &std::string::String
5858
help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left
5959
|
60-
LL | let _ = &a.to_owned() + &b;
61-
| ^^^^^^^^^^^^^ ^^
60+
LL | let _ = a + &b;
61+
| ^ ^^
6262

6363
error[E0308]: mismatched types
6464
--> $DIR/issue-39018.rs:29:17

0 commit comments

Comments
 (0)