Skip to content

Commit 0c9d6a9

Browse files
committed
fix: use_self FP on type in const generics
1 parent 65c339e commit 0c9d6a9

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

clippy_lints/src/use_self.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_hir::def_id::LocalDefId;
1010
use rustc_hir::intravisit::{InferKind, Visitor, VisitorExt, walk_ty};
1111
use rustc_hir::{
1212
self as hir, AmbigArg, Expr, ExprKind, FnRetTy, FnSig, GenericArgsParentheses, GenericParamKind, HirId, Impl,
13-
ImplItemImplKind, ImplItemKind, Item, ItemKind, Pat, PatExpr, PatExprKind, PatKind, Path, QPath, Ty, TyKind,
13+
ImplItemImplKind, ImplItemKind, Item, ItemKind, Node, Pat, PatExpr, PatExprKind, PatKind, Path, QPath, Ty, TyKind,
1414
};
1515
use rustc_lint::{LateContext, LateLintPass};
1616
use rustc_middle::ty::Ty as MiddleTy;
@@ -213,6 +213,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
213213
path.res,
214214
Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } | Res::Def(DefKind::TyParam, _)
215215
)
216+
&& !ty_is_in_generic_args(cx, hir_ty)
216217
&& !types_to_skip.contains(&hir_ty.hir_id)
217218
&& let ty = ty_from_hir_ty(cx, hir_ty.as_unambig_ty())
218219
&& let impl_ty = cx.tcx.type_of(impl_id).instantiate_identity()
@@ -312,6 +313,17 @@ fn lint_path_to_variant(cx: &LateContext<'_>, path: &Path<'_>) {
312313
}
313314
}
314315

316+
fn ty_is_in_generic_args<'tcx>(cx: &LateContext<'tcx>, hir_ty: &Ty<'tcx, AmbigArg>) -> bool {
317+
let Node::ImplItem(impl_item) = cx.tcx.parent_hir_node(hir_ty.hir_id) else {
318+
return false;
319+
};
320+
321+
impl_item.generics.params.iter().any(|param| match param.kind {
322+
GenericParamKind::Const { ty: const_ty, .. } => const_ty.hir_id == hir_ty.hir_id,
323+
_ => false,
324+
})
325+
}
326+
315327
/// Checks whether types `a` and `b` have the same lifetime parameters.
316328
///
317329
/// This function does not check that types `a` and `b` are the same types.

tests/ui/use_self.fixed

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,3 +769,15 @@ mod issue_13277 {
769769
type Item<'foo> = Option<Bar<'foo>>;
770770
}
771771
}
772+
773+
mod issue16164 {
774+
trait Bits {
775+
fn bit<const I: u8>(self) -> bool;
776+
}
777+
778+
impl Bits for u8 {
779+
fn bit<const I: u8>(self) -> bool {
780+
todo!()
781+
}
782+
}
783+
}

tests/ui/use_self.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,3 +769,15 @@ mod issue_13277 {
769769
type Item<'foo> = Option<Bar<'foo>>;
770770
}
771771
}
772+
773+
mod issue16164 {
774+
trait Bits {
775+
fn bit<const I: u8>(self) -> bool;
776+
}
777+
778+
impl Bits for u8 {
779+
fn bit<const I: u8>(self) -> bool {
780+
todo!()
781+
}
782+
}
783+
}

0 commit comments

Comments
 (0)