Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion clippy_lints/src/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_hir::def_id::LocalDefId;
use rustc_hir::intravisit::{InferKind, Visitor, VisitorExt, walk_ty};
use rustc_hir::{
self as hir, AmbigArg, Expr, ExprKind, FnRetTy, FnSig, GenericArgsParentheses, GenericParamKind, HirId, Impl,
ImplItemImplKind, ImplItemKind, Item, ItemKind, Pat, PatExpr, PatExprKind, PatKind, Path, QPath, Ty, TyKind,
ImplItemImplKind, ImplItemKind, Item, ItemKind, Node, Pat, PatExpr, PatExprKind, PatKind, Path, QPath, Ty, TyKind,
};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::Ty as MiddleTy;
Expand Down Expand Up @@ -213,6 +213,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
path.res,
Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } | Res::Def(DefKind::TyParam, _)
)
&& !ty_is_in_generic_args(cx, hir_ty)
&& !types_to_skip.contains(&hir_ty.hir_id)
&& let ty = ty_from_hir_ty(cx, hir_ty.as_unambig_ty())
&& let impl_ty = cx.tcx.type_of(impl_id).instantiate_identity()
Expand Down Expand Up @@ -312,6 +313,32 @@ fn lint_path_to_variant(cx: &LateContext<'_>, path: &Path<'_>) {
}
}

fn ty_is_in_generic_args<'tcx>(cx: &LateContext<'tcx>, hir_ty: &Ty<'tcx, AmbigArg>) -> bool {
cx.tcx.hir_parent_iter(hir_ty.hir_id).any(|(_, parent)| {
matches!(parent, Node::ImplItem(impl_item) if impl_item.generics.params.iter().any(|param| {
let GenericParamKind::Const { ty: const_ty, .. } = &param.kind else {
return false;
};
ty_contains_ty(const_ty, hir_ty)
}))
})
}

fn ty_contains_ty<'tcx>(outer: &Ty<'tcx>, inner: &Ty<'tcx, AmbigArg>) -> bool {
if outer.hir_id == inner.hir_id {
return true;
}

match &outer.kind {
TyKind::Array(element_ty, _)
| TyKind::Slice(element_ty)
| TyKind::Ptr(hir::MutTy { ty: element_ty, .. })
| TyKind::Ref(_, hir::MutTy { ty: element_ty, .. }) => ty_contains_ty(element_ty, inner),
TyKind::Tup(element_tys) => element_tys.iter().any(|ty| ty_contains_ty(ty, inner)),
_ => false, // TODO: handle more cases if needed
}
}

/// Checks whether types `a` and `b` have the same lifetime parameters.
///
/// This function does not check that types `a` and `b` are the same types.
Expand Down
25 changes: 25 additions & 0 deletions tests/ui/use_self.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
clippy::needless_lifetimes,
clippy::missing_transmute_annotations
)]
#![allow(incomplete_features)]
#![feature(adt_const_params)]
#![feature(unsized_const_params)]

#[macro_use]
extern crate proc_macro_derive;
Expand Down Expand Up @@ -769,3 +772,25 @@ mod issue_13277 {
type Item<'foo> = Option<Bar<'foo>>;
}
}

mod issue16164 {
trait Bits {
fn bit<const I: u8>(self) -> bool;
}

impl Bits for u8 {
fn bit<const I: u8>(self) -> bool {
todo!()
}
}

trait T {
fn f<const C: (u8, u8)>(self) -> bool;
}

impl T for u8 {
fn f<const C: (u8, u8)>(self) -> bool {
todo!()
}
}
}
25 changes: 25 additions & 0 deletions tests/ui/use_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
clippy::needless_lifetimes,
clippy::missing_transmute_annotations
)]
#![allow(incomplete_features)]
#![feature(adt_const_params)]
#![feature(unsized_const_params)]

#[macro_use]
extern crate proc_macro_derive;
Expand Down Expand Up @@ -769,3 +772,25 @@ mod issue_13277 {
type Item<'foo> = Option<Bar<'foo>>;
}
}

mod issue16164 {
trait Bits {
fn bit<const I: u8>(self) -> bool;
}

impl Bits for u8 {
fn bit<const I: u8>(self) -> bool {
todo!()
}
}

trait T {
fn f<const C: (u8, u8)>(self) -> bool;
}

impl T for u8 {
fn f<const C: (u8, u8)>(self) -> bool {
todo!()
}
}
}
Loading