@@ -6,8 +6,8 @@ use rustc_errors::Applicability;
66use rustc_hir:: def:: { DefKind , Res } ;
77use rustc_hir:: { Expr , ExprKind , QPath } ;
88use rustc_lint:: { LateContext , LateLintPass } ;
9- use rustc_middle:: ty:: { self , List , Ty } ;
10- use rustc_session:: declare_lint_pass ;
9+ use rustc_middle:: ty:: { List , Ty , TyCtxt } ;
10+ use rustc_session:: impl_lint_pass ;
1111use rustc_span:: sym;
1212use std:: iter;
1313
@@ -36,12 +36,35 @@ declare_clippy_lint! {
3636 being enclosed in `Path::new` when the argument implements the trait"
3737}
3838
39- declare_lint_pass ! ( NeedlessPathNew => [ NEEDLESS_PATH_NEW ] ) ;
39+ impl_lint_pass ! ( NeedlessPathNew < ' _> => [ NEEDLESS_PATH_NEW ] ) ;
4040
41- impl < ' tcx > LateLintPass < ' tcx > for NeedlessPathNew {
41+ pub struct NeedlessPathNew < ' tcx > {
42+ path_ty : Option < Ty < ' tcx > > ,
43+ asref_def_id : Option < DefId > ,
44+ }
45+
46+ impl < ' tcx > NeedlessPathNew < ' tcx > {
47+ pub fn new ( tcx : TyCtxt < ' tcx > ) -> Self {
48+ Self {
49+ path_ty : ( tcx. get_diagnostic_item ( sym:: Path ) )
50+ . map ( |path_def_id| Ty :: new_adt ( tcx, tcx. adt_def ( path_def_id) , List :: empty ( ) ) ) ,
51+ asref_def_id : tcx. get_diagnostic_item ( sym:: AsRef ) ,
52+ }
53+ }
54+ }
55+
56+ impl < ' tcx > LateLintPass < ' tcx > for NeedlessPathNew < ' tcx > {
4257 fn check_expr ( & mut self , cx : & LateContext < ' tcx > , e : & ' tcx Expr < ' tcx > ) {
4358 let tcx = cx. tcx ;
4459
60+ let Some ( path_ty) = self . path_ty else {
61+ return ;
62+ } ;
63+
64+ let Some ( asref_def_id) = self . asref_def_id else {
65+ return ;
66+ } ;
67+
4568 let ( fn_did, arguments) = match e. kind {
4669 ExprKind :: Call ( callee, args)
4770 if let Res :: Def ( DefKind :: Fn | DefKind :: AssocFn , did) = path_res ( cx, callee) =>
@@ -72,17 +95,6 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPathNew {
7295 }
7396 } ;
7497
75- let path_ty = {
76- let Some ( path_def_id) = tcx. get_diagnostic_item ( sym:: Path ) else {
77- return ;
78- } ;
79- Ty :: new_adt ( tcx, tcx. adt_def ( path_def_id) , List :: empty ( ) )
80- } ;
81-
82- let Some ( asref_def_id) = tcx. get_diagnostic_item ( sym:: AsRef ) else {
83- return ;
84- } ;
85-
8698 let implements_asref_path = |arg| implements_trait ( cx, arg, asref_def_id, & [ path_ty. into ( ) ] ) ;
8799
88100 let parameters = sig. inputs ( ) ;
0 commit comments