@@ -29,7 +29,7 @@ pub struct DeconstructedPat<'p, Cx: TypeCx> {
2929 /// correspond to a user-supplied pattern.
3030 data : Option < Cx :: PatData > ,
3131 /// Whether removing this arm would change the behavior of the match expression.
32- useful : Cell < bool > ,
32+ pub ( crate ) useful : Cell < bool > ,
3333}
3434
3535impl < ' p , Cx : TypeCx > DeconstructedPat < ' p , Cx > {
@@ -106,34 +106,17 @@ impl<'p, Cx: TypeCx> DeconstructedPat<'p, Cx> {
106106 pub ( crate ) fn set_useful ( & self ) {
107107 self . useful . set ( true )
108108 }
109- pub ( crate ) fn is_useful ( & self ) -> bool {
110- if self . useful . get ( ) {
111- true
112- } else if self . is_or_pat ( ) && self . iter_fields ( ) . any ( |f| f. is_useful ( ) ) {
113- // We always expand or patterns in the matrix, so we will never see the actual
114- // or-pattern (the one with constructor `Or`) in the column. As such, it will not be
115- // marked as useful itself, only its children will. We recover this information here.
116- self . set_useful ( ) ;
117- true
118- } else {
119- false
109+
110+ /// Walk top-down and call `it` in each place where a pattern occurs
111+ /// starting with the root pattern `walk` is called on. If `it` returns
112+ /// false then we will descend no further but siblings will be processed.
113+ pub fn walk ( & ' p self , it : & mut impl FnMut ( & ' p Self ) -> bool ) {
114+ if !it ( self ) {
115+ return ;
120116 }
121- }
122117
123- /// Report the subpatterns that were not useful, if any.
124- pub ( crate ) fn redundant_subpatterns ( & self ) -> Vec < & Self > {
125- let mut subpats = Vec :: new ( ) ;
126- self . collect_redundant_subpatterns ( & mut subpats) ;
127- subpats
128- }
129- fn collect_redundant_subpatterns < ' a > ( & ' a self , subpats : & mut Vec < & ' a Self > ) {
130- // We don't look at subpatterns if we already reported the whole pattern as redundant.
131- if !self . is_useful ( ) {
132- subpats. push ( self ) ;
133- } else {
134- for p in self . iter_fields ( ) {
135- p. collect_redundant_subpatterns ( subpats) ;
136- }
118+ for p in self . iter_fields ( ) {
119+ p. walk ( it)
137120 }
138121 }
139122}
0 commit comments