Skip to content

Commit 5b7e3a1

Browse files
authored
Rollup merge of rust-lang#52605 - estebank:str-plus-eq, r=oli-obk
Do not suggest using `to_owned()` on `&str += &str` - Don't provide incorrect suggestion for `&str += &str` (fix rust-lang#52410) - On `&str + String` suggest `&str.to_owned() + &String` as a single suggestion
2 parents a98c19e + 9369b52 commit 5b7e3a1

File tree

3 files changed

+38
-42
lines changed

3 files changed

+38
-42
lines changed

src/librustc_typeck/check/op.rs

+36-31
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
307307
if let Some(missing_trait) = missing_trait {
308308
if op.node == hir::BinOpKind::Add &&
309309
self.check_str_addition(expr, lhs_expr, rhs_expr, lhs_ty,
310-
rhs_ty, &mut err) {
310+
rhs_ty, &mut err, true) {
311311
// This has nothing here because it means we did string
312-
// concatenation (e.g. "Hello " + "World!"). This means
312+
// concatenation (e.g. "Hello " += "World!"). This means
313313
// we don't want the note in the else clause to be emitted
314314
} else if let ty::TyParam(_) = lhs_ty.sty {
315315
// FIXME: point to span of param
@@ -381,7 +381,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
381381
if let Some(missing_trait) = missing_trait {
382382
if op.node == hir::BinOpKind::Add &&
383383
self.check_str_addition(expr, lhs_expr, rhs_expr, lhs_ty,
384-
rhs_ty, &mut err) {
384+
rhs_ty, &mut err, false) {
385385
// This has nothing here because it means we did string
386386
// concatenation (e.g. "Hello " + "World!"). This means
387387
// we don't want the note in the else clause to be emitted
@@ -410,13 +410,16 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
410410
(lhs_ty, rhs_ty, return_ty)
411411
}
412412

413-
fn check_str_addition(&self,
414-
expr: &'gcx hir::Expr,
415-
lhs_expr: &'gcx hir::Expr,
416-
rhs_expr: &'gcx hir::Expr,
417-
lhs_ty: Ty<'tcx>,
418-
rhs_ty: Ty<'tcx>,
419-
err: &mut errors::DiagnosticBuilder) -> bool {
413+
fn check_str_addition(
414+
&self,
415+
expr: &'gcx hir::Expr,
416+
lhs_expr: &'gcx hir::Expr,
417+
rhs_expr: &'gcx hir::Expr,
418+
lhs_ty: Ty<'tcx>,
419+
rhs_ty: Ty<'tcx>,
420+
err: &mut errors::DiagnosticBuilder,
421+
is_assign: bool,
422+
) -> bool {
420423
let codemap = self.tcx.sess.codemap();
421424
let msg = "`to_owned()` can be used to create an owned `String` \
422425
from a string reference. String concatenation \
@@ -428,34 +431,36 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
428431
match (&lhs_ty.sty, &rhs_ty.sty) {
429432
(&TyRef(_, l_ty, _), &TyRef(_, r_ty, _))
430433
if l_ty.sty == TyStr && r_ty.sty == TyStr => {
431-
err.span_label(expr.span,
432-
"`+` can't be used to concatenate two `&str` strings");
433-
match codemap.span_to_snippet(lhs_expr.span) {
434-
Ok(lstring) => err.span_suggestion(lhs_expr.span,
435-
msg,
436-
format!("{}.to_owned()", lstring)),
437-
_ => err.help(msg),
438-
};
434+
if !is_assign {
435+
err.span_label(expr.span,
436+
"`+` can't be used to concatenate two `&str` strings");
437+
match codemap.span_to_snippet(lhs_expr.span) {
438+
Ok(lstring) => err.span_suggestion(lhs_expr.span,
439+
msg,
440+
format!("{}.to_owned()", lstring)),
441+
_ => err.help(msg),
442+
};
443+
}
439444
true
440445
}
441446
(&TyRef(_, l_ty, _), &TyAdt(..))
442447
if l_ty.sty == TyStr && &format!("{:?}", rhs_ty) == "std::string::String" => {
443448
err.span_label(expr.span,
444449
"`+` can't be used to concatenate a `&str` with a `String`");
445-
match codemap.span_to_snippet(lhs_expr.span) {
446-
Ok(lstring) => err.span_suggestion(lhs_expr.span,
447-
msg,
448-
format!("{}.to_owned()", lstring)),
449-
_ => err.help(msg),
450-
};
451-
match codemap.span_to_snippet(rhs_expr.span) {
452-
Ok(rstring) => {
453-
err.span_suggestion(rhs_expr.span,
454-
"you also need to borrow the `String` on the right to \
455-
get a `&str`",
456-
format!("&{}", rstring));
450+
match (
451+
codemap.span_to_snippet(lhs_expr.span),
452+
codemap.span_to_snippet(rhs_expr.span),
453+
is_assign,
454+
) {
455+
(Ok(l), Ok(r), false) => {
456+
err.multipart_suggestion(msg, vec![
457+
(lhs_expr.span, format!("{}.to_owned()", l)),
458+
(rhs_expr.span, format!("&{}", r)),
459+
]);
460+
}
461+
_ => {
462+
err.help(msg);
457463
}
458-
_ => {}
459464
};
460465
true
461466
}

src/test/ui/issue-10401.stderr

-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ LL | a += { "b" };
55
| -^^^^^^^^^^^
66
| |
77
| cannot use `+=` on type `&str`
8-
| `+` can't be used to concatenate two `&str` strings
9-
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
10-
|
11-
LL | a.to_owned() += { "b" };
12-
| ^^^^^^^^^^^^
138

149
error: aborting due to previous error
1510

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

+2-6
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,8 @@ LL | let x = "Hello " + "World!".to_owned();
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `+` can't be used to concatenate a `&str` with a `String`
2424
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
2525
|
26-
LL | 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-
LL | let x = "Hello " + &"World!".to_owned();
31-
| ^^^^^^^^^^^^^^^^^^^^
26+
LL | let x = "Hello ".to_owned() + &"World!".to_owned();
27+
| ^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
3228

3329
error: aborting due to 3 previous errors
3430

0 commit comments

Comments
 (0)