@@ -237,36 +237,45 @@ 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+ pub ( crate ) fn for_each_immediate_subpat < ' a , ' tcx > (
245+ pat : & ' a Pat < ' tcx > ,
246+ mut callback : impl FnMut ( & ' a Pat < ' tcx > ) ,
247+ ) {
248+ match pat. kind {
249+ PatKind :: Wild
250+ | PatKind :: Binding { subpattern : None , .. }
251+ | PatKind :: Constant { value : _ }
252+ | PatKind :: Range ( _)
253+ | PatKind :: Never
254+ | PatKind :: Error ( _) => { }
255+
256+ PatKind :: AscribeUserType { ref subpattern, .. }
257+ | PatKind :: Binding { subpattern : Some ( ref subpattern) , .. }
258+ | PatKind :: Deref { ref subpattern }
259+ | PatKind :: DerefPattern { ref subpattern, .. }
260+ | PatKind :: ExpandedConstant { ref subpattern, .. } => callback ( subpattern) ,
261+
262+ PatKind :: Variant { ref subpatterns, .. } | PatKind :: Leaf { ref subpatterns } => {
263+ for field_pat in subpatterns {
264+ callback ( & field_pat. pattern ) ;
250265 }
251266 }
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) ;
267+
268+ PatKind :: Slice { ref prefix, ref slice, ref suffix }
269+ | PatKind :: Array { ref prefix, ref slice, ref suffix } => {
270+ for pat in prefix. iter ( ) . chain ( slice. as_deref ( ) ) . chain ( suffix) {
271+ callback ( pat) ;
264272 }
265273 }
266- Or { pats } => {
267- for pat in pats. iter ( ) {
268- visitor. visit_pat ( pat) ;
274+
275+ PatKind :: Or { ref pats } => {
276+ for pat in pats {
277+ callback ( pat) ;
269278 }
270279 }
271- } ;
280+ }
272281}
0 commit comments