From ee0bf5e6aa7a964bb5c776adba0faad087a6a425 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 16 May 2019 20:05:00 -0700 Subject: [PATCH] review comments --- src/librustc_typeck/check/op.rs | 20 +++++++++---------- src/test/ui/issues/issue-47377.stderr | 2 +- src/test/ui/issues/issue-47380.stderr | 2 +- src/test/ui/span/issue-39018.stderr | 20 +++++++++---------- .../ui/str/str-concat-on-double-ref.stderr | 2 +- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/librustc_typeck/check/op.rs b/src/librustc_typeck/check/op.rs index 619900b6ad903..26a5bdfef7d66 100644 --- a/src/librustc_typeck/check/op.rs +++ b/src/librustc_typeck/check/op.rs @@ -527,19 +527,20 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { 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"; - debug!("check_str_addition: {:?} + {:?}", lhs_ty, rhs_ty); + + let is_std_string = |ty| &format!("{:?}", ty) == "std::string::String"; + match (&lhs_ty.sty, &rhs_ty.sty) { (&Ref(_, l_ty, _), &Ref(_, r_ty, _)) // &str or &String + &str, &String or &&str - if (l_ty.sty == Str || &format!("{:?}", l_ty) == "std::string::String") && ( - r_ty.sty == Str || - &format!("{:?}", r_ty) == "std::string::String" || - &format!("{:?}", rhs_ty) == "&&str" - ) => + if (l_ty.sty == Str || is_std_string(l_ty)) && ( + r_ty.sty == Str || is_std_string(r_ty) || + &format!("{:?}", rhs_ty) == "&&str" + ) => { if !is_assign { // Do not supply this message if `&str += &str` err.span_label( op.span, - "`+` can't be used to concatenate two `&str` strings", + "`+` cannot be used to concatenate two `&str` strings", ); match source_map.span_to_snippet(lhs_expr.span) { Ok(lstring) => { @@ -566,12 +567,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { true } (&Ref(_, l_ty, _), &Adt(..)) // Handle `&str` & `&String` + `String` - if (l_ty.sty == Str || &format!("{:?}", l_ty) == "std::string::String") && - &format!("{:?}", rhs_ty) == "std::string::String" => + if (l_ty.sty == Str || is_std_string(l_ty)) && is_std_string(rhs_ty) => { err.span_label( op.span, - "`+` can't be used to concatenate a `&str` with a `String`", + "`+` cannot be used to concatenate a `&str` with a `String`", ); match ( source_map.span_to_snippet(lhs_expr.span), diff --git a/src/test/ui/issues/issue-47377.stderr b/src/test/ui/issues/issue-47377.stderr index 88466131e3144..7d11a8c802128 100644 --- a/src/test/ui/issues/issue-47377.stderr +++ b/src/test/ui/issues/issue-47377.stderr @@ -4,7 +4,7 @@ error[E0369]: binary operation `+` cannot be applied to type `&str` LL | let _a = b + ", World!"; | - ^ ---------- &str | | | - | | `+` can't be used to concatenate two `&str` strings + | | `+` cannot be used to concatenate two `&str` strings | &str 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 | diff --git a/src/test/ui/issues/issue-47380.stderr b/src/test/ui/issues/issue-47380.stderr index d69101eab4c46..89a154c5109d8 100644 --- a/src/test/ui/issues/issue-47380.stderr +++ b/src/test/ui/issues/issue-47380.stderr @@ -4,7 +4,7 @@ error[E0369]: binary operation `+` cannot be applied to type `&str` LL | println!("🦀🦀🦀🦀🦀"); let _a = b + ", World!"; | - ^ ---------- &str | | | - | | `+` can't be used to concatenate two `&str` strings + | | `+` cannot be used to concatenate two `&str` strings | &str 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 | diff --git a/src/test/ui/span/issue-39018.stderr b/src/test/ui/span/issue-39018.stderr index b174e95c8a1d4..cdb9c1168d8e7 100644 --- a/src/test/ui/span/issue-39018.stderr +++ b/src/test/ui/span/issue-39018.stderr @@ -4,7 +4,7 @@ error[E0369]: binary operation `+` cannot be applied to type `&str` LL | let x = "Hello " + "World!"; | -------- ^ -------- &str | | | - | | `+` can't be used to concatenate two `&str` strings + | | `+` cannot be used to concatenate two `&str` strings | &str 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 | @@ -27,7 +27,7 @@ error[E0369]: binary operation `+` cannot be applied to type `&str` LL | let x = "Hello " + "World!".to_owned(); | -------- ^ ------------------- std::string::String | | | - | | `+` can't be used to concatenate a `&str` with a `String` + | | `+` cannot be used to concatenate a `&str` with a `String` | &str 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 | @@ -40,7 +40,7 @@ error[E0369]: binary operation `+` cannot be applied to type `&std::string::Stri LL | let _ = &a + &b; | -- ^ -- &std::string::String | | | - | | `+` can't be used to concatenate two `&str` strings + | | `+` cannot be used to concatenate two `&str` strings | &std::string::String help: 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 | @@ -53,7 +53,7 @@ error[E0369]: binary operation `+` cannot be applied to type `&std::string::Stri LL | let _ = &a + b; | -- ^ - std::string::String | | | - | | `+` can't be used to concatenate a `&str` with a `String` + | | `+` cannot be used to concatenate a `&str` with a `String` | &std::string::String 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 | @@ -78,7 +78,7 @@ error[E0369]: binary operation `+` cannot be applied to type `&std::string::Stri LL | let _ = e + b; | - ^ - std::string::String | | | - | | `+` can't be used to concatenate a `&str` with a `String` + | | `+` cannot be used to concatenate a `&str` with a `String` | &std::string::String 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 | @@ -91,7 +91,7 @@ error[E0369]: binary operation `+` cannot be applied to type `&std::string::Stri LL | let _ = e + &b; | - ^ -- &std::string::String | | | - | | `+` can't be used to concatenate two `&str` strings + | | `+` cannot be used to concatenate two `&str` strings | &std::string::String 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 | @@ -104,7 +104,7 @@ error[E0369]: binary operation `+` cannot be applied to type `&std::string::Stri LL | let _ = e + d; | - ^ - &str | | | - | | `+` can't be used to concatenate two `&str` strings + | | `+` cannot be used to concatenate two `&str` strings | &std::string::String 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 | @@ -117,7 +117,7 @@ error[E0369]: binary operation `+` cannot be applied to type `&std::string::Stri LL | let _ = e + &d; | - ^ -- &&str | | | - | | `+` can't be used to concatenate two `&str` strings + | | `+` cannot be used to concatenate two `&str` strings | &std::string::String 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 | @@ -150,7 +150,7 @@ error[E0369]: binary operation `+` cannot be applied to type `&str` LL | let _ = c + &d; | - ^ -- &&str | | | - | | `+` can't be used to concatenate two `&str` strings + | | `+` cannot be used to concatenate two `&str` strings | &str 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 | @@ -163,7 +163,7 @@ error[E0369]: binary operation `+` cannot be applied to type `&str` LL | let _ = c + d; | - ^ - &str | | | - | | `+` can't be used to concatenate two `&str` strings + | | `+` cannot be used to concatenate two `&str` strings | &str 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 | diff --git a/src/test/ui/str/str-concat-on-double-ref.stderr b/src/test/ui/str/str-concat-on-double-ref.stderr index 17d9475b9c07d..3e53cdc4d98ca 100644 --- a/src/test/ui/str/str-concat-on-double-ref.stderr +++ b/src/test/ui/str/str-concat-on-double-ref.stderr @@ -4,7 +4,7 @@ error[E0369]: binary operation `+` cannot be applied to type `&std::string::Stri LL | let c = a + b; | - ^ - &str | | | - | | `+` can't be used to concatenate two `&str` strings + | | `+` cannot be used to concatenate two `&str` strings | &std::string::String 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 |