Auto merge of #144377 - camsteffen:simplify-impl-of-method, r=fee1-dead

Rename impl_of_method and trait_of_item

This PR used to tweak the implementation of impl_of_method, but that introduced a perf regression.

Rename impl_of_method and trait_of_item to impl_of_assoc and trait_of_assoc respectively. This reflects how the two functions are closely related. And it reflects the behavior more accurately as the functions check whether the input is an associated item.
This commit is contained in:
bors 2025-07-28 16:38:15 +00:00
commit e3514bde96
68 changed files with 107 additions and 111 deletions

View file

@ -2384,7 +2384,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
if let Some(body_expr) = finder.body_expr
&& let Some(loop_span) = finder.loop_span
&& let Some(def_id) = typeck_results.type_dependent_def_id(body_expr.hir_id)
&& let Some(trait_did) = tcx.trait_of_item(def_id)
&& let Some(trait_did) = tcx.trait_of_assoc(def_id)
&& tcx.is_diagnostic_item(sym::Iterator, trait_did)
{
if let Some(loop_bind) = finder.loop_bind {

View file

@ -938,7 +938,7 @@ impl<'tcx> BorrowedContentSource<'tcx> {
fn from_call(func: Ty<'tcx>, tcx: TyCtxt<'tcx>) -> Option<Self> {
match *func.kind() {
ty::FnDef(def_id, args) => {
let trait_id = tcx.trait_of_item(def_id)?;
let trait_id = tcx.trait_of_assoc(def_id)?;
if tcx.is_lang_item(trait_id, LangItem::Deref)
|| tcx.is_lang_item(trait_id, LangItem::DerefMut)

View file

@ -682,7 +682,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
}
let my_def = self.body.source.def_id();
let Some(td) =
self.infcx.tcx.impl_of_method(my_def).and_then(|x| self.infcx.tcx.trait_id_of_impl(x))
self.infcx.tcx.impl_of_assoc(my_def).and_then(|x| self.infcx.tcx.trait_id_of_impl(x))
else {
return (false, false, None);
};
@ -880,7 +880,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
let opt_suggestions = tcx
.typeck(path_segment.hir_id.owner.def_id)
.type_dependent_def_id(expr.hir_id)
.and_then(|def_id| tcx.impl_of_method(def_id))
.and_then(|def_id| tcx.impl_of_assoc(def_id))
.map(|def_id| tcx.associated_items(def_id))
.map(|assoc_items| {
assoc_items
@ -1056,7 +1056,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
.tcx
.typeck(path_segment.hir_id.owner.def_id)
.type_dependent_def_id(cur_expr.hir_id)
.and_then(|def_id| self.infcx.tcx.impl_of_method(def_id))
.and_then(|def_id| self.infcx.tcx.impl_of_assoc(def_id))
.map(|def_id| self.infcx.tcx.associated_items(def_id))
.map(|assoc_items| {
assoc_items.filter_by_name_unhygienic(sym::iter_mut).peekable()

View file

@ -1761,7 +1761,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
);
assert!(!matches!(
tcx.impl_of_method(def_id).map(|imp| tcx.def_kind(imp)),
tcx.impl_of_assoc(def_id).map(|imp| tcx.def_kind(imp)),
Some(DefKind::Impl { of_trait: true })
));
self.prove_predicates(

View file

@ -533,7 +533,7 @@ impl<'ll, 'tcx> DebugInfoCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
// First, let's see if this is a method within an inherent impl. Because
// if yes, we want to make the result subroutine DIE a child of the
// subroutine's self-type.
if let Some(impl_def_id) = cx.tcx.impl_of_method(instance.def_id()) {
if let Some(impl_def_id) = cx.tcx.impl_of_assoc(instance.def_id()) {
// If the method does *not* belong to a trait, proceed
if cx.tcx.trait_id_of_impl(impl_def_id).is_none() {
let impl_self_ty = cx.tcx.instantiate_and_normalize_erasing_regions(

View file

@ -86,7 +86,7 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> DefIdMap<S
// Only consider nodes that actually have exported symbols.
match tcx.def_kind(def_id) {
DefKind::Fn | DefKind::Static { .. } => {}
DefKind::AssocFn if tcx.impl_of_method(def_id.to_def_id()).is_some() => {}
DefKind::AssocFn if tcx.impl_of_assoc(def_id.to_def_id()).is_some() => {}
_ => return None,
};

View file

@ -784,7 +784,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
self.revalidate_conditional_constness(callee, fn_args, *fn_span);
// Attempting to call a trait method?
if let Some(trait_did) = tcx.trait_of_item(callee) {
if let Some(trait_did) = tcx.trait_of_assoc(callee) {
// We can't determine the actual callee here, so we have to do different checks
// than usual.

View file

@ -369,7 +369,7 @@ where
assert!(promoted.is_none() || Q::ALLOW_PROMOTED);
// Don't peek inside trait associated constants.
if promoted.is_none() && cx.tcx.trait_of_item(def).is_none() {
if promoted.is_none() && cx.tcx.trait_of_assoc(def).is_none() {
let qualifs = cx.tcx.at(constant.span).mir_const_qualif(def);
if !Q::in_qualifs(&qualifs) {

View file

@ -725,7 +725,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
) {
let tcx = *self.tcx;
let trait_def_id = tcx.trait_of_item(def_id).unwrap();
let trait_def_id = tcx.trait_of_assoc(def_id).unwrap();
let virtual_trait_ref = ty::TraitRef::from_method(tcx, trait_def_id, virtual_instance.args);
let existential_trait_ref = ty::ExistentialTraitRef::erase_self_ty(tcx, virtual_trait_ref);
let concrete_trait_ref = existential_trait_ref.with_self_ty(tcx, dyn_ty);

View file

@ -295,6 +295,10 @@ impl DefKind {
}
}
pub fn is_assoc(self) -> bool {
matches!(self, DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy)
}
/// This is a "module" in name resolution sense.
#[inline]
pub fn is_module_like(self) -> bool {

View file

@ -759,7 +759,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
&self,
err: &mut Diag<'_, impl EmissionGuarantee>,
) {
let trait_ = match self.tcx.trait_of_item(self.def_id) {
let trait_ = match self.tcx.trait_of_assoc(self.def_id) {
Some(def_id) => def_id,
None => return,
};

View file

@ -755,7 +755,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
let limit = if candidates.len() == 5 { 5 } else { 4 };
for (index, &item) in candidates.iter().take(limit).enumerate() {
let impl_ = tcx.impl_of_method(item).unwrap();
let impl_ = tcx.impl_of_assoc(item).unwrap();
let note_span = if item.is_local() {
Some(tcx.def_span(item))

View file

@ -694,7 +694,7 @@ impl<'tcx> Visitor<'tcx> for AnnotateUnitFallbackVisitor<'_, 'tcx> {
// i.e. changing `Default::default()` to `<() as Default>::default()`.
if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
&& let Res::Def(DefKind::AssocFn, def_id) = path.res
&& self.fcx.tcx.trait_of_item(def_id).is_some()
&& self.fcx.tcx.trait_of_assoc(def_id).is_some()
&& let Some(args) = self.fcx.typeck_results.borrow().node_args_opt(expr.hir_id)
&& let self_ty = args.type_at(0)
&& let Some(vid) = self.fcx.root_vid(self_ty)

View file

@ -96,7 +96,7 @@ impl<'tcx> LateLintPass<'tcx> for MapUnitFn {
fn is_impl_slice(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
&& let Some(impl_id) = cx.tcx.impl_of_method(method_id)
&& let Some(impl_id) = cx.tcx.impl_of_assoc(method_id)
{
return cx.tcx.type_of(impl_id).skip_binder().is_slice();
}

View file

@ -84,7 +84,7 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
return;
};
let Some(trait_id) = cx.tcx.trait_of_item(did) else { return };
let Some(trait_id) = cx.tcx.trait_of_assoc(did) else { return };
let Some(trait_) = cx.tcx.get_diagnostic_name(trait_id) else { return };

View file

@ -24,7 +24,7 @@ impl<'tcx> LateLintPass<'tcx> for PassByValue {
fn check_ty(&mut self, cx: &LateContext<'_>, ty: &'tcx hir::Ty<'tcx, AmbigArg>) {
match &ty.kind {
TyKind::Ref(_, hir::MutTy { ty: inner_ty, mutbl: hir::Mutability::Not }) => {
if let Some(impl_did) = cx.tcx.impl_of_method(ty.hir_id.owner.to_def_id()) {
if let Some(impl_did) = cx.tcx.impl_of_assoc(ty.hir_id.owner.to_def_id()) {
if cx.tcx.impl_trait_ref(impl_did).is_some() {
return;
}

View file

@ -1904,7 +1904,7 @@ impl InvalidAtomicOrdering {
if let ExprKind::MethodCall(method_path, _, args, _) = &expr.kind
&& recognized_names.contains(&method_path.ident.name)
&& let Some(m_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
&& let Some(impl_did) = cx.tcx.impl_of_method(m_def_id)
&& let Some(impl_did) = cx.tcx.impl_of_assoc(m_def_id)
&& let Some(adt) = cx.tcx.type_of(impl_did).instantiate_identity().ty_adt_def()
// skip extension traits, only lint functions from the standard library
&& cx.tcx.trait_id_of_impl(impl_did).is_none()

View file

@ -182,7 +182,7 @@ impl EffectiveVisibilities {
// don't check this condition for them.
let is_impl = matches!(tcx.def_kind(def_id), DefKind::Impl { .. });
let is_associated_item_in_trait_impl = tcx
.impl_of_method(def_id.to_def_id())
.impl_of_assoc(def_id.to_def_id())
.and_then(|impl_id| tcx.trait_id_of_impl(impl_id))
.is_some();
if !is_impl && !is_associated_item_in_trait_impl {

View file

@ -1988,29 +1988,21 @@ impl<'tcx> TyCtxt<'tcx> {
self.impl_trait_ref(def_id).map(|tr| tr.skip_binder().def_id)
}
/// If the given `DefId` describes an item belonging to a trait,
/// returns the `DefId` of the trait that the trait item belongs to;
/// otherwise, returns `None`.
pub fn trait_of_item(self, def_id: DefId) -> Option<DefId> {
if let DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy = self.def_kind(def_id) {
let parent = self.parent(def_id);
if let DefKind::Trait | DefKind::TraitAlias = self.def_kind(parent) {
return Some(parent);
}
}
None
/// If the given `DefId` is an associated item, returns the `DefId` of the parent trait or impl.
pub fn assoc_parent(self, def_id: DefId) -> Option<DefId> {
self.def_kind(def_id).is_assoc().then(|| self.parent(def_id))
}
/// If the given `DefId` describes a method belonging to an impl, returns the
/// `DefId` of the impl that the method belongs to; otherwise, returns `None`.
pub fn impl_of_method(self, def_id: DefId) -> Option<DefId> {
if let DefKind::AssocConst | DefKind::AssocFn | DefKind::AssocTy = self.def_kind(def_id) {
let parent = self.parent(def_id);
if let DefKind::Impl { .. } = self.def_kind(parent) {
return Some(parent);
}
}
None
/// If the given `DefId` is an associated item of a trait,
/// returns the `DefId` of the trait; otherwise, returns `None`.
pub fn trait_of_assoc(self, def_id: DefId) -> Option<DefId> {
self.assoc_parent(def_id).filter(|id| self.def_kind(id) == DefKind::Trait)
}
/// If the given `DefId` is an associated item of an impl,
/// returns the `DefId` of the impl; otherwise returns `None`.
pub fn impl_of_assoc(self, def_id: DefId) -> Option<DefId> {
self.assoc_parent(def_id).filter(|id| matches!(self.def_kind(id), DefKind::Impl { .. }))
}
pub fn is_exportable(self, def_id: DefId) -> bool {
@ -2181,7 +2173,7 @@ impl<'tcx> TyCtxt<'tcx> {
#[inline]
pub fn is_const_default_method(self, def_id: DefId) -> bool {
matches!(self.trait_of_item(def_id), Some(trait_id) if self.is_const_trait(trait_id))
matches!(self.trait_of_assoc(def_id), Some(trait_id) if self.is_const_trait(trait_id))
}
pub fn impl_method_has_trait_impl_trait_tys(self, def_id: DefId) -> bool {

View file

@ -21,7 +21,7 @@ impl<'tcx> MirLint<'tcx> for CheckCallRecursion {
if let DefKind::Fn | DefKind::AssocFn = tcx.def_kind(def_id) {
// If this is trait/impl method, extract the trait's args.
let trait_args = match tcx.trait_of_item(def_id.to_def_id()) {
let trait_args = match tcx.trait_of_assoc(def_id.to_def_id()) {
Some(trait_def_id) => {
let trait_args_count = tcx.generics_of(trait_def_id).count();
&GenericArgs::identity_for_item(tcx, def_id)[..trait_args_count]
@ -44,7 +44,7 @@ impl<'tcx> MirLint<'tcx> for CheckDropRecursion {
// First check if `body` is an `fn drop()` of `Drop`
if let DefKind::AssocFn = tcx.def_kind(def_id)
&& let Some(trait_ref) =
tcx.impl_of_method(def_id.to_def_id()).and_then(|def_id| tcx.impl_trait_ref(def_id))
tcx.impl_of_assoc(def_id.to_def_id()).and_then(|def_id| tcx.impl_trait_ref(def_id))
&& tcx.is_lang_item(trait_ref.instantiate_identity().def_id, LangItem::Drop)
// avoid erroneous `Drop` impls from causing ICEs below
&& let sig = tcx.fn_sig(def_id).instantiate_identity()

View file

@ -40,7 +40,7 @@ impl<'tcx> Visitor<'tcx> for PackedRefChecker<'_, 'tcx> {
if context.is_borrow() && util::is_disaligned(self.tcx, self.body, self.typing_env, *place)
{
let def_id = self.body.source.instance.def_id();
if let Some(impl_def_id) = self.tcx.impl_of_method(def_id)
if let Some(impl_def_id) = self.tcx.impl_of_assoc(def_id)
&& self.tcx.is_builtin_derived(impl_def_id)
{
// If we ever reach here it means that the generated derive

View file

@ -35,7 +35,7 @@ fn is_eligible_for_coverage(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
// Don't instrument functions with `#[automatically_derived]` on their
// enclosing impl block, on the assumption that most users won't care about
// coverage for derived impls.
if let Some(impl_of) = tcx.impl_of_method(def_id.to_def_id())
if let Some(impl_of) = tcx.impl_of_assoc(def_id.to_def_id())
&& tcx.is_automatically_derived(impl_of)
{
trace!("InstrumentCoverage skipped for {def_id:?} (automatically derived)");

View file

@ -75,7 +75,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceKind<'tcx>) -> Body<
build_call_shim(tcx, instance, Some(adjustment), CallKind::Direct(def_id))
}
ty::InstanceKind::FnPtrShim(def_id, ty) => {
let trait_ = tcx.trait_of_item(def_id).unwrap();
let trait_ = tcx.trait_of_assoc(def_id).unwrap();
// Supports `Fn` or `async Fn` traits.
let adjustment = match tcx
.fn_trait_kind_from_def_id(trait_)

View file

@ -650,13 +650,13 @@ fn characteristic_def_id_of_mono_item<'tcx>(
// its self-type. If the self-type does not provide a characteristic
// DefId, we use the location of the impl after all.
if tcx.trait_of_item(def_id).is_some() {
if tcx.trait_of_assoc(def_id).is_some() {
let self_ty = instance.args.type_at(0);
// This is a default implementation of a trait method.
return characteristic_def_id_of_type(self_ty).or(Some(def_id));
}
if let Some(impl_def_id) = tcx.impl_of_method(def_id) {
if let Some(impl_def_id) = tcx.impl_of_assoc(def_id) {
if tcx.sess.opts.incremental.is_some()
&& tcx
.trait_id_of_impl(impl_def_id)

View file

@ -368,7 +368,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
/// will be ignored for the purposes of dead code analysis (see PR #85200
/// for discussion).
fn should_ignore_item(&mut self, def_id: DefId) -> bool {
if let Some(impl_of) = self.tcx.impl_of_method(def_id) {
if let Some(impl_of) = self.tcx.impl_of_assoc(def_id) {
if !self.tcx.is_automatically_derived(impl_of) {
return false;
}
@ -429,7 +429,7 @@ impl<'tcx> MarkSymbolVisitor<'tcx> {
Node::TraitItem(trait_item) => {
// mark the trait live
let trait_item_id = trait_item.owner_id.to_def_id();
if let Some(trait_id) = self.tcx.trait_of_item(trait_item_id) {
if let Some(trait_id) = self.tcx.trait_of_assoc(trait_item_id) {
self.check_def_id(trait_id);
}
intravisit::walk_trait_item(self, trait_item);

View file

@ -339,7 +339,7 @@ pub(crate) fn transform_instance<'tcx>(
} else if let ty::InstanceKind::Virtual(def_id, _) = instance.def {
// Transform self into a trait object of the trait that defines the method for virtual
// functions to match the type erasure done below.
let upcast_ty = match tcx.trait_of_item(def_id) {
let upcast_ty = match tcx.trait_of_assoc(def_id) {
Some(trait_id) => trait_object_ty(
tcx,
ty::Binder::dummy(ty::TraitRef::from_method(tcx, trait_id, instance.args)),
@ -364,7 +364,7 @@ pub(crate) fn transform_instance<'tcx>(
};
instance.args = tcx.mk_args_trait(self_ty, instance.args.into_iter().skip(1));
} else if let ty::InstanceKind::VTableShim(def_id) = instance.def
&& let Some(trait_id) = tcx.trait_of_item(def_id)
&& let Some(trait_id) = tcx.trait_of_assoc(def_id)
{
// Adjust the type ids of VTableShims to the type id expected in the call sites for the
// entry in the vtable (i.e., by using the signature of the closure passed as an argument
@ -466,7 +466,7 @@ fn implemented_method<'tcx>(
let method_id;
let trait_id;
let trait_method;
let ancestor = if let Some(impl_id) = tcx.impl_of_method(instance.def_id()) {
let ancestor = if let Some(impl_id) = tcx.impl_of_assoc(instance.def_id()) {
// Implementation in an `impl` block
trait_ref = tcx.impl_trait_ref(impl_id)?;
let impl_method = tcx.associated_item(instance.def_id());
@ -480,7 +480,7 @@ fn implemented_method<'tcx>(
// Provided method in a `trait` block
trait_method = trait_method_bound;
method_id = instance.def_id();
trait_id = tcx.trait_of_item(method_id)?;
trait_id = tcx.trait_of_assoc(method_id)?;
trait_ref = ty::EarlyBinder::bind(TraitRef::from_method(tcx, trait_id, instance.args));
trait_id
} else {

View file

@ -1303,7 +1303,7 @@ impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
&& let Some(args) = self.node_args_opt(expr.hir_id)
&& args.iter().any(|arg| self.generic_arg_contains_target(arg))
&& let Some(def_id) = self.typeck_results.type_dependent_def_id(expr.hir_id)
&& self.tecx.tcx.trait_of_item(def_id).is_some()
&& self.tecx.tcx.trait_of_assoc(def_id).is_some()
&& !has_impl_trait(def_id)
// FIXME(fn_delegation): In delegation item argument spans are equal to last path
// segment. This leads to ICE's when emitting `multipart_suggestion`.

View file

@ -360,7 +360,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
},
] = path.segments
&& data.trait_ref.def_id == *trait_id
&& self.tcx.trait_of_item(*item_id) == Some(*trait_id)
&& self.tcx.trait_of_assoc(*item_id) == Some(*trait_id)
&& let None = self.tainted_by_errors()
{
let assoc_item = self.tcx.associated_item(item_id);

View file

@ -987,7 +987,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
else {
return false;
};
if self.tcx.trait_of_item(did) != Some(clone_trait) {
if self.tcx.trait_of_assoc(did) != Some(clone_trait) {
return false;
}
Some(ident.span)

View file

@ -759,7 +759,7 @@ fn instantiate_and_check_impossible_predicates<'tcx>(
// Specifically check trait fulfillment to avoid an error when trying to resolve
// associated items.
if let Some(trait_def_id) = tcx.trait_of_item(key.0) {
if let Some(trait_def_id) = tcx.trait_of_assoc(key.0) {
let trait_ref = ty::TraitRef::from_method(tcx, trait_def_id, key.1);
predicates.push(trait_ref.upcast(tcx));
}

View file

@ -1507,7 +1507,7 @@ fn confirm_builtin_candidate<'cx, 'tcx>(
let tcx = selcx.tcx();
let self_ty = obligation.predicate.self_ty();
let item_def_id = obligation.predicate.def_id;
let trait_def_id = tcx.trait_of_item(item_def_id).unwrap();
let trait_def_id = tcx.trait_of_assoc(item_def_id).unwrap();
let args = tcx.mk_args(&[self_ty.into()]);
let (term, obligations) = if tcx.is_lang_item(trait_def_id, LangItem::DiscriminantKind) {
let discriminant_def_id =

View file

@ -21,7 +21,7 @@ fn resolve_instance_raw<'tcx>(
) -> Result<Option<Instance<'tcx>>, ErrorGuaranteed> {
let PseudoCanonicalInput { typing_env, value: (def_id, args) } = key;
let result = if let Some(trait_def_id) = tcx.trait_of_item(def_id) {
let result = if let Some(trait_def_id) = tcx.trait_of_assoc(def_id) {
debug!(" => associated item, attempting to find impl in typing_env {:#?}", typing_env);
resolve_associated_item(
tcx,

View file

@ -98,7 +98,7 @@ impl<'tcx> LateLintPass<'tcx> for AssigningClones {
// That is overly conservative - the lint should fire even if there was no initializer,
// but the variable has been initialized before `lhs` was evaluated.
&& path_to_local(lhs).is_none_or(|lhs| local_is_initialized(cx, lhs))
&& let Some(resolved_impl) = cx.tcx.impl_of_method(resolved_fn.def_id())
&& let Some(resolved_impl) = cx.tcx.impl_of_assoc(resolved_fn.def_id())
// Derived forms don't implement `clone_from`/`clone_into`.
// See https://github.com/rust-lang/rust/pull/98445#issuecomment-1190681305
&& !cx.tcx.is_builtin_derived(resolved_impl)

View file

@ -63,7 +63,7 @@ fn is_used_as_unaligned(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
ExprKind::MethodCall(name, self_arg, ..) if self_arg.hir_id == e.hir_id => {
if matches!(name.ident.name, sym::read_unaligned | sym::write_unaligned)
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(parent.hir_id)
&& let Some(def_id) = cx.tcx.impl_of_method(def_id)
&& let Some(def_id) = cx.tcx.impl_of_assoc(def_id)
&& cx.tcx.type_of(def_id).instantiate_identity().is_raw_ptr()
{
true

View file

@ -37,7 +37,7 @@ fn get_const_name_and_ty_name(
} else {
return None;
}
} else if let Some(impl_id) = cx.tcx.impl_of_method(method_def_id)
} else if let Some(impl_id) = cx.tcx.impl_of_assoc(method_def_id)
&& let Some(ty_name) = get_primitive_ty_name(cx.tcx.type_of(impl_id).instantiate_identity())
&& matches!(
method_name,

View file

@ -364,7 +364,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing<'tcx> {
// * `&self` methods on `&T` can have auto-borrow, but `&self` methods on `T` will take
// priority.
if let Some(fn_id) = typeck.type_dependent_def_id(hir_id)
&& let Some(trait_id) = cx.tcx.trait_of_item(fn_id)
&& let Some(trait_id) = cx.tcx.trait_of_assoc(fn_id)
&& let arg_ty = cx.tcx.erase_regions(adjusted_ty)
&& let ty::Ref(_, sub_ty, _) = *arg_ty.kind()
&& let args =

View file

@ -339,7 +339,7 @@ fn check_with_condition<'tcx>(
ExprKind::Path(QPath::TypeRelative(_, name)) => {
if name.ident.name == sym::MIN
&& let Some(const_id) = cx.typeck_results().type_dependent_def_id(cond_num_val.hir_id)
&& let Some(impl_id) = cx.tcx.impl_of_method(const_id)
&& let Some(impl_id) = cx.tcx.impl_of_assoc(const_id)
&& let None = cx.tcx.impl_trait_ref(impl_id) // An inherent impl
&& cx.tcx.type_of(impl_id).instantiate_identity().is_integral()
{
@ -350,7 +350,7 @@ fn check_with_condition<'tcx>(
if let ExprKind::Path(QPath::TypeRelative(_, name)) = func.kind
&& name.ident.name == sym::min_value
&& let Some(func_id) = cx.typeck_results().type_dependent_def_id(func.hir_id)
&& let Some(impl_id) = cx.tcx.impl_of_method(func_id)
&& let Some(impl_id) = cx.tcx.impl_of_assoc(func_id)
&& let None = cx.tcx.impl_trait_ref(impl_id) // An inherent impl
&& cx.tcx.type_of(impl_id).instantiate_identity().is_integral()
{

View file

@ -317,7 +317,7 @@ impl<'tcx> Visitor<'tcx> for VarVisitor<'_, 'tcx> {
.cx
.typeck_results()
.type_dependent_def_id(expr.hir_id)
.and_then(|def_id| self.cx.tcx.trait_of_item(def_id))
.and_then(|def_id| self.cx.tcx.trait_of_assoc(def_id))
&& ((meth.ident.name == sym::index && self.cx.tcx.lang_items().index_trait() == Some(trait_id))
|| (meth.ident.name == sym::index_mut && self.cx.tcx.lang_items().index_mut_trait() == Some(trait_id)))
&& !self.check(args_1, args_0, expr)

View file

@ -14,7 +14,7 @@ pub(super) fn check<'tcx>(
bytes_recv: &'tcx hir::Expr<'_>,
) {
if let Some(bytes_id) = cx.typeck_results().type_dependent_def_id(count_recv.hir_id)
&& let Some(impl_id) = cx.tcx.impl_of_method(bytes_id)
&& let Some(impl_id) = cx.tcx.impl_of_assoc(bytes_id)
&& cx.tcx.type_of(impl_id).instantiate_identity().is_str()
&& let ty = cx.typeck_results().expr_ty(bytes_recv).peel_refs()
&& (ty.is_str() || is_type_lang_item(cx, ty, hir::LangItem::String))

View file

@ -30,7 +30,7 @@ pub(super) fn check<'tcx>(
}
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
&& let Some(impl_id) = cx.tcx.impl_of_method(method_id)
&& let Some(impl_id) = cx.tcx.impl_of_assoc(method_id)
&& cx.tcx.type_of(impl_id).instantiate_identity().is_str()
&& let ExprKind::Lit(Spanned {
node: LitKind::Str(ext_literal, ..),

View file

@ -28,7 +28,7 @@ pub(super) fn check(
if cx
.typeck_results()
.type_dependent_def_id(expr.hir_id)
.and_then(|id| cx.tcx.trait_of_item(id))
.and_then(|id| cx.tcx.trait_of_assoc(id))
.zip(cx.tcx.lang_items().clone_trait())
.is_none_or(|(x, y)| x != y)
{

View file

@ -18,7 +18,7 @@ pub(super) fn check<'tcx>(
arg: &'tcx hir::Expr<'_>,
) {
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
&& let Some(impl_id) = cx.tcx.impl_of_method(method_id)
&& let Some(impl_id) = cx.tcx.impl_of_assoc(method_id)
&& let identity = cx.tcx.type_of(impl_id).instantiate_identity()
&& let hir::ExprKind::Lit(Spanned {
node: LitKind::Int(Pu128(0), _),

View file

@ -50,7 +50,7 @@ pub fn is_clone_like(cx: &LateContext<'_>, method_name: Symbol, method_def_id: h
sym::to_path_buf => is_diag_item_method(cx, method_def_id, sym::Path),
sym::to_vec => cx
.tcx
.impl_of_method(method_def_id)
.impl_of_assoc(method_def_id)
.filter(|&impl_did| {
cx.tcx.type_of(impl_did).instantiate_identity().is_slice() && cx.tcx.impl_trait_ref(impl_did).is_none()
})

View file

@ -44,9 +44,9 @@ pub(super) fn check<'tcx>(
let typeck = cx.typeck_results();
if let Some(iter_id) = cx.tcx.get_diagnostic_item(sym::Iterator)
&& let Some(method_id) = typeck.type_dependent_def_id(expr.hir_id)
&& cx.tcx.trait_of_item(method_id) == Some(iter_id)
&& cx.tcx.trait_of_assoc(method_id) == Some(iter_id)
&& let Some(method_id) = typeck.type_dependent_def_id(cloned_call.hir_id)
&& cx.tcx.trait_of_item(method_id) == Some(iter_id)
&& cx.tcx.trait_of_assoc(method_id) == Some(iter_id)
&& let cloned_recv_ty = typeck.expr_ty_adjusted(cloned_recv)
&& let Some(iter_assoc_ty) = cx.get_associated_type(cloned_recv_ty, iter_id, sym::Item)
&& matches!(*iter_assoc_ty.kind(), ty::Ref(_, ty, _) if !is_copy(cx, ty))

View file

@ -18,7 +18,7 @@ pub(super) fn check<'tcx>(
map_expr: &'tcx Expr<'_>,
) {
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
&& let Some(impl_id) = cx.tcx.impl_of_method(method_id)
&& let Some(impl_id) = cx.tcx.impl_of_assoc(method_id)
&& is_type_diagnostic_item(cx, cx.tcx.type_of(impl_id).instantiate_identity(), sym::Option)
&& let ExprKind::Call(err_path, [err_arg]) = or_expr.kind
&& is_res_lang_ctor(cx, path_res(cx, err_path), ResultErr)

View file

@ -59,7 +59,7 @@ pub(super) fn check(
&& is_type_lang_item(cx, cx.typeck_results().expr_ty(collect_expr), LangItem::String)
&& let Some(take_id) = cx.typeck_results().type_dependent_def_id(take_expr.hir_id)
&& let Some(iter_trait_id) = cx.tcx.get_diagnostic_item(sym::Iterator)
&& cx.tcx.trait_of_item(take_id) == Some(iter_trait_id)
&& cx.tcx.trait_of_assoc(take_id) == Some(iter_trait_id)
&& let Some(repeat_kind) = parse_repeat_arg(cx, repeat_arg)
&& let ctxt = collect_expr.span.ctxt()
&& ctxt == take_expr.span.ctxt()

View file

@ -23,7 +23,7 @@ fn should_run_lint(cx: &LateContext<'_>, e: &hir::Expr<'_>, method_id: DefId) ->
return true;
}
// We check if it's an `Option` or a `Result`.
if let Some(id) = cx.tcx.impl_of_method(method_id) {
if let Some(id) = cx.tcx.impl_of_assoc(method_id) {
let identity = cx.tcx.type_of(id).instantiate_identity();
if !is_type_diagnostic_item(cx, identity, sym::Option) && !is_type_diagnostic_item(cx, identity, sym::Result) {
return false;
@ -69,7 +69,7 @@ pub(super) fn check(cx: &LateContext<'_>, e: &hir::Expr<'_>, recv: &hir::Expr<'_
hir::ExprKind::MethodCall(method, obj, [], _) => {
if ident_eq(name, obj) && method.ident.name == sym::clone
&& let Some(fn_id) = cx.typeck_results().type_dependent_def_id(closure_expr.hir_id)
&& let Some(trait_id) = cx.tcx.trait_of_item(fn_id)
&& let Some(trait_id) = cx.tcx.trait_of_assoc(fn_id)
&& cx.tcx.lang_items().clone_trait() == Some(trait_id)
// no autoderefs
&& !cx.typeck_results().expr_adjustments(obj).iter()

View file

@ -8,7 +8,7 @@ use super::MAP_ERR_IGNORE;
pub(super) fn check(cx: &LateContext<'_>, e: &Expr<'_>, arg: &Expr<'_>) {
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
&& let Some(impl_id) = cx.tcx.impl_of_method(method_id)
&& let Some(impl_id) = cx.tcx.impl_of_assoc(method_id)
&& is_type_diagnostic_item(cx, cx.tcx.type_of(impl_id).instantiate_identity(), sym::Result)
&& let ExprKind::Closure(&Closure {
capture_clause: CaptureBy::Ref,

View file

@ -13,7 +13,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, ex: &'tcx Expr<'tcx>, recv: &'
&& let (_, ref_depth, Mutability::Mut) = peel_mid_ty_refs_is_mutable(cx.typeck_results().expr_ty(recv))
&& ref_depth >= 1
&& let Some(method_id) = cx.typeck_results().type_dependent_def_id(ex.hir_id)
&& let Some(impl_id) = cx.tcx.impl_of_method(method_id)
&& let Some(impl_id) = cx.tcx.impl_of_assoc(method_id)
&& is_type_diagnostic_item(cx, cx.tcx.type_of(impl_id).instantiate_identity(), sym::Mutex)
{
span_lint_and_sugg(

View file

@ -18,7 +18,7 @@ fn is_open_options(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) {
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
&& let Some(impl_id) = cx.tcx.impl_of_method(method_id)
&& let Some(impl_id) = cx.tcx.impl_of_assoc(method_id)
&& is_open_options(cx, cx.tcx.type_of(impl_id).instantiate_identity())
{
let mut options = Vec::new();
@ -111,7 +111,7 @@ fn get_open_options(
// This might be a user defined extension trait with a method like `truncate_write`
// which would be a false positive
if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(argument.hir_id)
&& cx.tcx.trait_of_item(method_def_id).is_some()
&& cx.tcx.trait_of_assoc(method_def_id).is_some()
{
return false;
}

View file

@ -11,7 +11,7 @@ use super::PATH_BUF_PUSH_OVERWRITE;
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, arg: &'tcx Expr<'_>) {
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
&& let Some(impl_id) = cx.tcx.impl_of_method(method_id)
&& let Some(impl_id) = cx.tcx.impl_of_assoc(method_id)
&& is_type_diagnostic_item(cx, cx.tcx.type_of(impl_id).instantiate_identity(), sym::PathBuf)
&& let ExprKind::Lit(lit) = arg.kind
&& let LitKind::Str(ref path_lit, _) = lit.node

View file

@ -9,7 +9,7 @@ use super::STABLE_SORT_PRIMITIVE;
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) {
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
&& let Some(impl_id) = cx.tcx.impl_of_method(method_id)
&& let Some(impl_id) = cx.tcx.impl_of_assoc(method_id)
&& cx.tcx.type_of(impl_id).instantiate_identity().is_slice()
&& let Some(slice_type) = is_slice_of_primitives(cx, recv)
{

View file

@ -286,7 +286,7 @@ fn parse_iter_usage<'tcx>(
let iter_id = cx.tcx.get_diagnostic_item(sym::Iterator)?;
match (name.ident.name, args) {
(sym::next, []) if cx.tcx.trait_of_item(did) == Some(iter_id) => (IterUsageKind::Nth(0), e.span),
(sym::next, []) if cx.tcx.trait_of_assoc(did) == Some(iter_id) => (IterUsageKind::Nth(0), e.span),
(sym::next_tuple, []) => {
return if paths::ITERTOOLS_NEXT_TUPLE.matches(cx, did)
&& let ty::Adt(adt_def, subs) = cx.typeck_results().expr_ty(e).kind()
@ -303,7 +303,7 @@ fn parse_iter_usage<'tcx>(
None
};
},
(sym::nth | sym::skip, [idx_expr]) if cx.tcx.trait_of_item(did) == Some(iter_id) => {
(sym::nth | sym::skip, [idx_expr]) if cx.tcx.trait_of_assoc(did) == Some(iter_id) => {
if let Some(Constant::Int(idx)) = ConstEvalCtxt::new(cx).eval(idx_expr) {
let span = if name.ident.as_str() == "nth" {
e.span
@ -312,7 +312,7 @@ fn parse_iter_usage<'tcx>(
&& next_name.ident.name == sym::next
&& next_expr.span.ctxt() == ctxt
&& let Some(next_id) = cx.typeck_results().type_dependent_def_id(next_expr.hir_id)
&& cx.tcx.trait_of_item(next_id) == Some(iter_id)
&& cx.tcx.trait_of_assoc(next_id) == Some(iter_id)
{
next_expr.span
} else {

View file

@ -10,7 +10,7 @@ use super::SUSPICIOUS_SPLITN;
pub(super) fn check(cx: &LateContext<'_>, method_name: Symbol, expr: &Expr<'_>, self_arg: &Expr<'_>, count: u128) {
if count <= 1
&& let Some(call_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
&& let Some(impl_id) = cx.tcx.impl_of_method(call_id)
&& let Some(impl_id) = cx.tcx.impl_of_assoc(call_id)
&& cx.tcx.impl_trait_ref(impl_id).is_none()
&& let self_ty = cx.tcx.type_of(impl_id).instantiate_identity()
&& (self_ty.is_slice() || self_ty.is_str())

View file

@ -165,7 +165,7 @@ pub(super) fn check_method(cx: &LateContext<'_>, expr: &Expr<'_>) {
pub(super) fn check_function(cx: &LateContext<'_>, expr: &Expr<'_>, callee: &Expr<'_>) {
if let ExprKind::Path(ref qpath) = callee.kind
&& let Some(item_def_id) = cx.qpath_res(qpath, callee.hir_id).opt_def_id()
&& let Some(trait_def_id) = cx.tcx.trait_of_item(item_def_id)
&& let Some(trait_def_id) = cx.tcx.trait_of_assoc(item_def_id)
{
let qpath_spans = match qpath {
QPath::Resolved(_, path) => {

View file

@ -114,7 +114,7 @@ fn mirrored_exprs(a_expr: &Expr<'_>, a_ident: &Ident, b_expr: &Expr<'_>, b_ident
fn detect_lint(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, arg: &Expr<'_>) -> Option<LintTrigger> {
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
&& let Some(impl_id) = cx.tcx.impl_of_method(method_id)
&& let Some(impl_id) = cx.tcx.impl_of_assoc(method_id)
&& cx.tcx.type_of(impl_id).instantiate_identity().is_slice()
&& let ExprKind::Closure(&Closure { body, .. }) = arg.kind
&& let closure_body = cx.tcx.hir_body(body)

View file

@ -694,7 +694,7 @@ fn check_if_applicable_to_argument<'tcx>(cx: &LateContext<'tcx>, arg: &Expr<'tcx
sym::to_string => cx.tcx.is_diagnostic_item(sym::to_string_method, method_def_id),
sym::to_vec => cx
.tcx
.impl_of_method(method_def_id)
.impl_of_assoc(method_def_id)
.filter(|&impl_did| cx.tcx.type_of(impl_did).instantiate_identity().is_slice())
.is_some(),
_ => false,
@ -734,7 +734,7 @@ fn check_if_applicable_to_argument<'tcx>(cx: &LateContext<'tcx>, arg: &Expr<'tcx
fn check_borrow_predicate<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
if let ExprKind::MethodCall(_, caller, &[arg], _) = expr.kind
&& let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
&& cx.tcx.trait_of_item(method_def_id).is_none()
&& cx.tcx.trait_of_assoc(method_def_id).is_none()
&& let Some(borrow_id) = cx.tcx.get_diagnostic_item(sym::Borrow)
&& cx.tcx.predicates_of(method_def_id).predicates.iter().any(|(pred, _)| {
if let ClauseKind::Trait(trait_pred) = pred.kind().skip_binder()

View file

@ -79,7 +79,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: Symbo
applicability,
);
}
} else if let Some(impl_id) = cx.tcx.impl_of_method(def_id)
} else if let Some(impl_id) = cx.tcx.impl_of_assoc(def_id)
&& let Some(adt) = cx.tcx.type_of(impl_id).instantiate_identity().ty_adt_def()
&& matches!(cx.tcx.get_diagnostic_name(adt.did()), Some(sym::Option | sym::Result))
{
@ -131,7 +131,7 @@ fn is_calling_clone(cx: &LateContext<'_>, arg: &hir::Expr<'_>) -> bool {
hir::ExprKind::MethodCall(method, obj, [], _) => {
if method.ident.name == sym::clone
&& let Some(fn_id) = cx.typeck_results().type_dependent_def_id(closure_expr.hir_id)
&& let Some(trait_id) = cx.tcx.trait_of_item(fn_id)
&& let Some(trait_id) = cx.tcx.trait_of_assoc(fn_id)
// We check it's the `Clone` trait.
&& cx.tcx.lang_items().clone_trait().is_some_and(|id| id == trait_id)
// no autoderefs

View file

@ -18,7 +18,7 @@ pub(super) fn check<'tcx>(
name_span: Span,
) {
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
&& let Some(impl_id) = cx.tcx.impl_of_method(method_id)
&& let Some(impl_id) = cx.tcx.impl_of_assoc(method_id)
&& is_type_diagnostic_item(cx, cx.tcx.type_of(impl_id).instantiate_identity(), sym::Vec)
&& let ExprKind::Lit(Spanned {
node: LitKind::Int(Pu128(0), _),

View file

@ -41,7 +41,7 @@ fn check_op(cx: &LateContext<'_>, expr: &Expr<'_>, other: &Expr<'_>, left: bool)
ExprKind::MethodCall(_, arg, [], _)
if typeck
.type_dependent_def_id(expr.hir_id)
.and_then(|id| cx.tcx.trait_of_item(id))
.and_then(|id| cx.tcx.trait_of_assoc(id))
.is_some_and(|id| matches!(cx.tcx.get_diagnostic_name(id), Some(sym::ToString | sym::ToOwned))) =>
{
(arg, arg.span)

View file

@ -179,7 +179,7 @@ fn in_impl<'tcx>(
bin_op: DefId,
) -> Option<(&'tcx rustc_hir::Ty<'tcx>, &'tcx rustc_hir::Ty<'tcx>)> {
if let Some(block) = get_enclosing_block(cx, e.hir_id)
&& let Some(impl_def_id) = cx.tcx.impl_of_method(block.hir_id.owner.to_def_id())
&& let Some(impl_def_id) = cx.tcx.impl_of_assoc(block.hir_id.owner.to_def_id())
&& let item = cx.tcx.hir_expect_item(impl_def_id.expect_local())
&& let ItemKind::Impl(item) = &item.kind
&& let Some(of_trait) = &item.of_trait

View file

@ -380,7 +380,7 @@ fn can_switch_ranges<'tcx>(
if let ExprKind::MethodCall(_, receiver, _, _) = parent_expr.kind
&& receiver.hir_id == use_ctxt.child_id
&& let Some(method_did) = cx.typeck_results().type_dependent_def_id(parent_expr.hir_id)
&& let Some(trait_did) = cx.tcx.trait_of_item(method_did)
&& let Some(trait_did) = cx.tcx.trait_of_assoc(method_did)
&& matches!(
cx.tcx.get_diagnostic_name(trait_did),
Some(sym::Iterator | sym::IntoIterator | sym::RangeBounds)

View file

@ -113,7 +113,7 @@ impl<'tcx> LateLintPass<'tcx> for ReturnSelfNotMustUse {
) {
if matches!(kind, FnKind::Method(_, _))
// We are only interested in methods, not in functions or associated functions.
&& let Some(impl_def) = cx.tcx.impl_of_method(fn_def.to_def_id())
&& let Some(impl_def) = cx.tcx.impl_of_assoc(fn_def.to_def_id())
// We don't want this method to be te implementation of a trait because the
// `#[must_use]` should be put on the trait definition directly.
&& cx.tcx.trait_id_of_impl(impl_def).is_none()

View file

@ -206,7 +206,7 @@ fn check_partial_eq(cx: &LateContext<'_>, method_span: Span, method_def_id: Loca
let arg_ty = cx.typeck_results().expr_ty_adjusted(arg);
if let Some(fn_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
&& let Some(trait_id) = cx.tcx.trait_of_item(fn_id)
&& let Some(trait_id) = cx.tcx.trait_of_assoc(fn_id)
&& trait_id == trait_def_id
&& matches_ty(receiver_ty, arg_ty, self_arg, other_arg)
{
@ -250,7 +250,7 @@ fn check_to_string(cx: &LateContext<'_>, method_span: Span, method_def_id: Local
let is_bad = match expr.kind {
ExprKind::MethodCall(segment, _receiver, &[_arg], _) if segment.ident.name == name.name => {
if let Some(fn_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
&& let Some(trait_id) = cx.tcx.trait_of_item(fn_id)
&& let Some(trait_id) = cx.tcx.trait_of_assoc(fn_id)
&& trait_id == trait_def_id
{
true
@ -318,7 +318,7 @@ where
&& let ExprKind::Path(qpath) = f.kind
&& is_default_method_on_current_ty(self.cx.tcx, qpath, self.implemented_ty_id)
&& let Some(method_def_id) = path_def_id(self.cx, f)
&& let Some(trait_def_id) = self.cx.tcx.trait_of_item(method_def_id)
&& let Some(trait_def_id) = self.cx.tcx.trait_of_assoc(method_def_id)
&& self.cx.tcx.is_diagnostic_item(sym::Default, trait_def_id)
{
span_error(self.cx, self.method_span, expr);
@ -426,7 +426,7 @@ fn check_from(cx: &LateContext<'_>, method_span: Span, method_def_id: LocalDefId
if let Some((fn_def_id, node_args)) = fn_def_id_with_node_args(cx, expr)
&& let [s1, s2] = **node_args
&& let (Some(s1), Some(s2)) = (s1.as_type(), s2.as_type())
&& let Some(trait_def_id) = cx.tcx.trait_of_item(fn_def_id)
&& let Some(trait_def_id) = cx.tcx.trait_of_assoc(fn_def_id)
&& cx.tcx.is_diagnostic_item(sym::Into, trait_def_id)
&& get_impl_trait_def_id(cx, method_def_id) == cx.tcx.get_diagnostic_item(sym::From)
&& s1 == sig.inputs()[0]

View file

@ -84,7 +84,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedIoAmount {
/// get desugared to match.
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx hir::Block<'tcx>) {
let fn_def_id = block.hir_id.owner.to_def_id();
if let Some(impl_id) = cx.tcx.impl_of_method(fn_def_id)
if let Some(impl_id) = cx.tcx.impl_of_assoc(fn_def_id)
&& let Some(trait_id) = cx.tcx.trait_id_of_impl(impl_id)
{
// We don't want to lint inside io::Read or io::Write implementations, as the author has more
@ -300,7 +300,7 @@ fn check_io_mode(cx: &LateContext<'_>, call: &hir::Expr<'_>) -> Option<IoOp> {
};
if let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(call.hir_id)
&& let Some(trait_def_id) = cx.tcx.trait_of_item(method_def_id)
&& let Some(trait_def_id) = cx.tcx.trait_of_assoc(method_def_id)
{
if let Some(diag_name) = cx.tcx.get_diagnostic_name(trait_def_id) {
match diag_name {

View file

@ -51,7 +51,7 @@ impl ops::BitOrAssign for EagernessSuggestion {
fn fn_eagerness(cx: &LateContext<'_>, fn_id: DefId, name: Symbol, have_one_arg: bool) -> EagernessSuggestion {
use EagernessSuggestion::{Eager, Lazy, NoChange};
let ty = match cx.tcx.impl_of_method(fn_id) {
let ty = match cx.tcx.impl_of_assoc(fn_id) {
Some(id) => cx.tcx.type_of(id).instantiate_identity(),
None => return Lazy,
};

View file

@ -349,7 +349,7 @@ pub fn is_ty_alias(qpath: &QPath<'_>) -> bool {
/// Checks if the given method call expression calls an inherent method.
pub fn is_inherent_method_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) {
cx.tcx.trait_of_item(method_id).is_none()
cx.tcx.trait_of_assoc(method_id).is_none()
} else {
false
}
@ -357,7 +357,7 @@ pub fn is_inherent_method_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
/// Checks if a method is defined in an impl of a diagnostic item
pub fn is_diag_item_method(cx: &LateContext<'_>, def_id: DefId, diag_item: Symbol) -> bool {
if let Some(impl_did) = cx.tcx.impl_of_method(def_id)
if let Some(impl_did) = cx.tcx.impl_of_assoc(def_id)
&& let Some(adt) = cx.tcx.type_of(impl_did).instantiate_identity().ty_adt_def()
{
return cx.tcx.is_diagnostic_item(diag_item, adt.did());
@ -367,7 +367,7 @@ pub fn is_diag_item_method(cx: &LateContext<'_>, def_id: DefId, diag_item: Symbo
/// Checks if a method is in a diagnostic item trait
pub fn is_diag_trait_item(cx: &LateContext<'_>, def_id: DefId, diag_item: Symbol) -> bool {
if let Some(trait_did) = cx.tcx.trait_of_item(def_id) {
if let Some(trait_did) = cx.tcx.trait_of_assoc(def_id) {
return cx.tcx.is_diagnostic_item(diag_item, trait_did);
}
false
@ -620,7 +620,7 @@ fn is_default_equivalent_ctor(cx: &LateContext<'_>, def_id: DefId, path: &QPath<
if let QPath::TypeRelative(_, method) = path
&& method.ident.name == sym::new
&& let Some(impl_did) = cx.tcx.impl_of_method(def_id)
&& let Some(impl_did) = cx.tcx.impl_of_assoc(def_id)
&& let Some(adt) = cx.tcx.type_of(impl_did).instantiate_identity().ty_adt_def()
{
return std_types_symbols.iter().any(|&symbol| {

View file

@ -420,7 +420,7 @@ pub fn is_stable_const_fn(cx: &LateContext<'_>, def_id: DefId, msrv: Msrv) -> bo
.lookup_const_stability(def_id)
.or_else(|| {
cx.tcx
.trait_of_item(def_id)
.trait_of_assoc(def_id)
.and_then(|trait_def_id| cx.tcx.lookup_const_stability(trait_def_id))
})
.is_none_or(|const_stab| {