@@ -237,36 +237,46 @@ pub fn walk_pat<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
237237 visitor : & mut V ,
238238 pat : & ' thir Pat < ' tcx > ,
239239) {
240- use PatKind :: * ;
241- match & pat. kind {
242- AscribeUserType { subpattern, ascription : _ }
243- | Deref { subpattern }
244- | DerefPattern { subpattern, .. }
245- | Binding { subpattern : Some ( subpattern) , .. } => visitor. visit_pat ( subpattern) ,
246- Binding { .. } | Wild | Never | Error ( _) => { }
247- Variant { subpatterns, adt_def : _, args : _, variant_index : _ } | Leaf { subpatterns } => {
248- for subpattern in subpatterns {
249- visitor. visit_pat ( & subpattern. pattern ) ;
240+ for_each_immediate_subpat ( pat, |p| visitor. visit_pat ( p) ) ;
241+ }
242+
243+ /// Invokes `callback` on each immediate subpattern of `pat`, if any.
244+ /// A building block for assembling THIR pattern visitors.
245+ pub ( crate ) fn for_each_immediate_subpat < ' a , ' tcx > (
246+ pat : & ' a Pat < ' tcx > ,
247+ mut callback : impl FnMut ( & ' a Pat < ' tcx > ) ,
248+ ) {
249+ match pat. kind {
250+ PatKind :: Wild
251+ | PatKind :: Binding { subpattern : None , .. }
252+ | PatKind :: Constant { value : _ }
253+ | PatKind :: Range ( _)
254+ | PatKind :: Never
255+ | PatKind :: Error ( _) => { }
256+
257+ PatKind :: AscribeUserType { ref subpattern, .. }
258+ | PatKind :: Binding { subpattern : Some ( ref subpattern) , .. }
259+ | PatKind :: Deref { ref subpattern }
260+ | PatKind :: DerefPattern { ref subpattern, .. }
261+ | PatKind :: ExpandedConstant { ref subpattern, .. } => callback ( subpattern) ,
262+
263+ PatKind :: Variant { ref subpatterns, .. } | PatKind :: Leaf { ref subpatterns } => {
264+ for field_pat in subpatterns {
265+ callback ( & field_pat. pattern ) ;
250266 }
251267 }
252- Constant { value : _ } => { }
253- ExpandedConstant { def_id : _, is_inline : _, subpattern } => visitor. visit_pat ( subpattern) ,
254- Range ( _) => { }
255- Slice { prefix, slice, suffix } | Array { prefix, slice, suffix } => {
256- for subpattern in prefix. iter ( ) {
257- visitor. visit_pat ( subpattern) ;
258- }
259- if let Some ( pat) = slice {
260- visitor. visit_pat ( pat) ;
261- }
262- for subpattern in suffix. iter ( ) {
263- visitor. visit_pat ( subpattern) ;
268+
269+ PatKind :: Slice { ref prefix, ref slice, ref suffix }
270+ | PatKind :: Array { ref prefix, ref slice, ref suffix } => {
271+ for pat in prefix. iter ( ) . chain ( slice. as_deref ( ) ) . chain ( suffix) {
272+ callback ( pat) ;
264273 }
265274 }
266- Or { pats } => {
267- for pat in pats. iter ( ) {
268- visitor. visit_pat ( pat) ;
275+
276+ PatKind :: Or { ref pats } => {
277+ for pat in pats {
278+ callback ( pat) ;
269279 }
270280 }
271- } ;
281+ }
272282}
0 commit comments