let mut pat_ty = ty;
if let hir::ExprKind::Lit(Spanned { node: ast::LitKind::ByteStr(_), .. }) = lt.kind {
let expected = self.structurally_resolved_type(span, expected);
- if let ty::Ref(_, inner_ty, _) = expected.kind() {
- if matches!(inner_ty.kind(), ty::Slice(_)) {
- let tcx = self.tcx;
- trace!(?lt.hir_id.local_id, "polymorphic byte string lit");
- self.typeck_results
- .borrow_mut()
- .treat_byte_string_as_slice
- .insert(lt.hir_id.local_id);
- pat_ty = tcx.mk_imm_ref(tcx.lifetimes.re_static, tcx.mk_slice(tcx.types.u8));
- }
+ if let ty::Ref(_, inner_ty, _) = expected.kind()
+ && matches!(inner_ty.kind(), ty::Slice(_))
+ {
+ let tcx = self.tcx;
+ trace!(?lt.hir_id.local_id, "polymorphic byte string lit");
+ self.typeck_results
+ .borrow_mut()
+ .treat_byte_string_as_slice
+ .insert(lt.hir_id.local_id);
+ pat_ty = tcx.mk_imm_ref(tcx.lifetimes.re_static, tcx.mk_slice(tcx.types.u8));
}
}
// Unify each side with `expected`.
// Subtyping doesn't matter here, as the value is some kind of scalar.
let demand_eqtype = |x: &mut _, y| {
- if let Some((ref mut fail, x_ty, x_span)) = *x {
- if let Some(mut err) = self.demand_eqtype_pat_diag(x_span, expected, x_ty, ti) {
- if let Some((_, y_ty, y_span)) = y {
- self.endpoint_has_type(&mut err, y_span, y_ty);
- }
- err.emit();
- *fail = true;
- };
+ if let Some((ref mut fail, x_ty, x_span)) = *x
+ && let Some(mut err) = self.demand_eqtype_pat_diag(x_span, expected, x_ty, ti)
+ {
+ if let Some((_, y_ty, y_span)) = y {
+ self.endpoint_has_type(&mut err, y_span, y_ty);
+ }
+ err.emit();
+ *fail = true;
}
};
demand_eqtype(&mut lhs, rhs);
if let Some(mut err) = self.demand_eqtype_pat_diag(span, var_ty, ty, ti) {
let hir = self.tcx.hir();
let var_ty = self.resolve_vars_with_obligations(var_ty);
- let msg = format!("first introduced with type `{}` here", var_ty);
+ let msg = format!("first introduced with type `{var_ty}` here");
err.span_label(hir.span(var_id), msg);
let in_match = hir.parent_iter(var_id).any(|(_, n)| {
matches!(
{
err.span_suggestion(
*span,
- &format!("did you mean `{}`", snippet),
- format!(" &{}", expected),
+ &format!("did you mean `{snippet}`"),
+ format!(" &{expected}"),
Applicability::MachineApplicable,
);
}
"type `{}` cannot be dereferenced",
type_str
);
- err.span_label(span, format!("type `{}` cannot be dereferenced", type_str));
+ err.span_label(span, format!("type `{type_str}` cannot be dereferenced"));
if self.tcx.sess.teach(&err.get_code().unwrap()) {
err.note(CANNOT_IMPLICITLY_DEREF_POINTER_TRAIT_OBJ);
}
path_str
);
- let mut err = struct_span_err!(tcx.sess, pat.span, E0164, "{}", msg);
+ let mut err = struct_span_err!(tcx.sess, pat.span, E0164, "{msg}");
match res {
Res::Def(DefKind::Fn | DefKind::AssocFn, _) => {
err.span_label(pat.span, "`fn` calls are not allowed in patterns");
self.tcx.sess,
pat.span,
E0769,
- "tuple variant `{}` written as struct variant",
- path
+ "tuple variant `{path}` written as struct variant",
);
err.span_suggestion_verbose(
qpath.span().shrink_to_hi().to(pat.span.shrink_to_hi()),
sess,
pat.span,
E0638,
- "`..` required with {} marked as non-exhaustive",
- descr
+ "`..` required with {descr} marked as non-exhaustive",
);
err.span_suggestion_verbose(
sp_comma,
"field `{}` bound multiple times in the pattern",
ident
)
- .span_label(span, format!("multiple uses of `{}` in pattern", ident))
- .span_label(other_field, format!("first use of `{}`", ident))
+ .span_label(span, format!("multiple uses of `{ident}` in pattern"))
+ .span_label(other_field, format!("first use of `{ident}`"))
.emit();
}
&& let (Some(span), true) = (ti.span, ti.origin_expr)
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
{
- let any_target_ty = Autoderef::new(
- &self.infcx,
- self.param_env,
- self.body_id,
- span,
- self.resolve_vars_if_possible(ti.expected),
- span,
- )
- .any(|(ty, _)| {
- debug!("kind={:?}", ty.kind());
- match ty.kind() {
- ty::Adt(adt_def, _)
- if self.tcx.is_diagnostic_item(sym::Option, adt_def.did())
- || self.tcx.is_diagnostic_item(sym::Result, adt_def.did()) =>
- {
- // Slicing won't work here, but `.as_deref()` might (issue #91328).
- err.span_suggestion(
- span,
- "consider using `as_deref` here",
- format!("{snippet}.as_deref()"),
- Applicability::MaybeIncorrect,
- );
- false
- }
- _ => self.is_slice_or_array_or_vector(ty),
+ let ty = self.resolve_vars_if_possible(ti.expected);
+ let is_slice_or_array_or_vector = self.is_slice_or_array_or_vector(&mut err, snippet.clone(), ty);
+ match is_slice_or_array_or_vector.1.kind() {
+ ty::Adt(adt_def, _)
+ if self.tcx.is_diagnostic_item(sym::Option, adt_def.did())
+ || self.tcx.is_diagnostic_item(sym::Result, adt_def.did()) =>
+ {
+ // Slicing won't work here, but `.as_deref()` might (issue #91328).
+ err.span_suggestion(
+ span,
+ "consider using `as_deref` here",
+ format!("{snippet}.as_deref()"),
+ Applicability::MaybeIncorrect,
+ );
}
- });
-
- if any_target_ty {
+ _ => ()
+ }
+ if is_slice_or_array_or_vector.0 {
err.span_suggestion(
span,
"consider slicing here",
err.emit();
}
- fn is_slice_or_array_or_vector(&self, ty: Ty<'tcx>) -> bool {
+ fn is_slice_or_array_or_vector(
+ &self,
+ err: &mut Diagnostic,
+ snippet: String,
+ ty: Ty<'tcx>,
+ ) -> (bool, Ty<'tcx>) {
match ty.kind() {
- ty::Adt(adt_def, _) if self.tcx.is_diagnostic_item(sym::Vec, adt_def.did()) => true,
- ty::Ref(_, ty, _) => self.is_slice_or_array_or_vector(*ty),
- ty::Slice(..) | ty::Array(..) => true,
- _ => false,
+ ty::Adt(adt_def, _) if self.tcx.is_diagnostic_item(sym::Vec, adt_def.did()) => {
+ (true, ty)
+ }
+ ty::Ref(_, ty, _) => self.is_slice_or_array_or_vector(err, snippet, *ty),
+ ty::Slice(..) | ty::Array(..) => (true, ty),
+ _ => (false, ty),
}
}
}