Skip to content

Commit 3511856

Browse files
committed
remove a type string comparison in check_str_addition
1 parent 475aec1 commit 3511856

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
@@ -631,18 +631,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
631631
let rm_borrow_msg = "remove the borrow to obtain an owned `String`";
632632
let to_owned_msg = "create an owned `String` from a string reference";
633633

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

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

0 commit comments

Comments
 (0)