@@ -66,9 +66,7 @@ impl hir::Pat {
6666
6767 /// Call `f` on every "binding" in a pattern, e.g., on `a` in
6868 /// `match foo() { Some(a) => (), None => () }`
69- pub fn each_binding < F > ( & self , mut f : F )
70- where F : FnMut ( hir:: BindingAnnotation , HirId , Span , ast:: Ident ) ,
71- {
69+ pub fn each_binding ( & self , mut f : impl FnMut ( hir:: BindingAnnotation , HirId , Span , ast:: Ident ) ) {
7270 self . walk ( |p| {
7371 if let PatKind :: Binding ( binding_mode, _, ident, _) = p. node {
7472 f ( binding_mode, p. hir_id , p. span , ident) ;
@@ -81,59 +79,53 @@ impl hir::Pat {
8179 /// `match foo() { Some(a) => (), None => () }`.
8280 ///
8381 /// When encountering an or-pattern `p_0 | ... | p_n` only `p_0` will be visited.
84- pub fn each_binding_or_first < F > ( & self , c : & mut F )
85- where F : FnMut ( hir:: BindingAnnotation , HirId , Span , ast:: Ident ) ,
86- {
87- match & self . node {
88- PatKind :: Binding ( bm, _, ident, sub) => {
89- c ( * bm, self . hir_id , self . span , * ident) ;
90- sub. iter ( ) . for_each ( |p| p. each_binding_or_first ( c) ) ;
91- }
92- PatKind :: Or ( ps) => ps[ 0 ] . each_binding_or_first ( c) ,
93- PatKind :: Struct ( _, fs, _) => fs. iter ( ) . for_each ( |f| f. pat . each_binding_or_first ( c) ) ,
94- PatKind :: TupleStruct ( _, ps, _) | PatKind :: Tuple ( ps, _) => {
95- ps. iter ( ) . for_each ( |p| p. each_binding_or_first ( c) ) ;
96- }
97- PatKind :: Box ( p) | PatKind :: Ref ( p, _) => p. each_binding_or_first ( c) ,
98- PatKind :: Slice ( before, slice, after) => {
99- before. iter ( )
100- . chain ( slice. iter ( ) )
101- . chain ( after. iter ( ) )
102- . for_each ( |p| p. each_binding_or_first ( c) ) ;
82+ pub fn each_binding_or_first (
83+ & self ,
84+ f : & mut impl FnMut ( hir:: BindingAnnotation , HirId , Span , ast:: Ident ) ,
85+ ) {
86+ self . walk ( |p| match & p. node {
87+ PatKind :: Or ( ps) => {
88+ ps[ 0 ] . each_binding_or_first ( f) ;
89+ false
90+ } ,
91+ PatKind :: Binding ( bm, _, ident, _) => {
92+ f ( * bm, p. hir_id , p. span , * ident) ;
93+ true
10394 }
104- PatKind :: Wild | PatKind :: Lit ( _ ) | PatKind :: Range ( .. ) | PatKind :: Path ( _ ) => { }
105- }
95+ _ => true ,
96+ } )
10697 }
10798
10899 /// Checks if the pattern contains any patterns that bind something to
109100 /// an ident, e.g., `foo`, or `Foo(foo)` or `foo @ Bar(..)`.
110101 pub fn contains_bindings ( & self ) -> bool {
111- let mut contains_bindings = false ;
112- self . walk ( |p| {
113- if let PatKind :: Binding ( ..) = p. node {
114- contains_bindings = true ;
115- false // there's at least one binding, can short circuit now.
116- } else {
117- true
118- }
119- } ) ;
120- contains_bindings
102+ self . satisfies ( |p| match p. node {
103+ PatKind :: Binding ( ..) => true ,
104+ _ => false ,
105+ } )
121106 }
122107
123108 /// Checks if the pattern contains any patterns that bind something to
124109 /// an ident or wildcard, e.g., `foo`, or `Foo(_)`, `foo @ Bar(..)`,
125110 pub fn contains_bindings_or_wild ( & self ) -> bool {
126- let mut contains_bindings = false ;
127- self . walk ( |p| {
128- match p. node {
129- PatKind :: Binding ( ..) | PatKind :: Wild => {
130- contains_bindings = true ;
131- false // there's at least one binding/wildcard, can short circuit now.
132- }
133- _ => true
111+ self . satisfies ( |p| match p. node {
112+ PatKind :: Binding ( ..) | PatKind :: Wild => true ,
113+ _ => false ,
114+ } )
115+ }
116+
117+ /// Checks if the pattern satisfies the given predicate on some sub-pattern.
118+ fn satisfies ( & self , pred : impl Fn ( & Self ) -> bool ) -> bool {
119+ let mut satisfies = false ;
120+ self . walk_short ( |p| {
121+ if pred ( p) {
122+ satisfies = true ;
123+ false // Found one, can short circuit now.
124+ } else {
125+ true
134126 }
135127 } ) ;
136- contains_bindings
128+ satisfies
137129 }
138130
139131 pub fn simple_ident ( & self ) -> Option < ast:: Ident > {
@@ -147,20 +139,20 @@ impl hir::Pat {
147139 /// Returns variants that are necessary to exist for the pattern to match.
148140 pub fn necessary_variants ( & self ) -> Vec < DefId > {
149141 let mut variants = vec ! [ ] ;
150- self . walk ( |p| {
151- match p . node {
152- PatKind :: Path ( hir:: QPath :: Resolved ( _, ref path) ) |
153- PatKind :: TupleStruct ( hir:: QPath :: Resolved ( _, ref path) , ..) |
154- PatKind :: Struct ( hir:: QPath :: Resolved ( _, ref path) , ..) => {
155- match path . res {
156- Res :: Def ( DefKind :: Variant , id ) => variants . push ( id) ,
157- Res :: Def ( DefKind :: Ctor ( CtorOf :: Variant , .. ) , id ) => variants . push ( id ) ,
158- _ => ( )
159- }
142+ self . walk ( |p| match & p . node {
143+ PatKind :: Or ( _ ) => false ,
144+ PatKind :: Path ( hir:: QPath :: Resolved ( _, path) ) |
145+ PatKind :: TupleStruct ( hir:: QPath :: Resolved ( _, path) , ..) |
146+ PatKind :: Struct ( hir:: QPath :: Resolved ( _, path) , ..) => {
147+ if let Res :: Def ( DefKind :: Variant , id )
148+ | Res :: Def ( DefKind :: Ctor ( CtorOf :: Variant , .. ) , id)
149+ = path . res
150+ {
151+ variants . push ( id ) ;
160152 }
161- _ => ( )
153+ true
162154 }
163- true
155+ _ => true ,
164156 } ) ;
165157 variants. sort ( ) ;
166158 variants. dedup ( ) ;
@@ -176,14 +168,12 @@ impl hir::Pat {
176168 let mut result = None ;
177169 self . each_binding ( |annotation, _, _, _| {
178170 match annotation {
179- hir:: BindingAnnotation :: Ref => {
180- match result {
181- None | Some ( hir:: MutImmutable ) => result = Some ( hir:: MutImmutable ) ,
182- _ => ( ) ,
183- }
171+ hir:: BindingAnnotation :: Ref => match result {
172+ None | Some ( hir:: MutImmutable ) => result = Some ( hir:: MutImmutable ) ,
173+ _ => { }
184174 }
185175 hir:: BindingAnnotation :: RefMut => result = Some ( hir:: MutMutable ) ,
186- _ => ( ) ,
176+ _ => { }
187177 }
188178 } ) ;
189179 result
0 commit comments