]> git.lizzy.rs Git - rust.git/commitdiff
Simplify a suggestion for str addition
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Mon, 27 Mar 2017 12:39:44 +0000 (14:39 +0200)
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>
Tue, 25 Apr 2017 09:07:42 +0000 (11:07 +0200)
src/librustc_typeck/check/op.rs
src/test/ui/span/issue-39018.stderr

index 42296006b79d189c834ab26eb1f019042db97b84..6968a37361a1c4a35d19fe9841171cfd9349b140 100644 (file)
@@ -244,8 +244,8 @@ fn check_overloaded_binop(&self,
 
                         if let Some(missing_trait) = missing_trait {
                             if missing_trait == "std::ops::Add" &&
-                                self.check_str_addition(expr, lhs_expr, lhs_ty,
-                                                         rhs_expr, rhs_ty, &mut err) {
+                                self.check_str_addition(lhs_expr, lhs_ty,
+                                                         rhs_expr, rhs_ty_var, &mut err) {
                                 // This has nothing here because it means we did string
                                 // concatenation (e.g. "Hello " + "World!"). This means
                                 // we don't want the note in the else clause to be emitted
@@ -266,7 +266,6 @@ fn check_overloaded_binop(&self,
     }
 
     fn check_str_addition(&self,
-                          expr: &'gcx hir::Expr,
                           lhs_expr: &'gcx hir::Expr,
                           lhs_ty: Ty<'tcx>,
                           rhs_expr: &'gcx hir::Expr,
@@ -281,18 +280,16 @@ fn check_str_addition(&self,
                     err.note("`+` can't be used to concatenate two `&str` strings");
                     let codemap = self.tcx.sess.codemap();
                     let suggestion =
-                        match (codemap.span_to_snippet(lhs_expr.span),
-                                codemap.span_to_snippet(rhs_expr.span)) {
-                            (Ok(lstring), Ok(rstring)) =>
-                                format!("{}.to_owned() + {}", lstring, rstring),
+                        match codemap.span_to_snippet(lhs_expr.span) {
+                            Ok(lstring) => format!("{}.to_owned()", lstring),
                             _ => format!("<expression>")
                         };
-                    err.span_suggestion(expr.span,
-                        &format!("to_owned() can be used to create an owned `String` \
+                    err.span_suggestion(lhs_expr.span,
+                        &format!("`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."), suggestion);
+                                  requires ownership of the string on the left:"), suggestion);
                     is_string_addition = true;
                 }
 
index 46d39dac5aca718a5402b37e8bca3ccf5aed033e..b588238202e1285449f2464a8b29754bd0726334 100644 (file)
@@ -2,9 +2,7 @@ error[E0369]: binary operation `+` cannot be applied to type `&'static str`
   --> $DIR/issue-39018.rs:12:13
    |
 12 |     let x = "Hello " + "World!";
-   |             ^^^^^^^^-----------
-   |             |
-   |             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. `"Hello ".to_owned() + "World!"`
+   |             ^^^^^^^^ `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: `"Hello ".to_owned()`
    |
    = note: `+` can't be used to concatenate two `&str` strings