Fix clippy for impl_trait_header changes

This commit is contained in:
Cameron Steffen
2025-10-07 15:49:06 -05:00
parent b323f567d9
commit d9a53899db
14 changed files with 31 additions and 42 deletions
@@ -28,7 +28,7 @@ pub(super) fn check<'tcx>(
return;
}
let trait_ref = cx.tcx.impl_trait_ref(impl_id).expect("must be a trait implementation");
let trait_ref = cx.tcx.impl_trait_ref(impl_id);
// Only care about `impl PartialOrd<Foo> for Foo`
// For `impl PartialOrd<B> for A, input_types is [A, B]
@@ -27,7 +27,7 @@ pub(super) fn check<'tcx>(
return;
}
let trait_ref = cx.tcx.impl_trait_ref(impl_id).expect("must be a trait implementation");
let trait_ref = cx.tcx.impl_trait_ref(impl_id);
// Only care about `impl PartialEq<Foo> for Foo`
// For `impl PartialEq<B> for A, input_types is [A, B]
@@ -52,11 +52,9 @@
impl<'tcx> LateLintPass<'tcx> for FallibleImplFrom {
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
// check for `impl From<???> for ..`
if let hir::ItemKind::Impl(_) = &item.kind
&& let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(item.owner_id)
&& cx
.tcx
.is_diagnostic_item(sym::From, impl_trait_ref.skip_binder().def_id)
if let hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = &item.kind
&& let impl_trait_id = cx.tcx.impl_trait_id(item.owner_id)
&& cx.tcx.is_diagnostic_item(sym::From, impl_trait_id)
{
lint_impl_body(cx, item.owner_id, item.span);
}
@@ -76,8 +76,7 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
// `impl Into<target_ty> for self_ty`
&& let Some(GenericArgs { args: [GenericArg::Type(target_ty)], .. }) = into_trait_seg.args
&& span_is_local(item.span)
&& let Some(middle_trait_ref) = cx.tcx.impl_trait_ref(item.owner_id)
.map(ty::EarlyBinder::instantiate_identity)
&& let middle_trait_ref = cx.tcx.impl_trait_ref(item.owner_id).instantiate_identity()
&& cx.tcx.is_diagnostic_item(sym::Into, middle_trait_ref.def_id)
&& !matches!(middle_trait_ref.args.type_at(1).kind(), ty::Alias(ty::Opaque, _))
&& self.msrv.meets(cx, msrvs::RE_REBALANCING_COHERENCE)
@@ -339,8 +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_assoc(const_id)
&& let None = cx.tcx.impl_trait_ref(impl_id) // An inherent impl
&& let Some(impl_id) = cx.tcx.inherent_impl_of_assoc(const_id)
&& cx.tcx.type_of(impl_id).instantiate_identity().is_integral()
{
print_lint_and_sugg(cx, var_name, expr);
@@ -350,8 +349,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_assoc(func_id)
&& let None = cx.tcx.impl_trait_ref(impl_id) // An inherent impl
&& let Some(impl_id) = cx.tcx.inherent_impl_of_assoc(func_id)
&& cx.tcx.type_of(impl_id).instantiate_identity().is_integral()
{
print_lint_and_sugg(cx, var_name, expr);
@@ -10,8 +10,7 @@
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_assoc(call_id)
&& cx.tcx.impl_trait_ref(impl_id).is_none()
&& let Some(impl_id) = cx.tcx.inherent_impl_of_assoc(call_id)
&& let self_ty = cx.tcx.type_of(impl_id).instantiate_identity()
&& (self_ty.is_slice() || self_ty.is_str())
{
@@ -167,7 +167,7 @@ fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::Impl
let container_id = assoc_item.container_id(cx.tcx);
let trait_def_id = match assoc_item.container {
AssocContainer::Trait => Some(container_id),
AssocContainer::TraitImpl(_) => cx.tcx.impl_trait_ref(container_id).map(|t| t.skip_binder().def_id),
AssocContainer::TraitImpl(_) => Some(cx.tcx.impl_trait_id(container_id)),
AssocContainer::InherentImpl => None,
};
@@ -6,7 +6,7 @@
use rustc_hir::def_id::DefId;
use rustc_hir::{Block, Body, Expr, ExprKind, ImplItem, ImplItemKind, Item, ItemKind, LangItem, UnOp};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::ty::{EarlyBinder, TyCtxt, TypeckResults};
use rustc_middle::ty::{TyCtxt, TypeckResults};
use rustc_session::impl_lint_pass;
use rustc_span::sym;
use rustc_span::symbol::kw;
@@ -173,10 +173,11 @@ fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
}
});
let trait_impl = cx.tcx.impl_trait_ref(item.owner_id).skip_binder();
match trait_ {
Trait::Clone => {
if let Some(copy_trait) = self.copy_trait
&& let Some(trait_impl) = cx.tcx.impl_trait_ref(item.owner_id).map(EarlyBinder::skip_binder)
&& implements_trait(cx, trait_impl.self_ty(), copy_trait, &[])
{
for (assoc, _, block) in assoc_fns {
@@ -185,10 +186,9 @@ fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) {
}
},
Trait::PartialOrd => {
if let Some(trait_impl) = cx.tcx.impl_trait_ref(item.owner_id).map(EarlyBinder::skip_binder)
// If `Self` and `Rhs` are not the same type, then a corresponding `Ord` impl is not possible,
// since it doesn't have an `Rhs`
&& let [lhs, rhs] = trait_impl.args.as_slice()
// If `Self` and `Rhs` are not the same type, then a corresponding `Ord` impl is not possible,
// since it doesn't have an `Rhs`
if let [lhs, rhs] = trait_impl.args.as_slice()
&& lhs == rhs
&& let Some(ord_trait) = self.ord_trait
&& implements_trait(cx, trait_impl.self_ty(), ord_trait, &[])
@@ -87,7 +87,7 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
&& let Some(trait_id) = of_trait.trait_ref.trait_def_id()
&& send_trait == trait_id
&& of_trait.polarity == ImplPolarity::Positive
&& let Some(ty_trait_ref) = cx.tcx.impl_trait_ref(item.owner_id)
&& let ty_trait_ref = cx.tcx.impl_trait_ref(item.owner_id)
&& let self_ty = ty_trait_ref.instantiate_identity().self_ty()
&& let ty::Adt(adt_def, impl_trait_args) = self_ty.kind()
{
@@ -6,9 +6,9 @@
use rustc_errors::Applicability;
use rustc_hir::def_id::DefId;
use rustc_hir::hir_id::HirIdMap;
use rustc_hir::{Body, Expr, ExprKind, HirId, ImplItem, ImplItemKind, Node, PatKind, TraitItem, TraitItemKind};
use rustc_hir::{Body, Expr, ExprKind, HirId, ImplItem, ImplItemImplKind, ImplItemKind, Node, PatKind, TraitItem, TraitItemKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::{self, ConstKind, EarlyBinder, GenericArgKind, GenericArgsRef};
use rustc_middle::ty::{self, ConstKind, GenericArgKind, GenericArgsRef};
use rustc_session::impl_lint_pass;
use rustc_span::Span;
use rustc_span::symbol::{Ident, kw};
@@ -320,15 +320,14 @@ fn check_body(&mut self, cx: &LateContext<'tcx>, body: &Body<'tcx>) {
Node::ImplItem(&ImplItem {
kind: ImplItemKind::Fn(ref sig, _),
owner_id,
impl_kind,
..
}) => {
if let Node::Item(item) = cx.tcx.parent_hir_node(owner_id.into())
&& let Some(trait_ref) = cx
.tcx
.impl_trait_ref(item.owner_id)
.map(EarlyBinder::instantiate_identity)
&& let Some(trait_item_id) = cx.tcx.trait_item_of(owner_id)
if let ImplItemImplKind::Trait { trait_item_def_id, .. } = impl_kind
&& let Ok(trait_item_id) = trait_item_def_id
{
let impl_id = cx.tcx.parent(owner_id.into());
let trait_ref = cx.tcx.impl_trait_ref(impl_id).instantiate_identity();
(
trait_item_id,
FnKind::ImplTraitFn(
@@ -113,10 +113,9 @@ fn check_fn(
) {
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_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()
&& cx.tcx.inherent_impl_of_assoc(fn_def.to_def_id()).is_some()
{
let hir_id = cx.tcx.local_def_id_to_hir_id(fn_def);
check_method(cx, decl, fn_def, span, hir_id.expect_owner());
@@ -85,9 +85,8 @@ 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_assoc(fn_def_id)
&& let Some(trait_id) = cx.tcx.trait_id_of_impl(impl_id)
{
if let Some(impl_id) = cx.tcx.trait_impl_of_assoc(fn_def_id) {
let trait_id = cx.tcx.impl_trait_id(impl_id);
// We don't want to lint inside io::Read or io::Write implementations, as the author has more
// information about their trait implementation than our lint, see https://github.com/rust-lang/rust-clippy/issues/4836
if let Some(trait_name) = cx.tcx.get_diagnostic_name(trait_id)
@@ -10,7 +10,7 @@
use rustc_hir::intravisit::{InferKind, Visitor, VisitorExt, walk_ty};
use rustc_hir::{
self as hir, AmbigArg, Expr, ExprKind, FnRetTy, FnSig, GenericArgsParentheses, GenericParamKind, HirId, Impl,
ImplItemKind, Item, ItemKind, Pat, PatExpr, PatExprKind, PatKind, Path, QPath, Ty, TyKind,
ImplItemImplKind, ImplItemKind, Item, ItemKind, Pat, PatExpr, PatExprKind, PatKind, Path, QPath, Ty, TyKind,
};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::Ty as MiddleTy;
@@ -142,13 +142,14 @@ fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &hir::ImplItem<'_
// We want to skip types in trait `impl`s that aren't declared as `Self` in the trait
// declaration. The collection of those types is all this method implementation does.
if let ImplItemKind::Fn(FnSig { decl, .. }, ..) = impl_item.kind
&& let ImplItemImplKind::Trait { .. } = impl_item.impl_kind
&& let Some(&mut StackItem::Check {
impl_id,
ref mut types_to_skip,
..
}) = self.stack.last_mut()
&& let Some(impl_trait_ref) = cx.tcx.impl_trait_ref(impl_id)
{
let impl_trait_ref = cx.tcx.impl_trait_ref(impl_id);
// `self_ty` is the semantic self type of `impl <trait> for <type>`. This cannot be
// `Self`.
let self_ty = impl_trait_ref.instantiate_identity().self_ty();
@@ -26,10 +26,7 @@ fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
items,
..
}) = &item.kind
&& let Some(trait_ref) = cx
.tcx
.impl_trait_ref(item.owner_id)
.map(EarlyBinder::instantiate_identity)
&& let trait_ref = cx.tcx.impl_trait_ref(item.owner_id).instantiate_identity()
&& internal_paths::EARLY_LINT_PASS.matches(cx, trait_ref.def_id)
&& let ty::Adt(self_ty_def, _) = trait_ref.self_ty().kind()
&& self_ty_def.is_struct()