@@ -541,6 +541,102 @@ fn ident_search_pat(ident: Ident) -> (Pat, Pat) {
541
541
( Pat :: Sym ( ident. name ) , Pat :: Sym ( ident. name ) )
542
542
}
543
543
544
+ fn pat_search_pat ( tcx : TyCtxt < ' _ > , pat : & rustc_hir:: Pat < ' _ > ) -> ( Pat , Pat ) {
545
+ match pat. kind {
546
+ rustc_hir:: PatKind :: Missing | rustc_hir:: PatKind :: Err ( _) => ( Pat :: Str ( "" ) , Pat :: Str ( "" ) ) ,
547
+ rustc_hir:: PatKind :: Wild => ( Pat :: Sym ( kw:: Underscore ) , Pat :: Sym ( kw:: Underscore ) ) ,
548
+ rustc_hir:: PatKind :: Binding ( binding_mode, _, ident, Some ( end_pat) ) => {
549
+ let start = match binding_mode. 0 {
550
+ rustc_hir:: ByRef :: Yes ( rustc_hir:: Mutability :: Not ) => Pat :: Str ( "ref" ) ,
551
+ rustc_hir:: ByRef :: Yes ( rustc_hir:: Mutability :: Mut ) => Pat :: Str ( "ref mut" ) ,
552
+ rustc_hir:: ByRef :: No => {
553
+ let ( s, _) = ident_search_pat ( ident) ;
554
+ s
555
+ } ,
556
+ } ;
557
+
558
+ let ( _, end) = pat_search_pat ( tcx, end_pat) ;
559
+ ( start, end)
560
+ } ,
561
+ rustc_hir:: PatKind :: Binding ( binding_mode, _, ident, None ) => {
562
+ let ( s, end) = ident_search_pat ( ident) ;
563
+ let start = match binding_mode. 0 {
564
+ rustc_hir:: ByRef :: Yes ( rustc_hir:: Mutability :: Not ) => Pat :: Str ( "ref" ) ,
565
+ rustc_hir:: ByRef :: Yes ( rustc_hir:: Mutability :: Mut ) => Pat :: Str ( "ref mut" ) ,
566
+ rustc_hir:: ByRef :: No => s,
567
+ } ;
568
+
569
+ ( start, end)
570
+ } ,
571
+ rustc_hir:: PatKind :: Struct ( path, _, _) => {
572
+ let ( start, _) = qpath_search_pat ( & path) ;
573
+ ( start, Pat :: Str ( "}" ) )
574
+ } ,
575
+ rustc_hir:: PatKind :: TupleStruct ( path, _, _) => {
576
+ let ( start, _) = qpath_search_pat ( & path) ;
577
+ ( start, Pat :: Str ( ")" ) )
578
+ } ,
579
+ rustc_hir:: PatKind :: Or ( plist) => {
580
+ // documented invariant
581
+ debug_assert ! ( plist. len( ) >= 2 ) ;
582
+ let ( start, _) = pat_search_pat ( tcx, plist. first ( ) . unwrap ( ) ) ;
583
+ let ( _, end) = pat_search_pat ( tcx, plist. last ( ) . unwrap ( ) ) ;
584
+ ( start, end)
585
+ } ,
586
+ rustc_hir:: PatKind :: Never => ( Pat :: Str ( "!" ) , Pat :: Str ( "" ) ) ,
587
+ rustc_hir:: PatKind :: Tuple ( _, _) => ( Pat :: Str ( "(" ) , Pat :: Str ( ")" ) ) ,
588
+ rustc_hir:: PatKind :: Box ( p) => {
589
+ let ( _, end) = pat_search_pat ( tcx, p) ;
590
+ ( Pat :: Str ( "box" ) , end)
591
+ } ,
592
+ rustc_hir:: PatKind :: Deref ( _) => ( Pat :: Str ( "deref!(" ) , Pat :: Str ( ")" ) ) ,
593
+ rustc_hir:: PatKind :: Ref ( p, _) => {
594
+ let ( _, end) = pat_search_pat ( tcx, p) ;
595
+ ( Pat :: Str ( "&" ) , end)
596
+ } ,
597
+ rustc_hir:: PatKind :: Expr ( expr) => pat_search_pat_expr_kind ( expr) ,
598
+ rustc_hir:: PatKind :: Guard ( pat, guard) => {
599
+ let ( start, _) = pat_search_pat ( tcx, pat) ;
600
+ let ( _, end) = expr_search_pat ( tcx, guard) ;
601
+ ( start, end)
602
+ } ,
603
+ rustc_hir:: PatKind :: Range ( None , None , range) => match range {
604
+ rustc_hir:: RangeEnd :: Included => ( Pat :: Str ( "..=" ) , Pat :: Str ( "" ) ) ,
605
+ rustc_hir:: RangeEnd :: Excluded => ( Pat :: Str ( ".." ) , Pat :: Str ( "" ) ) ,
606
+ } ,
607
+ rustc_hir:: PatKind :: Range ( r_start, r_end, range) => {
608
+ let ( start, _) = match r_start {
609
+ Some ( e) => pat_search_pat_expr_kind ( e) ,
610
+ None => match range {
611
+ rustc_hir:: RangeEnd :: Included => ( Pat :: Str ( "..=" ) , Pat :: Str ( "" ) ) ,
612
+ rustc_hir:: RangeEnd :: Excluded => ( Pat :: Str ( ".." ) , Pat :: Str ( "" ) ) ,
613
+ } ,
614
+ } ;
615
+
616
+ let ( _, end) = match r_end {
617
+ Some ( e) => pat_search_pat_expr_kind ( e) ,
618
+ None => match range {
619
+ rustc_hir:: RangeEnd :: Included => ( Pat :: Str ( "" ) , Pat :: Str ( "..=" ) ) ,
620
+ rustc_hir:: RangeEnd :: Excluded => ( Pat :: Str ( "" ) , Pat :: Str ( ".." ) ) ,
621
+ } ,
622
+ } ;
623
+ ( start, end)
624
+ } ,
625
+ rustc_hir:: PatKind :: Slice ( _, _, _) => ( Pat :: Str ( "[" ) , Pat :: Str ( "]" ) ) ,
626
+ }
627
+ }
628
+
629
+ fn pat_search_pat_expr_kind ( expr : & crate :: PatExpr < ' _ > ) -> ( Pat , Pat ) {
630
+ match expr. kind {
631
+ crate :: PatExprKind :: Lit { lit, negated } => {
632
+ let ( start, end) = lit_search_pat ( & lit. node ) ;
633
+ if negated { ( Pat :: Str ( "!" ) , end) } else { ( start, end) }
634
+ } ,
635
+ crate :: PatExprKind :: ConstBlock ( _block) => ( Pat :: Str ( "const {" ) , Pat :: Str ( "}" ) ) ,
636
+ crate :: PatExprKind :: Path ( path) => qpath_search_pat ( & path) ,
637
+ }
638
+ }
639
+
544
640
pub trait WithSearchPat < ' cx > {
545
641
type Context : LintContext ;
546
642
fn search_pat ( & self , cx : & Self :: Context ) -> ( Pat , Pat ) ;
@@ -569,6 +665,7 @@ impl_with_search_pat!((_cx: LateContext<'tcx>, self: Ty<'_>) => ty_search_pat(se
569
665
impl_with_search_pat ! ( ( _cx: LateContext <' tcx>, self : Ident ) => ident_search_pat( * self ) ) ;
570
666
impl_with_search_pat ! ( ( _cx: LateContext <' tcx>, self : Lit ) => lit_search_pat( & self . node) ) ;
571
667
impl_with_search_pat ! ( ( _cx: LateContext <' tcx>, self : Path <' _>) => path_search_pat( self ) ) ;
668
+ impl_with_search_pat ! ( ( cx: LateContext <' tcx>, self : rustc_hir:: Pat <' _>) => pat_search_pat( cx. tcx, self ) ) ;
572
669
573
670
impl_with_search_pat ! ( ( _cx: EarlyContext <' tcx>, self : Attribute ) => attr_search_pat( self ) ) ;
574
671
impl_with_search_pat ! ( ( _cx: EarlyContext <' tcx>, self : ast:: Ty ) => ast_ty_search_pat( self ) ) ;
0 commit comments