@@ -18,8 +18,8 @@ use rustc_ast::AttrStyle;
1818use rustc_hir:: intravisit:: FnKind ;
1919use rustc_hir:: {
2020 Block , BlockCheckMode , Body , Closure , Destination , Expr , ExprKind , FieldDef , FnHeader , FnRetTy , HirId , Impl ,
21- ImplItem , ImplItemKind , IsAuto , Item , ItemKind , LoopSource , MatchSource , MutTy , Node , QPath , Safety , TraitItem ,
22- TraitItemKind , Ty , TyKind , UnOp , UnsafeSource , Variant , VariantData , YieldSource ,
21+ ImplItem , ImplItemKind , IsAuto , Item , ItemKind , Lit , LoopSource , MatchSource , MutTy , Node , Path , QPath , Safety ,
22+ TraitItem , TraitItemKind , Ty , TyKind , UnOp , UnsafeSource , Variant , VariantData , YieldSource ,
2323} ;
2424use rustc_lint:: { LateContext , LintContext } ;
2525use rustc_middle:: ty:: TyCtxt ;
@@ -121,6 +121,26 @@ fn qpath_search_pat(path: &QPath<'_>) -> (Pat, Pat) {
121121 }
122122}
123123
124+ fn path_search_pat ( path : & Path < ' _ > ) -> ( Pat , Pat ) {
125+ let ( head, tail) = match path. segments {
126+ [ head, .., tail] => ( head, tail) ,
127+ [ p] => ( p, p) ,
128+ [ ] => return ( Pat :: Str ( "" ) , Pat :: Str ( "" ) ) ,
129+ } ;
130+ (
131+ if head. ident . name == kw:: PathRoot {
132+ Pat :: Str ( "::" )
133+ } else {
134+ Pat :: Sym ( head. ident . name )
135+ } ,
136+ if tail. args . is_some ( ) {
137+ Pat :: Str ( ">" )
138+ } else {
139+ Pat :: Sym ( tail. ident . name )
140+ } ,
141+ )
142+ }
143+
124144/// Get the search patterns to use for the given expression
125145fn expr_search_pat ( tcx : TyCtxt < ' _ > , e : & Expr < ' _ > ) -> ( Pat , Pat ) {
126146 match e. kind {
@@ -355,33 +375,39 @@ fn ty_search_pat(ty: &Ty<'_>) -> (Pat, Pat) {
355375 }
356376}
357377
378+ fn ident_search_pat ( ident : Ident ) -> ( Pat , Pat ) {
379+ ( Pat :: Sym ( ident. name ) , Pat :: Sym ( ident. name ) )
380+ }
381+
358382pub trait WithSearchPat < ' cx > {
359383 type Context : LintContext ;
360384 fn search_pat ( & self , cx : & Self :: Context ) -> ( Pat , Pat ) ;
361385 fn span ( & self ) -> Span ;
362386}
363387macro_rules! impl_with_search_pat {
364- ( $cx: ident: $ty: ident with $fn: ident $( ( $tcx: ident) ) ?) => {
365- impl <' cx> WithSearchPat <' cx> for $ty<' cx> {
366- type Context = $cx<' cx>;
367- #[ allow( unused_variables) ]
368- fn search_pat( & self , cx: & Self :: Context ) -> ( Pat , Pat ) {
369- $( let $tcx = cx. tcx; ) ?
370- $fn( $( $tcx, ) ? self )
388+ ( ( $cx_ident: ident: $cx_ty: ident<$cx_lt: lifetime>, $self: tt: $ty: ty) => $fn: ident( $( $args: tt) * ) ) => {
389+ impl <$cx_lt> WithSearchPat <$cx_lt> for $ty {
390+ type Context = $cx_ty<$cx_lt>;
391+ fn search_pat( & $self, $cx_ident: & Self :: Context ) -> ( Pat , Pat ) {
392+ $fn( $( $args) * )
371393 }
372394 fn span( & self ) -> Span {
373395 self . span
374396 }
375397 }
376398 } ;
377399}
378- impl_with_search_pat ! ( LateContext : Expr with expr_search_pat( tcx) ) ;
379- impl_with_search_pat ! ( LateContext : Item with item_search_pat) ;
380- impl_with_search_pat ! ( LateContext : TraitItem with trait_item_search_pat) ;
381- impl_with_search_pat ! ( LateContext : ImplItem with impl_item_search_pat) ;
382- impl_with_search_pat ! ( LateContext : FieldDef with field_def_search_pat) ;
383- impl_with_search_pat ! ( LateContext : Variant with variant_search_pat) ;
384- impl_with_search_pat ! ( LateContext : Ty with ty_search_pat) ;
400+ impl_with_search_pat ! ( ( cx: LateContext <' tcx>, self : Expr <' tcx>) => expr_search_pat( cx. tcx, self ) ) ;
401+ impl_with_search_pat ! ( ( _cx: LateContext <' tcx>, self : Item <' _>) => item_search_pat( self ) ) ;
402+ impl_with_search_pat ! ( ( _cx: LateContext <' tcx>, self : TraitItem <' _>) => trait_item_search_pat( self ) ) ;
403+ impl_with_search_pat ! ( ( _cx: LateContext <' tcx>, self : ImplItem <' _>) => impl_item_search_pat( self ) ) ;
404+ impl_with_search_pat ! ( ( _cx: LateContext <' tcx>, self : FieldDef <' _>) => field_def_search_pat( self ) ) ;
405+ impl_with_search_pat ! ( ( _cx: LateContext <' tcx>, self : Variant <' _>) => variant_search_pat( self ) ) ;
406+ impl_with_search_pat ! ( ( _cx: LateContext <' tcx>, self : Ty <' _>) => ty_search_pat( self ) ) ;
407+ impl_with_search_pat ! ( ( _cx: LateContext <' tcx>, self : Attribute ) => attr_search_pat( self ) ) ;
408+ impl_with_search_pat ! ( ( _cx: LateContext <' tcx>, self : Ident ) => ident_search_pat( * self ) ) ;
409+ impl_with_search_pat ! ( ( _cx: LateContext <' tcx>, self : Lit ) => lit_search_pat( & self . node) ) ;
410+ impl_with_search_pat ! ( ( _cx: LateContext <' tcx>, self : Path <' _>) => path_search_pat( self ) ) ;
385411
386412impl < ' cx > WithSearchPat < ' cx > for ( & FnKind < ' cx > , & Body < ' cx > , HirId , Span ) {
387413 type Context = LateContext < ' cx > ;
@@ -395,32 +421,6 @@ impl<'cx> WithSearchPat<'cx> for (&FnKind<'cx>, &Body<'cx>, HirId, Span) {
395421 }
396422}
397423
398- // `Attribute` does not have the `hir` associated lifetime, so we cannot use the macro
399- impl < ' cx > WithSearchPat < ' cx > for & ' cx Attribute {
400- type Context = LateContext < ' cx > ;
401-
402- fn search_pat ( & self , _cx : & Self :: Context ) -> ( Pat , Pat ) {
403- attr_search_pat ( self )
404- }
405-
406- fn span ( & self ) -> Span {
407- self . span
408- }
409- }
410-
411- // `Ident` does not have the `hir` associated lifetime, so we cannot use the macro
412- impl < ' cx > WithSearchPat < ' cx > for Ident {
413- type Context = LateContext < ' cx > ;
414-
415- fn search_pat ( & self , _cx : & Self :: Context ) -> ( Pat , Pat ) {
416- ( Pat :: Sym ( self . name ) , Pat :: Sym ( self . name ) )
417- }
418-
419- fn span ( & self ) -> Span {
420- self . span
421- }
422- }
423-
424424/// Checks if the item likely came from a proc-macro.
425425///
426426/// This should be called after `in_external_macro` and the initial pattern matching of the ast as
0 commit comments