about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2022-07-20 16:17:23 +0530
committerGitHub <noreply@github.com>2022-07-20 16:17:23 +0530
commit17a2832ba0b2596db7b23d55f68b581eb7e80254 (patch)
treed048ac8eac17d0e0df23a51f08f7052fa26ded1e
parent3257c3401d68057e83089f3b8da27f7ecc21e228 (diff)
parent35118568a6a9d81c0ce0ad1c9f579b7da1f6022d (diff)
downloadrust-17a2832ba0b2596db7b23d55f68b581eb7e80254.tar.gz
rust-17a2832ba0b2596db7b23d55f68b581eb7e80254.zip
Rollup merge of #99486 - TaKO8Ki:remove-type-string-comparison-in-check-str-addition, r=compiler-errors
Refactor: remove a string comparison between types in `check_str_addition`

This patch removes remove a string of types comparison.
-rw-r--r--compiler/rustc_typeck/src/check/op.rs17
1 files changed, 9 insertions, 8 deletions
diff --git a/compiler/rustc_typeck/src/check/op.rs b/compiler/rustc_typeck/src/check/op.rs
index a2daf6886f1..9858cd8fa19 100644
--- a/compiler/rustc_typeck/src/check/op.rs
+++ b/compiler/rustc_typeck/src/check/op.rs
@@ -630,18 +630,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
         let rm_borrow_msg = "remove the borrow to obtain an owned `String`";
         let to_owned_msg = "create an owned `String` from a string reference";
 
-        let string_type = self.tcx.get_diagnostic_item(sym::String);
-        let is_std_string = |ty: Ty<'tcx>| match ty.ty_adt_def() {
-            Some(ty_def) => Some(ty_def.did()) == string_type,
-            None => false,
+        let is_std_string = |ty: Ty<'tcx>| {
+            ty.ty_adt_def()
+                .map_or(false, |ty_def| self.tcx.is_diagnostic_item(sym::String, ty_def.did()))
         };
 
         match (lhs_ty.kind(), rhs_ty.kind()) {
             (&Ref(_, l_ty, _), &Ref(_, r_ty, _)) // &str or &String + &str, &String or &&str
-                if (*l_ty.kind() == Str || is_std_string(l_ty)) && (
-                        *r_ty.kind() == Str || is_std_string(r_ty) ||
-                        &format!("{:?}", rhs_ty) == "&&str"
-                    ) =>
+                if (*l_ty.kind() == Str || is_std_string(l_ty))
+                    && (*r_ty.kind() == Str
+                        || is_std_string(r_ty)
+                        || matches!(
+                            r_ty.kind(), Ref(_, inner_ty, _) if *inner_ty.kind() == Str
+                        )) =>
             {
                 if let IsAssign::No = is_assign { // Do not supply this message if `&str += &str`
                     err.span_label(op.span, "`+` cannot be used to concatenate two `&str` strings");