Skip to content

Commit ca701d7

Browse files
committed
Simplify a suggestion for str addition
1 parent b857a1a commit ca701d7

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

src/librustc_typeck/check/op.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
244244

245245
if let Some(missing_trait) = missing_trait {
246246
if missing_trait == "std::ops::Add" &&
247-
self.check_str_addition(expr, lhs_expr, lhs_ty,
248-
rhs_expr, rhs_ty, &mut err) {
247+
self.check_str_addition(lhs_expr, lhs_ty,
248+
rhs_expr, rhs_ty_var, &mut err) {
249249
// This has nothing here because it means we did string
250250
// concatenation (e.g. "Hello " + "World!"). This means
251251
// we don't want the note in the else clause to be emitted
@@ -266,7 +266,6 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
266266
}
267267

268268
fn check_str_addition(&self,
269-
expr: &'gcx hir::Expr,
270269
lhs_expr: &'gcx hir::Expr,
271270
lhs_ty: Ty<'tcx>,
272271
rhs_expr: &'gcx hir::Expr,
@@ -281,18 +280,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
281280
err.note("`+` can't be used to concatenate two `&str` strings");
282281
let codemap = self.tcx.sess.codemap();
283282
let suggestion =
284-
match (codemap.span_to_snippet(lhs_expr.span),
285-
codemap.span_to_snippet(rhs_expr.span)) {
286-
(Ok(lstring), Ok(rstring)) =>
287-
format!("{}.to_owned() + {}", lstring, rstring),
283+
match codemap.span_to_snippet(lhs_expr.span) {
284+
Ok(lstring) => format!("{}.to_owned()", lstring),
288285
_ => format!("<expression>")
289286
};
290-
err.span_suggestion(expr.span,
291-
&format!("to_owned() can be used to create an owned `String` \
287+
err.span_suggestion(lhs_expr.span,
288+
&format!("`to_owned()` can be used to create an owned `String` \
292289
from a string reference. String concatenation \
293290
appends the string on the right to the string \
294291
on the left and may require reallocation. This \
295-
requires ownership of the string on the left."), suggestion);
292+
requires ownership of the string on the left:"), suggestion);
296293
is_string_addition = true;
297294
}
298295

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ error[E0369]: binary operation `+` cannot be applied to type `&'static str`
22
--> $DIR/issue-39018.rs:12:13
33
|
44
12 | let x = "Hello " + "World!";
5-
| ^^^^^^^^-----------
6-
| |
7-
| 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!"`
5+
| ^^^^^^^^ `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()`
86
|
97
= note: `+` can't be used to concatenate two `&str` strings
108

0 commit comments

Comments
 (0)