Skip to content

Commit 20bc72e

Browse files
committed
Handle custom diagnostic for &str + String
1 parent 1670a53 commit 20bc72e

File tree

3 files changed

+61
-27
lines changed

3 files changed

+61
-27
lines changed

src/librustc_typeck/check/op.rs

+43-26
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use super::{FnCtxt, Needs};
1414
use super::method::MethodCallee;
1515
use rustc::ty::{self, Ty, TypeFoldable, TypeVariants};
16-
use rustc::ty::TypeVariants::{TyStr, TyRef};
16+
use rustc::ty::TypeVariants::{TyStr, TyRef, TyAdt};
1717
use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow, AutoBorrowMutability};
1818
use rustc::infer::type_variable::TypeVariableOrigin;
1919
use errors;
@@ -301,7 +301,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
301301

302302
if let Some(missing_trait) = missing_trait {
303303
if missing_trait == "std::ops::Add" &&
304-
self.check_str_addition(expr, lhs_expr, lhs_ty,
304+
self.check_str_addition(expr, lhs_expr, rhs_expr, lhs_ty,
305305
rhs_ty, &mut err) {
306306
// This has nothing here because it means we did string
307307
// concatenation (e.g. "Hello " + "World!"). This means
@@ -330,37 +330,54 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
330330
fn check_str_addition(&self,
331331
expr: &'gcx hir::Expr,
332332
lhs_expr: &'gcx hir::Expr,
333+
rhs_expr: &'gcx hir::Expr,
333334
lhs_ty: Ty<'tcx>,
334335
rhs_ty: Ty<'tcx>,
335336
err: &mut errors::DiagnosticBuilder) -> bool {
337+
let codemap = self.tcx.sess.codemap();
338+
let msg = "`to_owned()` can be used to create an owned `String` \
339+
from a string reference. String concatenation \
340+
appends the string on the right to the string \
341+
on the left and may require reallocation. This \
342+
requires ownership of the string on the left";
336343
// If this function returns true it means a note was printed, so we don't need
337344
// to print the normal "implementation of `std::ops::Add` might be missing" note
338-
let mut is_string_addition = false;
339-
if let TyRef(_, l_ty) = lhs_ty.sty {
340-
if let TyRef(_, r_ty) = rhs_ty.sty {
341-
if l_ty.ty.sty == TyStr && r_ty.ty.sty == TyStr {
342-
err.span_label(expr.span,
343-
"`+` can't be used to concatenate two `&str` strings");
344-
let codemap = self.tcx.sess.codemap();
345-
let suggestion =
346-
match codemap.span_to_snippet(lhs_expr.span) {
347-
Ok(lstring) => format!("{}.to_owned()", lstring),
348-
_ => format!("<expression>")
349-
};
350-
err.span_suggestion(lhs_expr.span,
351-
&format!("`to_owned()` can be used to create an owned `String` \
352-
from a string reference. String concatenation \
353-
appends the string on the right to the string \
354-
on the left and may require reallocation. This \
355-
requires ownership of the string on the left"), suggestion);
356-
is_string_addition = true;
357-
}
358-
345+
match (&lhs_ty.sty, &rhs_ty.sty) {
346+
(&TyRef(_, ref l_ty), &TyRef(_, ref r_ty))
347+
if l_ty.ty.sty == TyStr && r_ty.ty.sty == TyStr => {
348+
err.span_label(expr.span,
349+
"`+` can't be used to concatenate two `&str` strings");
350+
match codemap.span_to_snippet(lhs_expr.span) {
351+
Ok(lstring) => err.span_suggestion(lhs_expr.span,
352+
msg,
353+
format!("{}.to_owned()", lstring)),
354+
_ => err.help(msg),
355+
};
356+
true
359357
}
360-
358+
(&TyRef(_, ref l_ty), &TyAdt(..))
359+
if l_ty.ty.sty == TyStr && &format!("{:?}", rhs_ty) == "std::string::String" => {
360+
err.span_label(expr.span,
361+
"`+` can't be used to concatenate a `&str` with a `String`");
362+
match codemap.span_to_snippet(lhs_expr.span) {
363+
Ok(lstring) => err.span_suggestion(lhs_expr.span,
364+
msg,
365+
format!("{}.to_owned()", lstring)),
366+
_ => err.help(msg),
367+
};
368+
match codemap.span_to_snippet(rhs_expr.span) {
369+
Ok(rstring) => {
370+
err.span_suggestion(rhs_expr.span,
371+
"you also need to borrow the `String` on the right to \
372+
get a `&str`",
373+
format!("&{}", rstring));
374+
}
375+
_ => {}
376+
};
377+
true
378+
}
379+
_ => false,
361380
}
362-
363-
is_string_addition
364381
}
365382

366383
pub fn check_user_unop(&self,

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

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ pub fn main() {
1717
// that won't output for the above string concatenation
1818
let y = World::Hello + World::Goodbye;
1919
//~^ ERROR cannot be applied to type
20+
21+
let x = "Hello " + "World!".to_owned();
22+
//~^ ERROR cannot be applied to type
2023
}
2124

2225
enum World {

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,19 @@ error[E0369]: binary operation `+` cannot be applied to type `World`
1616
|
1717
= note: an implementation of `std::ops::Add` might be missing for `World`
1818

19-
error: aborting due to 2 previous errors
19+
error[E0369]: binary operation `+` cannot be applied to type `&str`
20+
--> $DIR/issue-39018.rs:21:13
21+
|
22+
21 | let x = "Hello " + "World!".to_owned();
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `+` can't be used to concatenate a `&str` with a `String`
24+
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
25+
|
26+
21 | let x = "Hello ".to_owned() + "World!".to_owned();
27+
| ^^^^^^^^^^^^^^^^^^^
28+
help: you also need to borrow the `String` on the right to get a `&str`
29+
|
30+
21 | let x = "Hello " + &"World!".to_owned();
31+
| ^^^^^^^^^^^^^^^^^^^^
32+
33+
error: aborting due to 3 previous errors
2034

0 commit comments

Comments
 (0)