Skip to content

Commit

Permalink
Rollup merge of #99486 - TaKO8Ki:remove-type-string-comparison-in-che…
Browse files Browse the repository at this point in the history
…ck-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.
  • Loading branch information
Dylan-DPC authored Jul 20, 2022
2 parents 3257c34 + 3511856 commit 17a2832
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions compiler/rustc_typeck/src/check/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 17a2832

Please sign in to comment.