Skip to content

Commit 17a2832

Browse files
authored
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.
2 parents 3257c34 + 3511856 commit 17a2832

File tree

1 file changed

+9
-8
lines changed
  • compiler/rustc_typeck/src/check

1 file changed

+9
-8
lines changed

compiler/rustc_typeck/src/check/op.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -630,18 +630,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
630630
let rm_borrow_msg = "remove the borrow to obtain an owned `String`";
631631
let to_owned_msg = "create an owned `String` from a string reference";
632632

633-
let string_type = self.tcx.get_diagnostic_item(sym::String);
634-
let is_std_string = |ty: Ty<'tcx>| match ty.ty_adt_def() {
635-
Some(ty_def) => Some(ty_def.did()) == string_type,
636-
None => false,
633+
let is_std_string = |ty: Ty<'tcx>| {
634+
ty.ty_adt_def()
635+
.map_or(false, |ty_def| self.tcx.is_diagnostic_item(sym::String, ty_def.did()))
637636
};
638637

639638
match (lhs_ty.kind(), rhs_ty.kind()) {
640639
(&Ref(_, l_ty, _), &Ref(_, r_ty, _)) // &str or &String + &str, &String or &&str
641-
if (*l_ty.kind() == Str || is_std_string(l_ty)) && (
642-
*r_ty.kind() == Str || is_std_string(r_ty) ||
643-
&format!("{:?}", rhs_ty) == "&&str"
644-
) =>
640+
if (*l_ty.kind() == Str || is_std_string(l_ty))
641+
&& (*r_ty.kind() == Str
642+
|| is_std_string(r_ty)
643+
|| matches!(
644+
r_ty.kind(), Ref(_, inner_ty, _) if *inner_ty.kind() == Str
645+
)) =>
645646
{
646647
if let IsAssign::No = is_assign { // Do not supply this message if `&str += &str`
647648
err.span_label(op.span, "`+` cannot be used to concatenate two `&str` strings");

0 commit comments

Comments
 (0)