@@ -25,6 +25,7 @@ use rustc_span::{ErrorGuaranteed, kw};
2525
2626use crate :: constrained_generic_params as cgp;
2727use crate :: errors:: UnconstrainedGenericParameter ;
28+ use crate :: hir:: def:: Res ;
2829
2930mod min_specialization;
3031
@@ -253,8 +254,7 @@ pub(crate) fn enforce_impl_non_lifetime_params_are_constrained(
253254 res
254255}
255256
256- /// A HIR visitor that checks if a specific generic parameter (by its `DefId`)
257- /// is used within a given HIR tree.
257+ /// Use a Visitor to find usages of the type or lifetime parameter
258258struct ParamUsageVisitor < ' tcx > {
259259 tcx : TyCtxt < ' tcx > ,
260260 /// The `DefId` of the generic parameter we are looking for.
@@ -270,10 +270,8 @@ impl<'tcx> Visitor<'tcx> for ParamUsageVisitor<'tcx> {
270270 self . tcx
271271 }
272272
273- /// We use `ControlFlow` to stop visiting as soon as we find what we're looking for.
274273 type Result = ControlFlow < ( ) > ;
275274
276- /// This is the primary method for finding usages of type or const parameters.
277275 fn visit_path ( & mut self , path : & Path < ' tcx > , _id : HirId ) -> Self :: Result {
278276 if let Some ( res_def_id) = path. res . opt_def_id ( ) {
279277 if res_def_id == self . param_def_id {
@@ -318,12 +316,25 @@ fn suggest_to_remove_or_use_generic(
318316 return ;
319317 } ;
320318
321- // search if the parameter is used in the impl body
322- let mut visitor = ParamUsageVisitor {
323- tcx, // Pass the TyCtxt
324- param_def_id : param. def_id ,
325- found : false ,
319+ // get the struct_def_id from the self type
320+ let Some ( struct_def_id) = ( || {
321+ let ty = hir_impl. self_ty ;
322+ if let TyKind :: Path ( QPath :: Resolved ( _, path) ) = ty. kind
323+ && let Res :: Def ( _, def_id) = path. res
324+ {
325+ Some ( def_id)
326+ } else {
327+ None
328+ }
329+ } ) ( ) else {
330+ return ;
326331 } ;
332+ let generics = tcx. generics_of ( struct_def_id) ;
333+ // println!("number of struct generics: {}", generics.own_params.len());
334+ // println!("number of impl generics: {}", hir_impl.generics.params.len());
335+
336+ // search if the parameter is used in the impl body
337+ let mut visitor = ParamUsageVisitor { tcx, param_def_id : param. def_id , found : false } ;
327338
328339 for item_ref in hir_impl. items {
329340 let _ = visitor. visit_impl_item_ref ( item_ref) ;
0 commit comments