Skip to content

Commit 83b97ae

Browse files
committed
Use clippy_utils::ty::ty_from_hir_ty to avoid ICE
1 parent daab21e commit 83b97ae

File tree

1 file changed

+2
-25
lines changed

1 file changed

+2
-25
lines changed

clippy_lints/src/use_self.rs

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::is_from_proc_macro;
44
use clippy_utils::msrvs::{self, Msrv};
5-
use clippy_utils::ty::same_type_and_consts;
5+
use clippy_utils::ty::{same_type_and_consts, ty_from_hir_ty};
66
use rustc_data_structures::fx::FxHashSet;
77
use rustc_errors::Applicability;
88
use rustc_hir::def::{CtorOf, DefKind, Res};
@@ -12,7 +12,6 @@ use rustc_hir::{
1212
self as hir, AmbigArg, Expr, ExprKind, FnRetTy, FnSig, GenericArgsParentheses, GenericParam, GenericParamKind,
1313
HirId, Impl, ImplItemKind, Item, ItemKind, Pat, PatKind, Path, QPath, Ty, TyKind,
1414
};
15-
use rustc_hir_analysis::lower_ty;
1615
use rustc_lint::{LateContext, LateLintPass};
1716
use rustc_middle::ty::Ty as MiddleTy;
1817
use rustc_session::impl_lint_pass;
@@ -73,7 +72,6 @@ impl UseSelf {
7372
enum StackItem {
7473
Check {
7574
impl_id: LocalDefId,
76-
in_body: u32,
7775
types_to_skip: FxHashSet<HirId>,
7876
},
7977
NoCheck,
@@ -117,7 +115,6 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
117115
.collect();
118116
StackItem::Check {
119117
impl_id: item.owner_id.def_id,
120-
in_body: 0,
121118
types_to_skip,
122119
}
123120
} else {
@@ -186,27 +183,12 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
186183
}
187184
}
188185

189-
fn check_body(&mut self, _: &LateContext<'_>, _: &hir::Body<'_>) {
190-
// `lower_ty` cannot be called in `Body`s or it will panic (sometimes). But in bodies
191-
// we can use `cx.typeck_results.node_type(..)` to get the `ty::Ty` from a `hir::Ty`.
192-
// However the `node_type()` method can *only* be called in bodies.
193-
if let Some(&mut StackItem::Check { ref mut in_body, .. }) = self.stack.last_mut() {
194-
*in_body = in_body.saturating_add(1);
195-
}
196-
}
197-
198-
fn check_body_post(&mut self, _: &LateContext<'_>, _: &hir::Body<'_>) {
199-
if let Some(&mut StackItem::Check { ref mut in_body, .. }) = self.stack.last_mut() {
200-
*in_body = in_body.saturating_sub(1);
201-
}
202-
}
203186

204187
fn check_ty(&mut self, cx: &LateContext<'tcx>, hir_ty: &Ty<'tcx, AmbigArg>) {
205188
if !hir_ty.span.from_expansion()
206189
&& self.msrv.meets(msrvs::TYPE_ALIAS_ENUM_VARIANTS)
207190
&& let Some(&StackItem::Check {
208191
impl_id,
209-
in_body,
210192
ref types_to_skip,
211193
}) = self.stack.last()
212194
&& let TyKind::Path(QPath::Resolved(_, path)) = hir_ty.kind
@@ -215,12 +197,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
215197
Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } | Res::Def(DefKind::TyParam, _)
216198
)
217199
&& !types_to_skip.contains(&hir_ty.hir_id)
218-
&& let ty = if in_body > 0 {
219-
cx.typeck_results().node_type(hir_ty.hir_id)
220-
} else {
221-
// We don't care about ignoring infer vars here
222-
lower_ty(cx.tcx, hir_ty.as_unambig_ty())
223-
}
200+
&& let ty = ty_from_hir_ty(cx, hir_ty)
224201
&& let impl_ty = cx.tcx.type_of(impl_id).instantiate_identity()
225202
&& same_type_and_consts(ty, impl_ty)
226203
// Ensure the type we encounter and the one from the impl have the same lifetime parameters. It may be that

0 commit comments

Comments
 (0)