Skip to content

Commit 434fb31

Browse files
committed
Auto merge of rust-lang#143357 - cjgillot:no-assoc-item-kind, r=compiler-errors
Retire hir::*ItemRef. This information was kept for various places that iterate on HIR to know about trait-items and impl-items. This PR replaces them by uses of the `associated_items` query that contain pretty much the same information. This shortens many spans to just `def_span`, which can be easier to read.
2 parents 41510cc + 8510965 commit 434fb31

29 files changed

+220
-256
lines changed

clippy_lints/src/arbitrary_source_item_ordering.rs

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +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-
AssocItemKind, Attribute, FieldDef, HirId, ImplItemRef, IsAuto, Item, ItemKind, Mod, QPath, TraitItemRef, TyKind,
11+
Attribute, FieldDef, HirId, IsAuto, ImplItemId, Item, ItemKind, Mod, OwnerId, QPath, TraitItemId, TyKind,
1212
Variant, VariantData,
1313
};
14+
use rustc_middle::ty::AssocKind;
1415
use rustc_lint::{LateContext, LateLintPass, LintContext};
1516
use rustc_session::impl_lint_pass;
17+
use rustc_span::Ident;
1618

1719
declare_clippy_lint! {
1820
/// ### What it does
@@ -194,22 +196,22 @@ impl ArbitrarySourceItemOrdering {
194196
}
195197

196198
/// Produces a linting warning for incorrectly ordered impl items.
197-
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) {
198200
span_lint_and_note(
199201
cx,
200202
ARBITRARY_SOURCE_ITEM_ORDERING,
201-
item.span,
203+
cx.tcx.def_span(item.owner_id),
202204
format!(
203205
"incorrect ordering of impl items (defined order: {:?})",
204206
self.assoc_types_order
205207
),
206-
Some(before_item.span),
207-
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)),
208210
);
209211
}
210212

211213
/// Produces a linting warning for incorrectly ordered item members.
212-
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) {
213215
span_lint_and_note(
214216
cx,
215217
ARBITRARY_SOURCE_ITEM_ORDERING,
@@ -220,7 +222,7 @@ impl ArbitrarySourceItemOrdering {
220222
);
221223
}
222224

223-
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) {
224226
let span = if let Some(ident) = item.kind.ident() {
225227
ident.span
226228
} else {
@@ -245,17 +247,17 @@ impl ArbitrarySourceItemOrdering {
245247
}
246248

247249
/// Produces a linting warning for incorrectly ordered trait items.
248-
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) {
249251
span_lint_and_note(
250252
cx,
251253
ARBITRARY_SOURCE_ITEM_ORDERING,
252-
item.span,
254+
cx.tcx.def_span(item.owner_id),
253255
format!(
254256
"incorrect ordering of trait items (defined order: {:?})",
255257
self.assoc_types_order
256258
),
257-
Some(before_item.span),
258-
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)),
259261
);
260262
}
261263
}
@@ -283,7 +285,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
283285
&& cur_v.ident.name.as_str() > variant.ident.name.as_str()
284286
&& cur_v.span != variant.span
285287
{
286-
Self::lint_member_name(cx, &variant.ident, &cur_v.ident);
288+
Self::lint_member_name(cx, variant.ident, cur_v.ident);
287289
}
288290
cur_v = Some(variant);
289291
}
@@ -299,57 +301,61 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
299301
&& cur_f.ident.name.as_str() > field.ident.name.as_str()
300302
&& cur_f.span != field.span
301303
{
302-
Self::lint_member_name(cx, &field.ident, &cur_f.ident);
304+
Self::lint_member_name(cx, field.ident, cur_f.ident);
303305
}
304306
cur_f = Some(field);
305307
}
306308
},
307309
ItemKind::Trait(is_auto, _safety, _ident, _generics, _generic_bounds, item_ref)
308310
if self.enable_ordering_for_trait && *is_auto == IsAuto::No =>
309311
{
310-
let mut cur_t: Option<&TraitItemRef> = None;
312+
let mut cur_t: Option<(TraitItemId, Ident)> = None;
311313

312-
for item in *item_ref {
313-
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()) {
314318
continue;
315319
}
316320

317-
if let Some(cur_t) = cur_t {
318-
let cur_t_kind = convert_assoc_item_kind(cur_t.kind);
321+
if let Some((cur_t, cur_ident)) = cur_t {
322+
let cur_t_kind = convert_assoc_item_kind(cx, cur_t.owner_id);
319323
let cur_t_kind_index = self.assoc_types_order.index_of(&cur_t_kind);
320-
let item_kind = convert_assoc_item_kind(item.kind);
324+
let item_kind = convert_assoc_item_kind(cx, item.owner_id);
321325
let item_kind_index = self.assoc_types_order.index_of(&item_kind);
322326

323-
if cur_t_kind == item_kind && cur_t.ident.name.as_str() > item.ident.name.as_str() {
324-
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);
325329
} else if cur_t_kind_index > item_kind_index {
326330
self.lint_trait_item(cx, item, cur_t);
327331
}
328332
}
329-
cur_t = Some(item);
333+
cur_t = Some((item, ident));
330334
}
331335
},
332336
ItemKind::Impl(trait_impl) if self.enable_ordering_for_impl => {
333-
let mut cur_t: Option<&ImplItemRef> = None;
337+
let mut cur_t: Option<(ImplItemId, Ident)> = None;
334338

335-
for item in trait_impl.items {
336-
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()) {
337343
continue;
338344
}
339345

340-
if let Some(cur_t) = cur_t {
341-
let cur_t_kind = convert_assoc_item_kind(cur_t.kind);
346+
if let Some((cur_t, cur_ident)) = cur_t {
347+
let cur_t_kind = convert_assoc_item_kind(cx, cur_t.owner_id);
342348
let cur_t_kind_index = self.assoc_types_order.index_of(&cur_t_kind);
343-
let item_kind = convert_assoc_item_kind(item.kind);
349+
let item_kind = convert_assoc_item_kind(cx, item.owner_id);
344350
let item_kind_index = self.assoc_types_order.index_of(&item_kind);
345351

346-
if cur_t_kind == item_kind && cur_t.ident.name.as_str() > item.ident.name.as_str() {
347-
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);
348354
} else if cur_t_kind_index > item_kind_index {
349355
self.lint_impl_item(cx, item, cur_t);
350356
}
351357
}
352-
cur_t = Some(item);
358+
cur_t = Some((item, ident));
353359
}
354360
},
355361
_ => {}, // Catch-all for `ItemKinds` that don't have fields.
@@ -458,18 +464,19 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
458464
}
459465
}
460466

461-
/// Converts a [`rustc_hir::AssocItemKind`] to a
462-
/// [`SourceItemOrderingTraitAssocItemKind`].
467+
/// Converts a [`ty::AssocKind`] to a [`SourceItemOrderingTraitAssocItemKind`].
463468
///
464469
/// This is implemented here because `rustc_hir` is not a dependency of
465470
/// `clippy_config`.
466-
fn convert_assoc_item_kind(value: AssocItemKind) -> SourceItemOrderingTraitAssocItemKind {
471+
fn convert_assoc_item_kind(cx: &LateContext<'_>, owner_id: OwnerId) -> SourceItemOrderingTraitAssocItemKind {
472+
let kind = cx.tcx.associated_item(owner_id.def_id).kind;
473+
467474
#[allow(clippy::enum_glob_use)] // Very local glob use for legibility.
468475
use SourceItemOrderingTraitAssocItemKind::*;
469-
match value {
470-
AssocItemKind::Const => Const,
471-
AssocItemKind::Type => Type,
472-
AssocItemKind::Fn { .. } => Fn,
476+
match kind {
477+
AssocKind::Const{..} => Const,
478+
AssocKind::Type {..}=> Type,
479+
AssocKind::Fn { .. } => Fn,
473480
}
474481
}
475482

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/escape.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use clippy_config::Conf;
22
use clippy_utils::diagnostics::span_lint_hir;
33
use rustc_abi::ExternAbi;
4-
use rustc_hir::{AssocItemKind, Body, FnDecl, HirId, HirIdSet, Impl, ItemKind, Node, Pat, PatKind, intravisit};
4+
use rustc_hir::{Body, FnDecl, HirId, HirIdSet, Node, Pat, PatKind, intravisit};
5+
use rustc_hir::def::DefKind;
56
use rustc_hir_typeck::expr_use_visitor::{Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
67
use rustc_lint::{LateContext, LateLintPass};
78
use rustc_middle::mir::FakeReadCause;
@@ -84,23 +85,18 @@ impl<'tcx> LateLintPass<'tcx> for BoxedLocal {
8485
.def_id;
8586

8687
let mut trait_self_ty = None;
87-
if let Node::Item(item) = cx.tcx.hir_node_by_def_id(parent_id) {
88+
match cx.tcx.def_kind(parent_id) {
8889
// If the method is an impl for a trait, don't warn.
89-
if let ItemKind::Impl(Impl { of_trait: Some(_), .. }) = item.kind {
90-
return;
90+
DefKind::Impl { of_trait: true } => {
91+
return
9192
}
9293

9394
// find `self` ty for this trait if relevant
94-
if let ItemKind::Trait(_, _, _, _, _, items) = item.kind {
95-
for trait_item in items {
96-
if trait_item.id.owner_id.def_id == fn_def_id
97-
// be sure we have `self` parameter in this function
98-
&& trait_item.kind == (AssocItemKind::Fn { has_self: true })
99-
{
100-
trait_self_ty = Some(TraitRef::identity(cx.tcx, trait_item.id.owner_id.to_def_id()).self_ty());
101-
}
102-
}
95+
DefKind::Trait => {
96+
trait_self_ty = Some(TraitRef::identity(cx.tcx, parent_id.to_def_id()).self_ty());
10397
}
98+
99+
_ => {}
104100
}
105101

106102
let mut v = EscapeDelegate {

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
};

0 commit comments

Comments
 (0)