Skip to content

Commit

Permalink
Simplify a suggestion for str addition
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Apr 25, 2017
1 parent b857a1a commit ca701d7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
17 changes: 7 additions & 10 deletions src/librustc_typeck/check/op.rs
Expand Up @@ -244,8 +244,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {

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
Expand All @@ -266,7 +266,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}

fn check_str_addition(&self,
expr: &'gcx hir::Expr,
lhs_expr: &'gcx hir::Expr,
lhs_ty: Ty<'tcx>,
rhs_expr: &'gcx hir::Expr,
Expand All @@ -281,18 +280,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
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;
}

Expand Down
4 changes: 1 addition & 3 deletions src/test/ui/span/issue-39018.stderr
Expand Up @@ -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

Expand Down

0 comments on commit ca701d7

Please sign in to comment.