Skip to content

Commit d891f77

Browse files
committed
Move has_self field to hir::AssocKind::Fn.
`hir::AssocItem` currently has a boolean `fn_has_self_parameter` field, which is misplaced, because it's only relevant for associated fns, not for associated consts or types. This commit moves it (and renames it) to the `AssocKind::Fn` variant, where it belongs. This requires introducing a new C-style enum, `AssocTag`, which is like `AssocKind` but without the fields. This is because `AssocKind` values are passed to various functions like `find_by_ident_and_kind` to indicate what kind of associated item should be searched for, and having to specify `has_self` isn't relevant there. New methods: - Predicates `AssocItem::is_fn` and `AssocItem::is_method`. - `AssocItem::as_tag` which converts `AssocItem::kind` to `AssocTag`. Removed `find_by_name_and_kinds`, which is unused. `AssocItem::descr` can now distinguish between methods and associated functions, which slightly improves some error messages.
1 parent e6dc2e9 commit d891f77

9 files changed

+17
-19
lines changed

clippy_lints/src/bool_assert_comparison.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fn is_impl_not_trait_with_bool_out<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -
5656
cx.tcx.associated_items(trait_id).find_by_ident_and_kind(
5757
cx.tcx,
5858
Ident::from_str("Output"),
59-
ty::AssocKind::Type,
59+
ty::AssocTag::Type,
6060
trait_id,
6161
)
6262
})

clippy_lints/src/len_zero.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_hir::{
1313
QPath, TraitItemRef, TyKind,
1414
};
1515
use rustc_lint::{LateContext, LateLintPass};
16-
use rustc_middle::ty::{self, AssocKind, FnSig, Ty};
16+
use rustc_middle::ty::{self, FnSig, Ty};
1717
use rustc_session::declare_lint_pass;
1818
use rustc_span::source_map::Spanned;
1919
use rustc_span::symbol::sym;
@@ -288,8 +288,7 @@ fn check_trait_items(cx: &LateContext<'_>, visited_trait: &Item<'_>, ident: Iden
288288
.items()
289289
.flat_map(|&i| cx.tcx.associated_items(i).filter_by_name_unhygienic(is_empty))
290290
.any(|i| {
291-
i.kind == AssocKind::Fn
292-
&& i.fn_has_self_parameter
291+
i.is_method()
293292
&& cx.tcx.fn_sig(i.def_id).skip_binder().inputs().skip_binder().len() == 1
294293
});
295294

@@ -466,7 +465,7 @@ fn check_for_is_empty(
466465
.inherent_impls(impl_ty)
467466
.iter()
468467
.flat_map(|&id| cx.tcx.associated_items(id).filter_by_name_unhygienic(is_empty))
469-
.find(|item| item.kind == AssocKind::Fn);
468+
.find(|item| item.is_fn());
470469

471470
let (msg, is_empty_span, self_kind) = match is_empty {
472471
None => (
@@ -486,7 +485,7 @@ fn check_for_is_empty(
486485
None,
487486
),
488487
Some(is_empty)
489-
if !(is_empty.fn_has_self_parameter
488+
if !(is_empty.is_method()
490489
&& check_is_empty_sig(
491490
cx,
492491
cx.tcx.fn_sig(is_empty.def_id).instantiate_identity().skip_binder(),
@@ -608,7 +607,7 @@ fn is_empty_array(expr: &Expr<'_>) -> bool {
608607
fn has_is_empty(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
609608
/// Gets an `AssocItem` and return true if it matches `is_empty(self)`.
610609
fn is_is_empty(cx: &LateContext<'_>, item: &ty::AssocItem) -> bool {
611-
if item.kind == AssocKind::Fn {
610+
if item.is_fn() {
612611
let sig = cx.tcx.fn_sig(item.def_id).skip_binder();
613612
let ty = sig.skip_binder();
614613
ty.inputs().len() == 1

clippy_lints/src/methods/needless_collect.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_hir::{
1717
};
1818
use rustc_lint::LateContext;
1919
use rustc_middle::hir::nested_filter;
20-
use rustc_middle::ty::{self, AssocKind, ClauseKind, EarlyBinder, GenericArg, GenericArgKind, Ty};
20+
use rustc_middle::ty::{self, AssocTag, ClauseKind, EarlyBinder, GenericArg, GenericArgKind, Ty};
2121
use rustc_span::symbol::Ident;
2222
use rustc_span::{Span, sym};
2323

@@ -241,7 +241,7 @@ fn is_contains_sig(cx: &LateContext<'_>, call_id: HirId, iter_expr: &Expr<'_>) -
241241
&& let Some(iter_item) = cx.tcx.associated_items(iter_trait).find_by_ident_and_kind(
242242
cx.tcx,
243243
Ident::with_dummy_span(sym::Item),
244-
AssocKind::Type,
244+
AssocTag::Type,
245245
iter_trait,
246246
)
247247
&& let args = cx.tcx.mk_args(&[GenericArg::from(typeck.expr_ty_adjusted(iter_expr))])

clippy_lints/src/methods/or_fun_call.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub(super) fn check<'tcx>(
7878
.iter()
7979
.flat_map(|impl_id| cx.tcx.associated_items(impl_id).filter_by_name_unhygienic(sugg))
8080
.find_map(|assoc| {
81-
if assoc.fn_has_self_parameter
81+
if assoc.is_method()
8282
&& cx.tcx.fn_sig(assoc.def_id).skip_binder().inputs().skip_binder().len() == 1
8383
{
8484
Some(assoc.def_id)

clippy_lints/src/needless_borrows_for_generic_args.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ fn has_ref_mut_self_method(cx: &LateContext<'_>, trait_def_id: DefId) -> bool {
299299
.associated_items(trait_def_id)
300300
.in_definition_order()
301301
.any(|assoc_item| {
302-
if assoc_item.fn_has_self_parameter {
302+
if assoc_item.is_method() {
303303
let self_ty = cx
304304
.tcx
305305
.fn_sig(assoc_item.def_id)

clippy_lints/src/same_name_method.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ 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::AssocKind;
76
use rustc_session::declare_lint_pass;
87
use rustc_span::Span;
98
use rustc_span::symbol::Symbol;
@@ -85,7 +84,7 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
8584
cx.tcx
8685
.associated_items(did)
8786
.in_definition_order()
88-
.filter(|assoc_item| matches!(assoc_item.kind, AssocKind::Fn))
87+
.filter(|assoc_item| assoc_item.is_fn())
8988
.map(|assoc_item| assoc_item.name)
9089
.collect()
9190
} else {

clippy_lints/src/unconditional_recursion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_hir::{Body, Expr, ExprKind, FnDecl, HirId, Item, ItemKind, Node, QPath
1010
use rustc_hir_analysis::lower_ty;
1111
use rustc_lint::{LateContext, LateLintPass};
1212
use rustc_middle::hir::nested_filter;
13-
use rustc_middle::ty::{self, AssocKind, Ty, TyCtxt};
13+
use rustc_middle::ty::{self, Ty, TyCtxt};
1414
use rustc_session::impl_lint_pass;
1515
use rustc_span::symbol::{Ident, kw};
1616
use rustc_span::{Span, sym};
@@ -322,7 +322,7 @@ impl UnconditionalRecursion {
322322
.in_definition_order()
323323
// We're not interested in foreign implementations of the `Default` trait.
324324
.find(|item| {
325-
item.kind == AssocKind::Fn && item.def_id.is_local() && item.name == kw::Default
325+
item.is_fn() && item.def_id.is_local() && item.name == kw::Default
326326
})
327327
&& let Some(body_node) = cx.tcx.hir_get_if_local(assoc_item.def_id)
328328
&& let Some(body_id) = body_node.body_id()

clippy_lints/src/unused_self.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedSelf {
7474
.is_some()
7575
};
7676
if let ItemKind::Impl(Impl { of_trait: None, .. }) = parent_item.kind
77-
&& assoc_item.fn_has_self_parameter
77+
&& assoc_item.is_method()
7878
&& let ImplItemKind::Fn(.., body_id) = &impl_item.kind
7979
&& (!cx.effective_visibilities.is_exported(impl_item.owner_id.def_id) || !self.avoid_breaking_exported_api)
8080
&& let body = cx.tcx.hir_body(*body_id)

clippy_utils/src/ty/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_middle::mir::interpret::Scalar;
1919
use rustc_middle::traits::EvaluationResult;
2020
use rustc_middle::ty::layout::ValidityRequirement;
2121
use rustc_middle::ty::{
22-
self, AdtDef, AliasTy, AssocItem, AssocKind, Binder, BoundRegion, FnSig, GenericArg, GenericArgKind,
22+
self, AdtDef, AliasTy, AssocItem, AssocTag, Binder, BoundRegion, FnSig, GenericArg, GenericArgKind,
2323
GenericArgsRef, GenericParamDefKind, IntTy, ParamEnv, Region, RegionKind, TraitRef, Ty, TyCtxt, TypeSuperVisitable,
2424
TypeVisitable, TypeVisitableExt, TypeVisitor, UintTy, Upcast, VariantDef, VariantDiscr,
2525
};
@@ -1112,7 +1112,7 @@ pub fn make_projection<'tcx>(
11121112
let Some(assoc_item) = tcx.associated_items(container_id).find_by_ident_and_kind(
11131113
tcx,
11141114
Ident::with_dummy_span(assoc_ty),
1115-
AssocKind::Type,
1115+
AssocTag::Type,
11161116
container_id,
11171117
) else {
11181118
debug_assert!(false, "type `{assoc_ty}` not found in `{container_id:?}`");
@@ -1345,7 +1345,7 @@ pub fn get_adt_inherent_method<'a>(cx: &'a LateContext<'_>, ty: Ty<'_>, method_n
13451345
.associated_items(did)
13461346
.filter_by_name_unhygienic(method_name)
13471347
.next()
1348-
.filter(|item| item.kind == AssocKind::Fn)
1348+
.filter(|item| item.as_tag() == AssocTag::Fn)
13491349
})
13501350
} else {
13511351
None

0 commit comments

Comments
 (0)