]> git.lizzy.rs Git - rust.git/commitdiff
Account for &String + String
authorEsteban Küber <esteban@kuber.com.ar>
Fri, 17 May 2019 17:45:54 +0000 (10:45 -0700)
committerEsteban Küber <esteban@kuber.com.ar>
Fri, 17 May 2019 17:45:54 +0000 (10:45 -0700)
src/librustc_typeck/check/op.rs
src/test/ui/span/issue-39018.stderr

index 26a5bdfef7d66b511f6da9c8fb8c2547ca3f95f9..cd207478f8f6f86e5c701df44e4257a01b2d7761 100644 (file)
@@ -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,
index cdb9c1168d8e7a2f2a5518c7b522b067075c702b..d8fbf841b6157cd5613248a65fbd22d545e8eb49 100644 (file)
@@ -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