From: Esteban Küber Date: Fri, 17 May 2019 17:45:54 +0000 (-0700) Subject: Account for &String + String X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=8895fb945d0ea0ce124923aadc16adb68c74f3e6;p=rust.git Account for &String + String --- diff --git a/src/librustc_typeck/check/op.rs b/src/librustc_typeck/check/op.rs index 26a5bdfef7d..cd207478f8f 100644 --- a/src/librustc_typeck/check/op.rs +++ b/src/librustc_typeck/check/op.rs @@ -579,10 +579,17 @@ fn check_str_addition( is_assign, ) { (Ok(l), Ok(r), false) => { + let to_string = if l.starts_with("&") { + // let a = String::new(); let b = String::new(); + // let _ = &a + b; + format!("{}", &l[1..]) + } else { + format!("{}.to_owned()", l) + }; err.multipart_suggestion( msg, vec![ - (lhs_expr.span, format!("{}.to_owned()", l)), + (lhs_expr.span, to_string), (rhs_expr.span, format!("&{}", r)), ], Applicability::MachineApplicable, diff --git a/src/test/ui/span/issue-39018.stderr b/src/test/ui/span/issue-39018.stderr index cdb9c1168d8..d8fbf841b61 100644 --- a/src/test/ui/span/issue-39018.stderr +++ b/src/test/ui/span/issue-39018.stderr @@ -57,8 +57,8 @@ LL | let _ = &a + b; | &std::string::String 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 | -LL | let _ = &a.to_owned() + &b; - | ^^^^^^^^^^^^^ ^^ +LL | let _ = a + &b; + | ^ ^^ error[E0308]: mismatched types --> $DIR/issue-39018.rs:29:17