Skip to content

Commit 6e801e2

Browse files
committed
Use type_of for impl self type
1 parent 4913d82 commit 6e801e2

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

clippy_lints/src/use_self.rs

+13-25
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir::{
99
def::{CtorOf, DefKind, Res},
1010
def_id::LocalDefId,
1111
intravisit::{walk_ty, NestedVisitorMap, Visitor},
12-
Expr, ExprKind, FnRetTy, FnSig, GenericArg, HirId, Impl, ImplItemKind, Item, ItemKind, Node, Path, QPath, TyKind,
12+
Expr, ExprKind, FnRetTy, FnSig, GenericArg, HirId, Impl, ImplItemKind, Item, ItemKind, Path, QPath, TyKind,
1313
};
1414
use rustc_lint::{LateContext, LateLintPass, LintContext};
1515
use rustc_middle::hir::map::Map;
@@ -74,8 +74,7 @@ impl UseSelf {
7474
#[derive(Debug)]
7575
enum StackItem {
7676
Check {
77-
hir_id: HirId,
78-
impl_trait_ref_def_id: Option<LocalDefId>,
77+
impl_id: LocalDefId,
7978
types_to_skip: FxHashSet<HirId>,
8079
types_to_lint: Vec<HirId>,
8180
},
@@ -87,7 +86,7 @@ impl_lint_pass!(UseSelf => [USE_SELF]);
8786
const SEGMENTS_MSG: &str = "segments should be composed of at least 1 element";
8887

8988
impl<'tcx> LateLintPass<'tcx> for UseSelf {
90-
fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
89+
fn check_item(&mut self, _cx: &LateContext<'_>, item: &Item<'_>) {
9190
if !is_item_interesting(item) {
9291
// This does two things:
9392
// 1) Reduce needless churn on `self.stack`
@@ -100,17 +99,15 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
10099
// avoid linting on nested items, we push `StackItem::NoCheck` on the stack to signal, that
101100
// we're in an `impl` or nested item, that we don't want to lint
102101
let stack_item = if_chain! {
103-
if let ItemKind::Impl(Impl { self_ty, ref of_trait, .. }) = item.kind;
102+
if let ItemKind::Impl(Impl { self_ty, .. }) = item.kind;
104103
if let TyKind::Path(QPath::Resolved(_, item_path)) = self_ty.kind;
105104
let parameters = &item_path.segments.last().expect(SEGMENTS_MSG).args;
106105
if parameters.as_ref().map_or(true, |params| {
107106
!params.parenthesized && !params.args.iter().any(|arg| matches!(arg, GenericArg::Lifetime(_)))
108107
});
109108
then {
110-
let impl_trait_ref_def_id = of_trait.as_ref().map(|_| cx.tcx.hir().local_def_id(item.hir_id()));
111109
StackItem::Check {
112-
hir_id: self_ty.hir_id,
113-
impl_trait_ref_def_id,
110+
impl_id: item.def_id,
114111
types_to_lint: Vec::new(),
115112
types_to_skip: std::iter::once(self_ty.hir_id).collect(),
116113
}
@@ -133,11 +130,11 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
133130
if_chain! {
134131
if let ImplItemKind::Fn(FnSig { decl, .. }, ..) = impl_item.kind;
135132
if let Some(&mut StackItem::Check {
136-
impl_trait_ref_def_id: Some(def_id),
133+
impl_id,
137134
ref mut types_to_skip,
138135
..
139136
}) = self.stack.last_mut();
140-
if let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(def_id);
137+
if let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(impl_id);
141138
then {
142139
// `self_ty` is the semantic self type of `impl <trait> for <type>`. This cannot be
143140
// `Self`.
@@ -195,13 +192,13 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
195192
// could only allow this lint on item scope. And we would have to check if those types are
196193
// already dealt with in `check_ty` anyway.
197194
if let Some(StackItem::Check {
198-
hir_id,
195+
impl_id,
199196
types_to_lint,
200197
types_to_skip,
201198
..
202199
}) = self.stack.last_mut()
203200
{
204-
let self_ty = ty_from_hir_id(cx, *hir_id);
201+
let self_ty = cx.tcx.type_of(*impl_id);
205202

206203
let mut visitor = LintTyCollector {
207204
cx,
@@ -220,15 +217,14 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
220217
if !in_macro(hir_ty.span);
221218
if meets_msrv(self.msrv.as_ref(), &msrvs::TYPE_ALIAS_ENUM_VARIANTS);
222219
if let Some(StackItem::Check {
223-
hir_id,
220+
impl_id,
224221
types_to_lint,
225222
types_to_skip,
226-
..
227223
}) = self.stack.last();
228224
if !types_to_skip.contains(&hir_ty.hir_id);
229225
if types_to_lint.contains(&hir_ty.hir_id)
230226
|| {
231-
let self_ty = ty_from_hir_id(cx, *hir_id);
227+
let self_ty = cx.tcx.type_of(*impl_id);
232228
should_lint_ty(hir_ty, hir_ty_to_ty(cx.tcx, hir_ty), self_ty)
233229
};
234230
let hir = cx.tcx.hir();
@@ -244,8 +240,8 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
244240
if_chain! {
245241
if !in_macro(expr.span);
246242
if meets_msrv(self.msrv.as_ref(), &msrvs::TYPE_ALIAS_ENUM_VARIANTS);
247-
if let Some(StackItem::Check { hir_id, .. }) = self.stack.last();
248-
if cx.typeck_results().expr_ty(expr) == ty_from_hir_id(cx, *hir_id);
243+
if let Some(&StackItem::Check { impl_id, .. }) = self.stack.last();
244+
if cx.typeck_results().expr_ty(expr) == cx.tcx.type_of(impl_id);
249245
then {} else { return; }
250246
}
251247
match expr.kind {
@@ -351,14 +347,6 @@ fn is_item_interesting(item: &Item<'_>) -> bool {
351347
)
352348
}
353349

354-
fn ty_from_hir_id<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Ty<'tcx> {
355-
if let Some(Node::Ty(hir_ty)) = cx.tcx.hir().find(hir_id) {
356-
hir_ty_to_ty(cx.tcx, hir_ty)
357-
} else {
358-
unreachable!("This function should only be called with `HirId`s that are for sure `Node::Ty`")
359-
}
360-
}
361-
362350
fn should_lint_ty(hir_ty: &hir::Ty<'_>, ty: Ty<'_>, self_ty: Ty<'_>) -> bool {
363351
if_chain! {
364352
if same_type_and_consts(ty, self_ty);

0 commit comments

Comments
 (0)