@@ -29,7 +29,7 @@ pub struct DeconstructedPat<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 < Cx : TypeCx > DeconstructedPat < Cx > {
@@ -112,34 +112,17 @@ impl<Cx: TypeCx> DeconstructedPat<Cx> {
112112 pub ( crate ) fn set_useful ( & self ) {
113113 self . useful . set ( true )
114114 }
115- pub ( crate ) fn is_useful ( & self ) -> bool {
116- if self . useful . get ( ) {
117- true
118- } else if self . is_or_pat ( ) && self . iter_fields ( ) . any ( |f| f. is_useful ( ) ) {
119- // We always expand or patterns in the matrix, so we will never see the actual
120- // or-pattern (the one with constructor `Or`) in the column. As such, it will not be
121- // marked as useful itself, only its children will. We recover this information here.
122- self . set_useful ( ) ;
123- true
124- } else {
125- false
115+
116+ /// Walk top-down and call `it` in each place where a pattern occurs
117+ /// starting with the root pattern `walk` is called on. If `it` returns
118+ /// false then we will descend no further but siblings will be processed.
119+ pub fn walk < ' a > ( & ' a self , it : & mut impl FnMut ( & ' a Self ) -> bool ) {
120+ if !it ( self ) {
121+ return ;
126122 }
127- }
128123
129- /// Report the subpatterns that were not useful, if any.
130- pub ( crate ) fn redundant_subpatterns ( & self ) -> Vec < & Self > {
131- let mut subpats = Vec :: new ( ) ;
132- self . collect_redundant_subpatterns ( & mut subpats) ;
133- subpats
134- }
135- fn collect_redundant_subpatterns < ' a > ( & ' a self , subpats : & mut Vec < & ' a Self > ) {
136- // We don't look at subpatterns if we already reported the whole pattern as redundant.
137- if !self . is_useful ( ) {
138- subpats. push ( self ) ;
139- } else {
140- for p in self . iter_fields ( ) {
141- p. collect_redundant_subpatterns ( subpats) ;
142- }
124+ for p in self . iter_fields ( ) {
125+ p. walk ( it)
143126 }
144127 }
145128}
0 commit comments