Skip to content

Commit 5924ef8

Browse files
committed
stop using Autoderef
1 parent a59cc57 commit 5924ef8

File tree

1 file changed

+29
-33
lines changed
  • compiler/rustc_typeck/src/check

1 file changed

+29
-33
lines changed

compiler/rustc_typeck/src/check/pat.rs

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,35 +2055,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20552055
&& let (Some(span), true) = (ti.span, ti.origin_expr)
20562056
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
20572057
{
2058-
let any_target_ty = Autoderef::new(
2059-
&self.infcx,
2060-
self.param_env,
2061-
self.body_id,
2062-
span,
2063-
self.resolve_vars_if_possible(ti.expected),
2064-
span,
2065-
)
2066-
.any(|(ty, _)| {
2067-
debug!("kind={:?}", ty.kind());
2068-
match ty.kind() {
2069-
ty::Adt(adt_def, _)
2070-
if self.tcx.is_diagnostic_item(sym::Option, adt_def.did())
2071-
|| self.tcx.is_diagnostic_item(sym::Result, adt_def.did()) =>
2072-
{
2073-
// Slicing won't work here, but `.as_deref()` might (issue #91328).
2074-
err.span_suggestion(
2075-
span,
2076-
"consider using `as_deref` here",
2077-
format!("{snippet}.as_deref()"),
2078-
Applicability::MaybeIncorrect,
2079-
);
2080-
false
2081-
}
2082-
_ => self.is_slice_or_array_or_vector(ty),
2058+
let ty = self.resolve_vars_if_possible(ti.expected);
2059+
let is_slice_or_array_or_vector = self.is_slice_or_array_or_vector(&mut err, snippet.clone(), ty);
2060+
match is_slice_or_array_or_vector.1.kind() {
2061+
ty::Adt(adt_def, _)
2062+
if self.tcx.is_diagnostic_item(sym::Option, adt_def.did())
2063+
|| self.tcx.is_diagnostic_item(sym::Result, adt_def.did()) =>
2064+
{
2065+
// Slicing won't work here, but `.as_deref()` might (issue #91328).
2066+
err.span_suggestion(
2067+
span,
2068+
"consider using `as_deref` here",
2069+
format!("{snippet}.as_deref()"),
2070+
Applicability::MaybeIncorrect,
2071+
);
20832072
}
2084-
});
2085-
2086-
if any_target_ty {
2073+
_ => ()
2074+
}
2075+
if is_slice_or_array_or_vector.0 {
20872076
err.span_suggestion(
20882077
span,
20892078
"consider slicing here",
@@ -2096,12 +2085,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20962085
err.emit();
20972086
}
20982087

2099-
fn is_slice_or_array_or_vector(&self, ty: Ty<'tcx>) -> bool {
2088+
fn is_slice_or_array_or_vector(
2089+
&self,
2090+
err: &mut Diagnostic,
2091+
snippet: String,
2092+
ty: Ty<'tcx>,
2093+
) -> (bool, Ty<'tcx>) {
21002094
match ty.kind() {
2101-
ty::Adt(adt_def, _) if self.tcx.is_diagnostic_item(sym::Vec, adt_def.did()) => true,
2102-
ty::Ref(_, ty, _) => self.is_slice_or_array_or_vector(*ty),
2103-
ty::Slice(..) | ty::Array(..) => true,
2104-
_ => false,
2095+
ty::Adt(adt_def, _) if self.tcx.is_diagnostic_item(sym::Vec, adt_def.did()) => {
2096+
(true, ty)
2097+
}
2098+
ty::Ref(_, ty, _) => self.is_slice_or_array_or_vector(err, snippet, *ty),
2099+
ty::Slice(..) | ty::Array(..) => (true, ty),
2100+
_ => (false, ty),
21052101
}
21062102
}
21072103
}

0 commit comments

Comments
 (0)