Skip to content

Commit 8510965

Browse files
committed
Retire hir::*ItemRef.
1 parent de43d8c commit 8510965

22 files changed

+141
-151
lines changed

clippy_lints/src/arbitrary_source_item_ordering.rs

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ use clippy_utils::diagnostics::span_lint_and_note;
88
use clippy_utils::is_cfg_test;
99
use rustc_attr_data_structures::AttributeKind;
1010
use rustc_hir::{
11-
Attribute, FieldDef, HirId, ImplItemRef, IsAuto, Item, ItemKind, Mod, OwnerId, QPath, TraitItemRef, TyKind,
11+
Attribute, FieldDef, HirId, IsAuto, ImplItemId, Item, ItemKind, Mod, OwnerId, QPath, TraitItemId, TyKind,
1212
Variant, VariantData,
1313
};
1414
use rustc_middle::ty::AssocKind;
1515
use rustc_lint::{LateContext, LateLintPass, LintContext};
1616
use rustc_session::impl_lint_pass;
17+
use rustc_span::Ident;
1718

1819
declare_clippy_lint! {
1920
/// ### What it does
@@ -195,22 +196,22 @@ impl ArbitrarySourceItemOrdering {
195196
}
196197

197198
/// Produces a linting warning for incorrectly ordered impl items.
198-
fn lint_impl_item<T: LintContext>(&self, cx: &T, item: &ImplItemRef, before_item: &ImplItemRef) {
199+
fn lint_impl_item(&self, cx: &LateContext<'_>, item: ImplItemId, before_item: ImplItemId) {
199200
span_lint_and_note(
200201
cx,
201202
ARBITRARY_SOURCE_ITEM_ORDERING,
202-
item.span,
203+
cx.tcx.def_span(item.owner_id),
203204
format!(
204205
"incorrect ordering of impl items (defined order: {:?})",
205206
self.assoc_types_order
206207
),
207-
Some(before_item.span),
208-
format!("should be placed before `{}`", before_item.ident.name),
208+
Some(cx.tcx.def_span(before_item.owner_id)),
209+
format!("should be placed before `{}`", cx.tcx.item_name(before_item.owner_id)),
209210
);
210211
}
211212

212213
/// Produces a linting warning for incorrectly ordered item members.
213-
fn lint_member_name<T: LintContext>(cx: &T, ident: &rustc_span::Ident, before_ident: &rustc_span::Ident) {
214+
fn lint_member_name<T: LintContext>(cx: &T, ident: Ident, before_ident: Ident) {
214215
span_lint_and_note(
215216
cx,
216217
ARBITRARY_SOURCE_ITEM_ORDERING,
@@ -221,7 +222,7 @@ impl ArbitrarySourceItemOrdering {
221222
);
222223
}
223224

224-
fn lint_member_item<T: LintContext>(cx: &T, item: &Item<'_>, before_item: &Item<'_>, msg: &'static str) {
225+
fn lint_member_item(cx: &LateContext<'_>, item: &Item<'_>, before_item: &Item<'_>, msg: &'static str) {
225226
let span = if let Some(ident) = item.kind.ident() {
226227
ident.span
227228
} else {
@@ -246,17 +247,17 @@ impl ArbitrarySourceItemOrdering {
246247
}
247248

248249
/// Produces a linting warning for incorrectly ordered trait items.
249-
fn lint_trait_item<T: LintContext>(&self, cx: &T, item: &TraitItemRef, before_item: &TraitItemRef) {
250+
fn lint_trait_item(&self, cx: &LateContext<'_>, item: TraitItemId, before_item: TraitItemId) {
250251
span_lint_and_note(
251252
cx,
252253
ARBITRARY_SOURCE_ITEM_ORDERING,
253-
item.span,
254+
cx.tcx.def_span(item.owner_id),
254255
format!(
255256
"incorrect ordering of trait items (defined order: {:?})",
256257
self.assoc_types_order
257258
),
258-
Some(before_item.span),
259-
format!("should be placed before `{}`", before_item.ident.name),
259+
Some(cx.tcx.def_span(before_item.owner_id)),
260+
format!("should be placed before `{}`", cx.tcx.item_name(before_item.owner_id)),
260261
);
261262
}
262263
}
@@ -284,7 +285,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
284285
&& cur_v.ident.name.as_str() > variant.ident.name.as_str()
285286
&& cur_v.span != variant.span
286287
{
287-
Self::lint_member_name(cx, &variant.ident, &cur_v.ident);
288+
Self::lint_member_name(cx, variant.ident, cur_v.ident);
288289
}
289290
cur_v = Some(variant);
290291
}
@@ -300,57 +301,61 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
300301
&& cur_f.ident.name.as_str() > field.ident.name.as_str()
301302
&& cur_f.span != field.span
302303
{
303-
Self::lint_member_name(cx, &field.ident, &cur_f.ident);
304+
Self::lint_member_name(cx, field.ident, cur_f.ident);
304305
}
305306
cur_f = Some(field);
306307
}
307308
},
308309
ItemKind::Trait(is_auto, _safety, _ident, _generics, _generic_bounds, item_ref)
309310
if self.enable_ordering_for_trait && *is_auto == IsAuto::No =>
310311
{
311-
let mut cur_t: Option<&TraitItemRef> = None;
312+
let mut cur_t: Option<(TraitItemId, Ident)> = None;
312313

313-
for item in *item_ref {
314-
if item.span.in_external_macro(cx.sess().source_map()) {
314+
for &item in *item_ref {
315+
let span = cx.tcx.def_span(item.owner_id);
316+
let ident = cx.tcx.item_ident(item.owner_id);
317+
if span.in_external_macro(cx.sess().source_map()) {
315318
continue;
316319
}
317320

318-
if let Some(cur_t) = cur_t {
319-
let cur_t_kind = convert_assoc_item_kind(cx, cur_t.id.owner_id);
321+
if let Some((cur_t, cur_ident)) = cur_t {
322+
let cur_t_kind = convert_assoc_item_kind(cx, cur_t.owner_id);
320323
let cur_t_kind_index = self.assoc_types_order.index_of(&cur_t_kind);
321-
let item_kind = convert_assoc_item_kind(cx,item.id.owner_id);
324+
let item_kind = convert_assoc_item_kind(cx, item.owner_id);
322325
let item_kind_index = self.assoc_types_order.index_of(&item_kind);
323326

324-
if cur_t_kind == item_kind && cur_t.ident.name.as_str() > item.ident.name.as_str() {
325-
Self::lint_member_name(cx, &item.ident, &cur_t.ident);
327+
if cur_t_kind == item_kind && cur_ident.name.as_str() > ident.name.as_str() {
328+
Self::lint_member_name(cx, ident, cur_ident);
326329
} else if cur_t_kind_index > item_kind_index {
327330
self.lint_trait_item(cx, item, cur_t);
328331
}
329332
}
330-
cur_t = Some(item);
333+
cur_t = Some((item, ident));
331334
}
332335
},
333336
ItemKind::Impl(trait_impl) if self.enable_ordering_for_impl => {
334-
let mut cur_t: Option<&ImplItemRef> = None;
337+
let mut cur_t: Option<(ImplItemId, Ident)> = None;
335338

336-
for item in trait_impl.items {
337-
if item.span.in_external_macro(cx.sess().source_map()) {
339+
for &item in trait_impl.items {
340+
let span = cx.tcx.def_span(item.owner_id);
341+
let ident = cx.tcx.item_ident(item.owner_id);
342+
if span.in_external_macro(cx.sess().source_map()) {
338343
continue;
339344
}
340345

341-
if let Some(cur_t) = cur_t {
342-
let cur_t_kind = convert_assoc_item_kind(cx, cur_t.id.owner_id);
346+
if let Some((cur_t, cur_ident)) = cur_t {
347+
let cur_t_kind = convert_assoc_item_kind(cx, cur_t.owner_id);
343348
let cur_t_kind_index = self.assoc_types_order.index_of(&cur_t_kind);
344-
let item_kind = convert_assoc_item_kind(cx, item.id.owner_id);
349+
let item_kind = convert_assoc_item_kind(cx, item.owner_id);
345350
let item_kind_index = self.assoc_types_order.index_of(&item_kind);
346351

347-
if cur_t_kind == item_kind && cur_t.ident.name.as_str() > item.ident.name.as_str() {
348-
Self::lint_member_name(cx, &item.ident, &cur_t.ident);
352+
if cur_t_kind == item_kind && cur_ident.name.as_str() > ident.name.as_str() {
353+
Self::lint_member_name(cx, ident, cur_ident);
349354
} else if cur_t_kind_index > item_kind_index {
350355
self.lint_impl_item(cx, item, cur_t);
351356
}
352357
}
353-
cur_t = Some(item);
358+
cur_t = Some((item, ident));
354359
}
355360
},
356361
_ => {}, // Catch-all for `ItemKinds` that don't have fields.

clippy_lints/src/derivable_impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ impl<'tcx> LateLintPass<'tcx> for DerivableImpls {
192192
&& !item.span.from_expansion()
193193
&& let Some(def_id) = trait_ref.trait_def_id()
194194
&& cx.tcx.is_diagnostic_item(sym::Default, def_id)
195-
&& let impl_item_hir = child.id.hir_id()
195+
&& let impl_item_hir = child.hir_id()
196196
&& let Node::ImplItem(impl_item) = cx.tcx.hir_node(impl_item_hir)
197197
&& let ImplItemKind::Fn(_, b) = &impl_item.kind
198198
&& let Body { value: func_expr, .. } = cx.tcx.hir_body(*b)

clippy_lints/src/empty_drop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl LateLintPass<'_> for EmptyDrop {
4141
..
4242
}) = item.kind
4343
&& trait_ref.trait_def_id() == cx.tcx.lang_items().drop_trait()
44-
&& let impl_item_hir = child.id.hir_id()
44+
&& let impl_item_hir = child.hir_id()
4545
&& let Node::ImplItem(impl_item) = cx.tcx.hir_node(impl_item_hir)
4646
&& let ImplItemKind::Fn(_, b) = &impl_item.kind
4747
&& let Body { value: func_expr, .. } = cx.tcx.hir_body(*b)

clippy_lints/src/fallible_impl_from.rs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,20 @@ declare_lint_pass!(FallibleImplFrom => [FALLIBLE_IMPL_FROM]);
5252
impl<'tcx> LateLintPass<'tcx> for FallibleImplFrom {
5353
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
5454
// check for `impl From<???> for ..`
55-
if let hir::ItemKind::Impl(impl_) = &item.kind
55+
if let hir::ItemKind::Impl(_) = &item.kind
5656
&& let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(item.owner_id)
5757
&& cx
5858
.tcx
5959
.is_diagnostic_item(sym::From, impl_trait_ref.skip_binder().def_id)
6060
{
61-
lint_impl_body(cx, item.span, impl_.items);
61+
lint_impl_body(cx, item.owner_id, item.span);
6262
}
6363
}
6464
}
6565

66-
fn lint_impl_body(cx: &LateContext<'_>, impl_span: Span, impl_items: &[hir::ImplItemRef]) {
66+
fn lint_impl_body(cx: &LateContext<'_>, item_def_id: hir::OwnerId, impl_span: Span) {
6767
use rustc_hir::intravisit::{self, Visitor};
68-
use rustc_hir::{Expr, ImplItemKind};
68+
use rustc_hir::Expr;
6969

7070
struct FindPanicUnwrap<'a, 'tcx> {
7171
lcx: &'a LateContext<'tcx>,
@@ -96,35 +96,35 @@ fn lint_impl_body(cx: &LateContext<'_>, impl_span: Span, impl_items: &[hir::Impl
9696
}
9797
}
9898

99-
for impl_item in impl_items {
100-
if impl_item.ident.name == sym::from
101-
&& let ImplItemKind::Fn(_, body_id) = cx.tcx.hir_impl_item(impl_item.id).kind
102-
{
103-
// check the body for `begin_panic` or `unwrap`
104-
let body = cx.tcx.hir_body(body_id);
105-
let mut fpu = FindPanicUnwrap {
106-
lcx: cx,
107-
typeck_results: cx.tcx.typeck(impl_item.id.owner_id.def_id),
108-
result: Vec::new(),
109-
};
110-
fpu.visit_expr(body.value);
99+
for impl_item in cx.tcx.associated_items(item_def_id)
100+
.filter_by_name_unhygienic_and_kind(sym::from, ty::AssocTag::Fn)
101+
{
102+
let impl_item_def_id= impl_item.def_id.expect_local();
111103

112-
// if we've found one, lint
113-
if !fpu.result.is_empty() {
114-
span_lint_and_then(
115-
cx,
116-
FALLIBLE_IMPL_FROM,
117-
impl_span,
118-
"consider implementing `TryFrom` instead",
119-
move |diag| {
120-
diag.help(
121-
"`From` is intended for infallible conversions only. \
122-
Use `TryFrom` if there's a possibility for the conversion to fail",
123-
);
124-
diag.span_note(fpu.result, "potential failure(s)");
125-
},
126-
);
127-
}
104+
// check the body for `begin_panic` or `unwrap`
105+
let body = cx.tcx.hir_body_owned_by(impl_item_def_id);
106+
let mut fpu = FindPanicUnwrap {
107+
lcx: cx,
108+
typeck_results: cx.tcx.typeck(impl_item_def_id),
109+
result: Vec::new(),
110+
};
111+
fpu.visit_expr(body.value);
112+
113+
// if we've found one, lint
114+
if !fpu.result.is_empty() {
115+
span_lint_and_then(
116+
cx,
117+
FALLIBLE_IMPL_FROM,
118+
impl_span,
119+
"consider implementing `TryFrom` instead",
120+
move |diag| {
121+
diag.help(
122+
"`From` is intended for infallible conversions only. \
123+
Use `TryFrom` if there's a possibility for the conversion to fail",
124+
);
125+
diag.span_note(fpu.result, "potential failure(s)");
126+
},
127+
);
128128
}
129129
}
130130
}

clippy_lints/src/from_over_into.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use clippy_utils::source::SpanRangeExt;
99
use rustc_errors::Applicability;
1010
use rustc_hir::intravisit::{Visitor, walk_path};
1111
use rustc_hir::{
12-
FnRetTy, GenericArg, GenericArgs, HirId, Impl, ImplItemKind, ImplItemRef, Item, ItemKind, PatKind, Path,
12+
FnRetTy, GenericArg, GenericArgs, HirId, Impl, ImplItemKind, ImplItemId, Item, ItemKind, PatKind, Path,
1313
PathSegment, Ty, TyKind,
1414
};
1515
use rustc_lint::{LateContext, LateLintPass};
@@ -102,7 +102,7 @@ impl<'tcx> LateLintPass<'tcx> for FromOverInto {
102102
middle_trait_ref.self_ty()
103103
);
104104
if let Some(suggestions) =
105-
convert_to_from(cx, into_trait_seg, target_ty.as_unambig_ty(), self_ty, impl_item_ref)
105+
convert_to_from(cx, into_trait_seg, target_ty.as_unambig_ty(), self_ty, *impl_item_ref)
106106
{
107107
diag.multipart_suggestion(message, suggestions, Applicability::MachineApplicable);
108108
} else {
@@ -164,14 +164,14 @@ fn convert_to_from(
164164
into_trait_seg: &PathSegment<'_>,
165165
target_ty: &Ty<'_>,
166166
self_ty: &Ty<'_>,
167-
impl_item_ref: &ImplItemRef,
167+
impl_item_ref: ImplItemId,
168168
) -> Option<Vec<(Span, String)>> {
169169
if !target_ty.find_self_aliases().is_empty() {
170170
// It's tricky to expand self-aliases correctly, we'll ignore it to not cause a
171171
// bad suggestion/fix.
172172
return None;
173173
}
174-
let impl_item = cx.tcx.hir_impl_item(impl_item_ref.id);
174+
let impl_item = cx.tcx.hir_impl_item(impl_item_ref);
175175
let ImplItemKind::Fn(ref sig, body_id) = impl_item.kind else {
176176
return None;
177177
};

clippy_lints/src/implicit_hasher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitHasher {
130130
});
131131

132132
let mut ctr_vis = ImplicitHasherConstructorVisitor::new(cx, target);
133-
for item in impl_.items.iter().map(|item| cx.tcx.hir_impl_item(item.id)) {
133+
for item in impl_.items.iter().map(|&item| cx.tcx.hir_impl_item(item)) {
134134
ctr_vis.visit_impl_item(item);
135135
}
136136

clippy_lints/src/iter_without_into_iter.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,12 @@ impl LateLintPass<'_> for IterWithoutIntoIter {
139139
// We can't check inherent impls for slices, but we know that they have an `iter(_mut)` method
140140
ty.peel_refs().is_slice() || get_adt_inherent_method(cx, ty, expected_method_name).is_some()
141141
})
142-
&& let Some(iter_assoc_span) = imp.items.iter().find_map(|item| {
143-
if item.ident.name == sym::IntoIter {
144-
Some(cx.tcx.hir_impl_item(item.id).expect_type().span)
145-
} else {
146-
None
147-
}
148-
})
142+
&& let Some(iter_assoc_span) = cx.tcx.associated_items(item.owner_id)
143+
.filter_by_name_unhygienic_and_kind(sym::IntoIter, ty::AssocTag::Type)
144+
.next()
145+
.map(|assoc_item| {
146+
cx.tcx.hir_node_by_def_id(assoc_item.def_id.expect_local()).expect_impl_item().expect_type().span
147+
})
149148
&& is_ty_exported(cx, ty)
150149
{
151150
span_lint_and_then(

clippy_lints/src/len_zero.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_hir::def_id::{DefId, DefIdSet};
1212
use rustc_hir::{
1313
BinOpKind, Expr, ExprKind, FnRetTy, GenericArg, GenericBound, HirId, ImplItem, ImplItemKind,
1414
ImplicitSelfKind, Item, ItemKind, Mutability, Node, OpaqueTyOrigin, PatExprKind, PatKind, PathSegment, PrimTy,
15-
QPath, TraitItemRef, TyKind,
15+
QPath, TraitItemId, TyKind,
1616
};
1717
use rustc_lint::{LateContext, LateLintPass};
1818
use rustc_middle::ty::{self, FnSig, Ty};
@@ -265,11 +265,11 @@ fn span_without_enclosing_paren(cx: &LateContext<'_>, span: Span) -> Span {
265265
}
266266
}
267267

268-
fn check_trait_items(cx: &LateContext<'_>, visited_trait: &Item<'_>, ident: Ident, trait_items: &[TraitItemRef]) {
269-
fn is_named_self(cx: &LateContext<'_>, item: &TraitItemRef, name: Symbol) -> bool {
270-
item.ident.name == name
268+
fn check_trait_items(cx: &LateContext<'_>, visited_trait: &Item<'_>, ident: Ident, trait_items: &[TraitItemId]) {
269+
fn is_named_self(cx: &LateContext<'_>, item: &TraitItemId, name: Symbol) -> bool {
270+
cx.tcx.item_name(item.owner_id) == name
271271
&& matches!(
272-
cx.tcx.fn_arg_idents(item.id.owner_id),
272+
cx.tcx.fn_arg_idents(item.owner_id),
273273
[Some(Ident { name: kw::SelfLower, .. })],
274274
)
275275
}

clippy_lints/src/lifetimes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ fn report_extra_impl_lifetimes<'tcx>(cx: &LateContext<'tcx>, impl_: &'tcx Impl<'
716716
walk_trait_ref(&mut checker, trait_ref);
717717
}
718718
walk_unambig_ty(&mut checker, impl_.self_ty);
719-
for item in impl_.items {
719+
for &item in impl_.items {
720720
walk_impl_item_ref(&mut checker, item);
721721
}
722722

0 commit comments

Comments
 (0)