@@ -10,7 +10,7 @@ use rustc_hir::def_id::LocalDefId;
1010use rustc_hir:: intravisit:: { InferKind , Visitor , VisitorExt , walk_ty} ;
1111use 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} ;
1515use rustc_lint:: { LateContext , LateLintPass } ;
1616use 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,46 @@ 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+ cx. tcx . hir_parent_iter ( hir_ty. hir_id ) . any ( |( _, parent) | {
318+ matches ! ( parent, Node :: ImplItem ( impl_item) if impl_item. generics. params. iter( ) . any( |param| {
319+ let GenericParamKind :: Const { ty: const_ty, .. } = & param. kind else {
320+ return false ;
321+ } ;
322+ ty_contains_ty( const_ty, hir_ty)
323+ } ) )
324+ } )
325+ }
326+
327+ fn ty_contains_ty < ' tcx > ( outer : & Ty < ' tcx > , inner : & Ty < ' tcx , AmbigArg > ) -> bool {
328+ struct ContainsVisitor < ' tcx > {
329+ inner : & ' tcx Ty < ' tcx , AmbigArg > ,
330+ found : bool ,
331+ }
332+
333+ impl < ' tcx > Visitor < ' tcx > for ContainsVisitor < ' tcx > {
334+ fn visit_ty ( & mut self , t : & ' tcx Ty < ' tcx , AmbigArg > ) {
335+ if self . found {
336+ return ;
337+ }
338+
339+ if t. hir_id == self . inner . hir_id {
340+ self . found = true ;
341+ return ;
342+ }
343+
344+ walk_ty ( self , t) ;
345+ }
346+ }
347+
348+ let Some ( outer) = outer. try_as_ambig_ty ( ) else {
349+ return false ;
350+ } ;
351+ let mut visitor = ContainsVisitor { inner, found : false } ;
352+ visitor. visit_ty ( outer) ;
353+ visitor. found
354+ }
355+
315356/// Checks whether types `a` and `b` have the same lifetime parameters.
316357///
317358/// This function does not check that types `a` and `b` are the same types.
0 commit comments