Skip to content

Commit de43d8c

Browse files
committed
Remove hir::AssocItemKind.
1 parent 519ad87 commit de43d8c

9 files changed

+85
-111
lines changed

clippy_lints/src/arbitrary_source_item_ordering.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ 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, ImplItemRef, IsAuto, Item, ItemKind, Mod, OwnerId, QPath, TraitItemRef, 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;
1617

@@ -315,9 +316,9 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
315316
}
316317

317318
if let Some(cur_t) = cur_t {
318-
let cur_t_kind = convert_assoc_item_kind(cur_t.kind);
319+
let cur_t_kind = convert_assoc_item_kind(cx, cur_t.id.owner_id);
319320
let cur_t_kind_index = self.assoc_types_order.index_of(&cur_t_kind);
320-
let item_kind = convert_assoc_item_kind(item.kind);
321+
let item_kind = convert_assoc_item_kind(cx,item.id.owner_id);
321322
let item_kind_index = self.assoc_types_order.index_of(&item_kind);
322323

323324
if cur_t_kind == item_kind && cur_t.ident.name.as_str() > item.ident.name.as_str() {
@@ -338,9 +339,9 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
338339
}
339340

340341
if let Some(cur_t) = cur_t {
341-
let cur_t_kind = convert_assoc_item_kind(cur_t.kind);
342+
let cur_t_kind = convert_assoc_item_kind(cx, cur_t.id.owner_id);
342343
let cur_t_kind_index = self.assoc_types_order.index_of(&cur_t_kind);
343-
let item_kind = convert_assoc_item_kind(item.kind);
344+
let item_kind = convert_assoc_item_kind(cx, item.id.owner_id);
344345
let item_kind_index = self.assoc_types_order.index_of(&item_kind);
345346

346347
if cur_t_kind == item_kind && cur_t.ident.name.as_str() > item.ident.name.as_str() {
@@ -458,18 +459,19 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
458459
}
459460
}
460461

461-
/// Converts a [`rustc_hir::AssocItemKind`] to a
462-
/// [`SourceItemOrderingTraitAssocItemKind`].
462+
/// Converts a [`ty::AssocKind`] to a [`SourceItemOrderingTraitAssocItemKind`].
463463
///
464464
/// This is implemented here because `rustc_hir` is not a dependency of
465465
/// `clippy_config`.
466-
fn convert_assoc_item_kind(value: AssocItemKind) -> SourceItemOrderingTraitAssocItemKind {
466+
fn convert_assoc_item_kind(cx: &LateContext<'_>, owner_id: OwnerId) -> SourceItemOrderingTraitAssocItemKind {
467+
let kind = cx.tcx.associated_item(owner_id.def_id).kind;
468+
467469
#[allow(clippy::enum_glob_use)] // Very local glob use for legibility.
468470
use SourceItemOrderingTraitAssocItemKind::*;
469-
match value {
470-
AssocItemKind::Const => Const,
471-
AssocItemKind::Type => Type,
472-
AssocItemKind::Fn { .. } => Fn,
471+
match kind {
472+
AssocKind::Const{..} => Const,
473+
AssocKind::Type {..}=> Type,
474+
AssocKind::Fn { .. } => Fn,
473475
}
474476
}
475477

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/functions/renamed_function_params.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
22
use rustc_errors::{Applicability, MultiSpan};
33
use rustc_hir::def_id::{DefId, DefIdSet};
44
use rustc_hir::hir_id::OwnerId;
5-
use rustc_hir::{Impl, ImplItem, ImplItemKind, ImplItemRef, ItemKind, Node, TraitRef};
5+
use rustc_hir::{Impl, ImplItem, ImplItemKind, ItemKind, Node, TraitRef};
66
use rustc_lint::LateContext;
77
use rustc_span::Span;
88
use rustc_span::symbol::{Ident, kw};
@@ -15,11 +15,10 @@ pub(super) fn check_impl_item(cx: &LateContext<'_>, item: &ImplItem<'_>, ignored
1515
&& let parent_node = cx.tcx.parent_hir_node(item.hir_id())
1616
&& let Node::Item(parent_item) = parent_node
1717
&& let ItemKind::Impl(Impl {
18-
items,
1918
of_trait: Some(trait_ref),
2019
..
2120
}) = &parent_item.kind
22-
&& let Some(did) = trait_item_def_id_of_impl(items, item.owner_id)
21+
&& let Some(did) = trait_item_def_id_of_impl(cx, item.owner_id)
2322
&& !is_from_ignored_trait(trait_ref, ignored_traits)
2423
{
2524
let mut param_idents_iter = cx.tcx.hir_body_param_idents(body_id);
@@ -93,14 +92,8 @@ impl RenamedFnArgs {
9392
}
9493

9594
/// Get the [`trait_item_def_id`](ImplItemRef::trait_item_def_id) of a relevant impl item.
96-
fn trait_item_def_id_of_impl(items: &[ImplItemRef], target: OwnerId) -> Option<DefId> {
97-
items.iter().find_map(|item| {
98-
if item.id.owner_id == target {
99-
item.trait_item_def_id
100-
} else {
101-
None
102-
}
103-
})
95+
fn trait_item_def_id_of_impl(cx: &LateContext<'_>, target: OwnerId) -> Option<DefId> {
96+
cx.tcx.associated_item(target).trait_item_def_id
10497
}
10598

10699
fn is_from_ignored_trait(of_trait: &TraitRef<'_>, ignored_traits: &DefIdSet) -> bool {

clippy_lints/src/infallible_try_from.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use clippy_utils::diagnostics::span_lint;
22
use clippy_utils::sym;
33
use rustc_errors::MultiSpan;
4-
use rustc_hir::{AssocItemKind, Item, ItemKind};
4+
use rustc_hir::{Item, ItemKind};
55
use rustc_lint::{LateContext, LateLintPass};
6+
use rustc_middle::ty::AssocTag;
67
use rustc_session::declare_lint_pass;
78

89
declare_clippy_lint! {
@@ -51,25 +52,23 @@ impl<'tcx> LateLintPass<'tcx> for InfallibleTryFrom {
5152
if !cx.tcx.is_diagnostic_item(sym::TryFrom, trait_def_id) {
5253
return;
5354
}
54-
for ii in imp.items {
55-
if ii.kind == AssocItemKind::Type {
56-
let ii = cx.tcx.hir_impl_item(ii.id);
57-
if ii.ident.name != sym::Error {
58-
continue;
59-
}
60-
let ii_ty = ii.expect_type();
61-
let ii_ty_span = ii_ty.span;
62-
let ii_ty = clippy_utils::ty::ty_from_hir_ty(cx, ii_ty);
63-
if !ii_ty.is_inhabited_from(cx.tcx, ii.owner_id.to_def_id(), cx.typing_env()) {
64-
let mut span = MultiSpan::from_span(cx.tcx.def_span(item.owner_id.to_def_id()));
65-
span.push_span_label(ii_ty_span, "infallible error type");
66-
span_lint(
67-
cx,
68-
INFALLIBLE_TRY_FROM,
69-
span,
70-
"infallible TryFrom impl; consider implementing From, instead",
71-
);
72-
}
55+
for ii in cx.tcx.associated_items(item.owner_id.def_id)
56+
.filter_by_name_unhygienic_and_kind(sym::Error, AssocTag::Type)
57+
{
58+
let ii_ty = cx.tcx.type_of(ii.def_id).instantiate_identity();
59+
if !ii_ty.is_inhabited_from(cx.tcx, ii.def_id, cx.typing_env()) {
60+
let mut span = MultiSpan::from_span(cx.tcx.def_span(item.owner_id.to_def_id()));
61+
let ii_ty_span = cx.tcx.hir_node_by_def_id(ii.def_id.expect_local())
62+
.expect_impl_item()
63+
.expect_type()
64+
.span;
65+
span.push_span_label(ii_ty_span, "infallible error type");
66+
span_lint(
67+
cx,
68+
INFALLIBLE_TRY_FROM,
69+
span,
70+
"infallible TryFrom impl; consider implementing From, instead",
71+
);
7372
}
7473
}
7574
}

clippy_lints/src/len_zero.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ use rustc_errors::Applicability;
1010
use rustc_hir::def::Res;
1111
use rustc_hir::def_id::{DefId, DefIdSet};
1212
use rustc_hir::{
13-
AssocItemKind, BinOpKind, Expr, ExprKind, FnRetTy, GenericArg, GenericBound, HirId, ImplItem, ImplItemKind,
13+
BinOpKind, Expr, ExprKind, FnRetTy, GenericArg, GenericBound, HirId, ImplItem, ImplItemKind,
1414
ImplicitSelfKind, Item, ItemKind, Mutability, Node, OpaqueTyOrigin, PatExprKind, PatKind, PathSegment, PrimTy,
1515
QPath, TraitItemRef, TyKind,
1616
};
1717
use rustc_lint::{LateContext, LateLintPass};
1818
use rustc_middle::ty::{self, FnSig, Ty};
1919
use rustc_session::declare_lint_pass;
2020
use rustc_span::source_map::Spanned;
21+
use rustc_span::symbol::kw;
2122
use rustc_span::{Ident, Span, Symbol};
2223
use rustc_trait_selection::traits::supertrait_def_ids;
2324

@@ -267,19 +268,10 @@ fn span_without_enclosing_paren(cx: &LateContext<'_>, span: Span) -> Span {
267268
fn check_trait_items(cx: &LateContext<'_>, visited_trait: &Item<'_>, ident: Ident, trait_items: &[TraitItemRef]) {
268269
fn is_named_self(cx: &LateContext<'_>, item: &TraitItemRef, name: Symbol) -> bool {
269270
item.ident.name == name
270-
&& if let AssocItemKind::Fn { has_self } = item.kind {
271-
has_self && {
272-
cx.tcx
273-
.fn_sig(item.id.owner_id)
274-
.skip_binder()
275-
.inputs()
276-
.skip_binder()
277-
.len()
278-
== 1
279-
}
280-
} else {
281-
false
282-
}
271+
&& matches!(
272+
cx.tcx.fn_arg_idents(item.id.owner_id),
273+
[Some(Ident { name: kw::SelfLower, .. })],
274+
)
283275
}
284276

285277
// fill the set with current and super traits

clippy_lints/src/missing_trait_methods.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,14 @@ impl<'tcx> LateLintPass<'tcx> for MissingTraitMethods {
6161
if !is_lint_allowed(cx, MISSING_TRAIT_METHODS, item.hir_id())
6262
&& span_is_local(item.span)
6363
&& let ItemKind::Impl(Impl {
64-
items,
6564
of_trait: Some(trait_ref),
6665
..
6766
}) = item.kind
6867
&& let Some(trait_id) = trait_ref.trait_def_id()
6968
{
70-
let trait_item_ids: DefIdSet = items
71-
.iter()
72-
.filter_map(|impl_item| impl_item.trait_item_def_id)
69+
let trait_item_ids: DefIdSet = cx.tcx.associated_items(item.owner_id)
70+
.in_definition_order()
71+
.filter_map(|assoc_item| assoc_item.trait_item_def_id)
7372
.collect();
7473

7574
for assoc in cx

clippy_lints/src/new_without_default.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_errors::Applicability;
66
use rustc_hir as hir;
77
use rustc_hir::HirIdSet;
88
use rustc_lint::{LateContext, LateLintPass, LintContext};
9+
use rustc_middle::ty::AssocKind;
910
use rustc_session::impl_lint_pass;
1011
use rustc_span::sym;
1112

@@ -61,18 +62,18 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
6162
of_trait: None,
6263
generics,
6364
self_ty: impl_self_ty,
64-
items,
6565
..
6666
}) = item.kind
6767
{
68-
for assoc_item in *items {
69-
if assoc_item.kind == (hir::AssocItemKind::Fn { has_self: false }) {
70-
let impl_item = cx.tcx.hir_impl_item(assoc_item.id);
68+
for assoc_item in cx.tcx.associated_items(item.owner_id.def_id)
69+
.filter_by_name_unhygienic(sym::new)
70+
{
71+
if let AssocKind::Fn { has_self: false, .. } = assoc_item.kind {
72+
let impl_item = cx.tcx.hir_node_by_def_id(assoc_item.def_id.expect_local()).expect_impl_item();
7173
if impl_item.span.in_external_macro(cx.sess().source_map()) {
7274
return;
7375
}
7476
if let hir::ImplItemKind::Fn(ref sig, _) = impl_item.kind {
75-
let name = impl_item.ident.name;
7677
let id = impl_item.owner_id;
7778
if sig.header.is_unsafe() {
7879
// can't be implemented for unsafe new
@@ -88,11 +89,9 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
8889
return;
8990
}
9091
if sig.decl.inputs.is_empty()
91-
&& name == sym::new
9292
&& cx.effective_visibilities.is_reachable(impl_item.owner_id.def_id)
93-
&& let self_def_id = cx.tcx.hir_get_parent_item(id.into())
94-
&& let self_ty = cx.tcx.type_of(self_def_id).instantiate_identity()
95-
&& self_ty == return_ty(cx, id)
93+
&& let self_ty = cx.tcx.type_of(item.owner_id).instantiate_identity()
94+
&& self_ty == return_ty(cx, impl_item.owner_id)
9695
&& let Some(default_trait_id) = cx.tcx.get_diagnostic_item(sym::Default)
9796
{
9897
if self.impling_types.is_none() {
@@ -111,7 +110,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
111110
// Check if a Default implementation exists for the Self type, regardless of
112111
// generics
113112
if let Some(ref impling_types) = self.impling_types
114-
&& let self_def = cx.tcx.type_of(self_def_id).instantiate_identity()
113+
&& let self_def = cx.tcx.type_of(item.owner_id).instantiate_identity()
115114
&& let Some(self_def) = self_def.ty_adt_def()
116115
&& let Some(self_local_did) = self_def.did().as_local()
117116
&& let self_id = cx.tcx.local_def_id_to_hir_id(self_local_did)

clippy_lints/src/same_name_method.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_data_structures::fx::FxHashMap;
33
use rustc_hir::def::{DefKind, Res};
44
use rustc_hir::{HirId, Impl, ItemKind, Node, Path, QPath, TraitRef, TyKind};
55
use rustc_lint::{LateContext, LateLintPass};
6-
use rustc_middle::ty::AssocItem;
6+
use rustc_middle::ty::{AssocKind, AssocItem};
77
use rustc_session::declare_lint_pass;
88
use rustc_span::Span;
99
use rustc_span::symbol::Symbol;
@@ -54,7 +54,6 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
5454
if matches!(cx.tcx.def_kind(id.owner_id), DefKind::Impl { .. })
5555
&& let item = cx.tcx.hir_item(id)
5656
&& let ItemKind::Impl(Impl {
57-
items,
5857
of_trait,
5958
self_ty,
6059
..
@@ -115,28 +114,23 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
115114
}
116115
};
117116

118-
for impl_item_ref in (*items)
119-
.iter()
120-
.filter(|impl_item_ref| matches!(impl_item_ref.kind, rustc_hir::AssocItemKind::Fn { .. }))
121-
{
122-
let method_name = impl_item_ref.ident.name;
123-
methods_in_trait.remove(&method_name);
124-
check_trait_method(method_name, impl_item_ref.span);
117+
for assoc_item in cx.tcx.associated_items(id.owner_id).in_definition_order() {
118+
if let AssocKind::Fn { name, .. } = assoc_item.kind {
119+
methods_in_trait.remove(&name);
120+
check_trait_method(name, cx.tcx.def_span(assoc_item.def_id));
121+
}
125122
}
126123

127124
for method_name in methods_in_trait {
128125
check_trait_method(method_name, item.span);
129126
}
130127
},
131128
None => {
132-
for impl_item_ref in (*items)
133-
.iter()
134-
.filter(|impl_item_ref| matches!(impl_item_ref.kind, rustc_hir::AssocItemKind::Fn { .. }))
135-
{
136-
let method_name = impl_item_ref.ident.name;
137-
let impl_span = impl_item_ref.span;
138-
let hir_id = impl_item_ref.id.hir_id();
139-
if let Some(trait_spans) = existing_name.trait_methods.get(&method_name) {
129+
for assoc_item in cx.tcx.associated_items(id.owner_id).in_definition_order() {
130+
let AssocKind::Fn { name, .. } = assoc_item.kind else { continue };
131+
let impl_span = cx.tcx.def_span(assoc_item.def_id);
132+
let hir_id = cx.tcx.local_def_id_to_hir_id(assoc_item.def_id.expect_local());
133+
if let Some(trait_spans) = existing_name.trait_methods.get(&name) {
140134
span_lint_hir_and_then(
141135
cx,
142136
SAME_NAME_METHOD,
@@ -148,12 +142,12 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
148142
// iterate on trait_spans?
149143
diag.span_note(
150144
trait_spans[0],
151-
format!("existing `{method_name}` defined here"),
145+
format!("existing `{name}` defined here"),
152146
);
153147
},
154148
);
155149
}
156-
existing_name.impl_methods.insert(method_name, (impl_span, hir_id));
150+
existing_name.impl_methods.insert(name, (impl_span, hir_id));
157151
}
158152
},
159153
}

0 commit comments

Comments
 (0)