@@ -42,79 +42,42 @@ impl<'hir> Entry<'hir> {
42
42
43
43
fn fn_decl < ' hir > ( node : Node < ' hir > ) -> Option < & ' hir FnDecl < ' hir > > {
44
44
match node {
45
- Node :: Item ( ref item) => match item. kind {
46
- ItemKind :: Fn ( ref sig, _, _) => Some ( & sig. decl ) ,
47
- _ => None ,
48
- } ,
49
-
50
- Node :: TraitItem ( ref item) => match item. kind {
51
- TraitItemKind :: Fn ( ref sig, _) => Some ( & sig. decl ) ,
52
- _ => None ,
53
- } ,
54
-
55
- Node :: ImplItem ( ref item) => match item. kind {
56
- ImplItemKind :: Fn ( ref sig, _) => Some ( & sig. decl ) ,
57
- _ => None ,
58
- } ,
59
-
60
- Node :: Expr ( ref expr) => match expr. kind {
61
- ExprKind :: Closure ( _, ref fn_decl, ..) => Some ( fn_decl) ,
62
- _ => None ,
63
- } ,
64
-
45
+ Node :: Item ( Item { kind : ItemKind :: Fn ( sig, _, _) , .. } )
46
+ | Node :: TraitItem ( TraitItem { kind : TraitItemKind :: Fn ( sig, _) , .. } )
47
+ | Node :: ImplItem ( ImplItem { kind : ImplItemKind :: Fn ( sig, _) , .. } ) => Some ( & sig. decl ) ,
48
+ Node :: Expr ( Expr { kind : ExprKind :: Closure ( _, fn_decl, ..) , .. } ) => Some ( fn_decl) ,
65
49
_ => None ,
66
50
}
67
51
}
68
52
69
53
fn fn_sig < ' hir > ( node : Node < ' hir > ) -> Option < & ' hir FnSig < ' hir > > {
70
54
match & node {
71
- Node :: Item ( item) => match & item. kind {
72
- ItemKind :: Fn ( sig, _, _) => Some ( sig) ,
73
- _ => None ,
74
- } ,
75
-
76
- Node :: TraitItem ( item) => match & item. kind {
77
- TraitItemKind :: Fn ( sig, _) => Some ( sig) ,
78
- _ => None ,
79
- } ,
80
-
81
- Node :: ImplItem ( item) => match & item. kind {
82
- ImplItemKind :: Fn ( sig, _) => Some ( sig) ,
83
- _ => None ,
84
- } ,
85
-
55
+ Node :: Item ( Item { kind : ItemKind :: Fn ( sig, _, _) , .. } )
56
+ | Node :: TraitItem ( TraitItem { kind : TraitItemKind :: Fn ( sig, _) , .. } )
57
+ | Node :: ImplItem ( ImplItem { kind : ImplItemKind :: Fn ( sig, _) , .. } ) => Some ( sig) ,
86
58
_ => None ,
87
59
}
88
60
}
89
61
90
62
fn associated_body < ' hir > ( node : Node < ' hir > ) -> Option < BodyId > {
91
63
match node {
92
- Node :: Item ( item) => match item. kind {
93
- ItemKind :: Const ( _, body) | ItemKind :: Static ( .., body) | ItemKind :: Fn ( .., body) => {
94
- Some ( body)
95
- }
96
- _ => None ,
97
- } ,
98
-
99
- Node :: TraitItem ( item) => match item. kind {
100
- TraitItemKind :: Const ( _, Some ( body) ) | TraitItemKind :: Fn ( _, TraitFn :: Provided ( body) ) => {
101
- Some ( body)
102
- }
103
- _ => None ,
104
- } ,
105
-
106
- Node :: ImplItem ( item) => match item. kind {
107
- ImplItemKind :: Const ( _, body) | ImplItemKind :: Fn ( _, body) => Some ( body) ,
108
- _ => None ,
109
- } ,
64
+ Node :: Item ( Item {
65
+ kind : ItemKind :: Const ( _, body) | ItemKind :: Static ( .., body) | ItemKind :: Fn ( .., body) ,
66
+ ..
67
+ } )
68
+ | Node :: TraitItem ( TraitItem {
69
+ kind :
70
+ TraitItemKind :: Const ( _, Some ( body) ) | TraitItemKind :: Fn ( _, TraitFn :: Provided ( body) ) ,
71
+ ..
72
+ } )
73
+ | Node :: ImplItem ( ImplItem {
74
+ kind : ImplItemKind :: Const ( _, body) | ImplItemKind :: Fn ( _, body) ,
75
+ ..
76
+ } )
77
+ | Node :: Expr ( Expr { kind : ExprKind :: Closure ( .., body, _, _) , .. } ) => Some ( * body) ,
110
78
111
79
Node :: AnonConst ( constant) => Some ( constant. body ) ,
112
80
113
- Node :: Expr ( expr) => match expr. kind {
114
- ExprKind :: Closure ( .., body, _, _) => Some ( body) ,
115
- _ => None ,
116
- } ,
117
-
118
81
_ => None ,
119
82
}
120
83
}
@@ -518,20 +481,21 @@ impl<'hir> Map<'hir> {
518
481
}
519
482
520
483
pub fn get_generics ( & self , id : DefId ) -> Option < & ' hir Generics < ' hir > > {
521
- self . get_if_local ( id) . and_then ( |node| match node {
522
- Node :: ImplItem ( ref impl_item) => Some ( & impl_item. generics ) ,
523
- Node :: TraitItem ( ref trait_item) => Some ( & trait_item. generics ) ,
524
- Node :: Item ( ref item) => match item. kind {
525
- ItemKind :: Fn ( _, ref generics, _)
526
- | ItemKind :: TyAlias ( _, ref generics)
527
- | ItemKind :: Enum ( _, ref generics)
528
- | ItemKind :: Struct ( _, ref generics)
529
- | ItemKind :: Union ( _, ref generics)
530
- | ItemKind :: Trait ( _, _, ref generics, ..)
531
- | ItemKind :: TraitAlias ( ref generics, _)
532
- | ItemKind :: Impl { ref generics, .. } => Some ( generics) ,
533
- _ => None ,
534
- } ,
484
+ self . get_if_local ( id) . and_then ( |node| match & node {
485
+ Node :: ImplItem ( impl_item) => Some ( & impl_item. generics ) ,
486
+ Node :: TraitItem ( trait_item) => Some ( & trait_item. generics ) ,
487
+ Node :: Item ( Item {
488
+ kind :
489
+ ItemKind :: Fn ( _, generics, _)
490
+ | ItemKind :: TyAlias ( _, generics)
491
+ | ItemKind :: Enum ( _, generics)
492
+ | ItemKind :: Struct ( _, generics)
493
+ | ItemKind :: Union ( _, generics)
494
+ | ItemKind :: Trait ( _, _, generics, ..)
495
+ | ItemKind :: TraitAlias ( generics, _)
496
+ | ItemKind :: Impl { generics, .. } ,
497
+ ..
498
+ } ) => Some ( generics) ,
535
499
_ => None ,
536
500
} )
537
501
}
@@ -571,11 +535,12 @@ impl<'hir> Map<'hir> {
571
535
_ => return false ,
572
536
}
573
537
match self . find ( self . get_parent_node ( id) ) {
574
- Some ( Node :: Item ( _) ) | Some ( Node :: TraitItem ( _) ) | Some ( Node :: ImplItem ( _) ) => true ,
575
- Some ( Node :: Expr ( e) ) => match e. kind {
576
- ExprKind :: Closure ( ..) => true ,
577
- _ => false ,
578
- } ,
538
+ Some (
539
+ Node :: Item ( _)
540
+ | Node :: TraitItem ( _)
541
+ | Node :: ImplItem ( _)
542
+ | Node :: Expr ( Expr { kind : ExprKind :: Closure ( ..) , .. } ) ,
543
+ ) => true ,
579
544
_ => false ,
580
545
}
581
546
}
@@ -642,12 +607,8 @@ impl<'hir> Map<'hir> {
642
607
if let ( Some ( ( _, next_node) ) , false ) = ( iter. peek ( ) , ignore_tail) {
643
608
match next_node {
644
609
Node :: Block ( Block { expr : None , .. } ) => return None ,
645
- Node :: Block ( Block { expr : Some ( expr) , .. } ) => {
646
- if hir_id != expr. hir_id {
647
- // The current node is not the tail expression of its parent.
648
- return None ;
649
- }
650
- }
610
+ // The current node is not the tail expression of its parent.
611
+ Node :: Block ( Block { expr : Some ( e) , .. } ) if hir_id != e. hir_id => return None ,
651
612
_ => { }
652
613
}
653
614
}
@@ -657,14 +618,11 @@ impl<'hir> Map<'hir> {
657
618
| Node :: TraitItem ( _)
658
619
| Node :: Expr ( Expr { kind : ExprKind :: Closure ( ..) , .. } )
659
620
| Node :: ImplItem ( _) => return Some ( hir_id) ,
660
- Node :: Expr ( ref expr) => {
661
- match expr. kind {
662
- // Ignore `return`s on the first iteration
663
- ExprKind :: Loop ( ..) | ExprKind :: Ret ( ..) => return None ,
664
- _ => { }
665
- }
621
+ // Ignore `return`s on the first iteration
622
+ Node :: Expr ( Expr { kind : ExprKind :: Loop ( ..) | ExprKind :: Ret ( ..) , .. } )
623
+ | Node :: Local ( _) => {
624
+ return None ;
666
625
}
667
- Node :: Local ( _) => return None ,
668
626
_ => { }
669
627
}
670
628
}
@@ -708,17 +666,12 @@ impl<'hir> Map<'hir> {
708
666
pub fn get_match_if_cause ( & self , hir_id : HirId ) -> Option < & ' hir Expr < ' hir > > {
709
667
for ( _, node) in self . parent_iter ( hir_id) {
710
668
match node {
711
- Node :: Item ( _) | Node :: ForeignItem ( _) | Node :: TraitItem ( _) | Node :: ImplItem ( _) => {
712
- break ;
713
- }
714
- Node :: Expr ( expr) => match expr. kind {
715
- ExprKind :: Match ( _, _, _) => return Some ( expr) ,
716
- _ => { }
717
- } ,
718
- Node :: Stmt ( stmt) => match stmt. kind {
719
- StmtKind :: Local ( _) => break ,
720
- _ => { }
721
- } ,
669
+ Node :: Item ( _)
670
+ | Node :: ForeignItem ( _)
671
+ | Node :: TraitItem ( _)
672
+ | Node :: ImplItem ( _)
673
+ | Node :: Stmt ( Stmt { kind : StmtKind :: Local ( _) , .. } ) => break ,
674
+ Node :: Expr ( expr @ Expr { kind : ExprKind :: Match ( ..) , .. } ) => return Some ( expr) ,
722
675
_ => { }
723
676
}
724
677
}
@@ -728,32 +681,22 @@ impl<'hir> Map<'hir> {
728
681
/// Returns the nearest enclosing scope. A scope is roughly an item or block.
729
682
pub fn get_enclosing_scope ( & self , hir_id : HirId ) -> Option < HirId > {
730
683
for ( hir_id, node) in self . parent_iter ( hir_id) {
731
- if match node {
732
- Node :: Item ( i ) => match i . kind {
684
+ if let Node :: Item ( Item {
685
+ kind :
733
686
ItemKind :: Fn ( ..)
734
687
| ItemKind :: Mod ( ..)
735
688
| ItemKind :: Enum ( ..)
736
689
| ItemKind :: Struct ( ..)
737
690
| ItemKind :: Union ( ..)
738
691
| ItemKind :: Trait ( ..)
739
- | ItemKind :: Impl { .. } => true ,
740
- _ => false ,
741
- } ,
742
- Node :: ForeignItem ( fi) => match fi. kind {
743
- ForeignItemKind :: Fn ( ..) => true ,
744
- _ => false ,
745
- } ,
746
- Node :: TraitItem ( ti) => match ti. kind {
747
- TraitItemKind :: Fn ( ..) => true ,
748
- _ => false ,
749
- } ,
750
- Node :: ImplItem ( ii) => match ii. kind {
751
- ImplItemKind :: Fn ( ..) => true ,
752
- _ => false ,
753
- } ,
754
- Node :: Block ( _) => true ,
755
- _ => false ,
756
- } {
692
+ | ItemKind :: Impl { .. } ,
693
+ ..
694
+ } )
695
+ | Node :: ForeignItem ( ForeignItem { kind : ForeignItemKind :: Fn ( ..) , .. } )
696
+ | Node :: TraitItem ( TraitItem { kind : TraitItemKind :: Fn ( ..) , .. } )
697
+ | Node :: ImplItem ( ImplItem { kind : ImplItemKind :: Fn ( ..) , .. } )
698
+ | Node :: Block ( _) = node
699
+ {
757
700
return Some ( hir_id) ;
758
701
}
759
702
}
@@ -769,11 +712,11 @@ impl<'hir> Map<'hir> {
769
712
return CRATE_HIR_ID ;
770
713
}
771
714
match self . get ( scope) {
772
- Node :: Item ( i ) => match i . kind {
773
- ItemKind :: OpaqueTy ( OpaqueTy { impl_trait_fn : None , .. } ) => { }
774
- _ => break ,
775
- } ,
776
- Node :: Block ( _) => { }
715
+ Node :: Item ( Item {
716
+ kind : ItemKind :: OpaqueTy ( OpaqueTy { impl_trait_fn : None , .. } ) ,
717
+ ..
718
+ } )
719
+ | Node :: Block ( _) => { }
777
720
_ => break ,
778
721
}
779
722
}
@@ -821,14 +764,11 @@ impl<'hir> Map<'hir> {
821
764
822
765
pub fn expect_variant_data ( & self , id : HirId ) -> & ' hir VariantData < ' hir > {
823
766
match self . find ( id) {
824
- Some ( Node :: Item ( i) ) => match i. kind {
825
- ItemKind :: Struct ( ref struct_def, _) | ItemKind :: Union ( ref struct_def, _) => {
826
- struct_def
827
- }
828
- _ => bug ! ( "struct ID bound to non-struct {}" , self . node_to_string( id) ) ,
829
- } ,
767
+ Some (
768
+ Node :: Ctor ( vd)
769
+ | Node :: Item ( Item { kind : ItemKind :: Struct ( vd, _) | ItemKind :: Union ( vd, _) , .. } ) ,
770
+ ) => vd,
830
771
Some ( Node :: Variant ( variant) ) => & variant. data ,
831
- Some ( Node :: Ctor ( data) ) => data,
832
772
_ => bug ! ( "expected struct or variant, found {}" , self . node_to_string( id) ) ,
833
773
}
834
774
}
0 commit comments