@@ -55,6 +55,17 @@ fn is_short_pattern_inner(pat: &ast::Pat) -> bool {
5555 }
5656}
5757
58+ struct RangeOperand < ' a > ( & ' a Option < ptr:: P < ast:: Expr > > ) ;
59+
60+ impl < ' a > Rewrite for RangeOperand < ' a > {
61+ fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
62+ match & self . 0 {
63+ None => Some ( "" . to_owned ( ) ) ,
64+ Some ( ref exp) => exp. rewrite ( context, shape) ,
65+ }
66+ }
67+ }
68+
5869impl Rewrite for Pat {
5970 fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
6071 match self . kind {
@@ -179,29 +190,34 @@ impl Rewrite for Pat {
179190 None
180191 }
181192 }
182- PatKind :: Range ( ref lhs, ref rhs, ref end_kind) => match ( lhs, rhs) {
183- ( Some ( lhs) , Some ( rhs) ) => {
184- let infix = match end_kind. node {
185- RangeEnd :: Included ( RangeSyntax :: DotDotDot ) => "..." ,
186- RangeEnd :: Included ( RangeSyntax :: DotDotEq ) => "..=" ,
187- RangeEnd :: Excluded => ".." ,
193+ PatKind :: Range ( ref lhs, ref rhs, ref end_kind) => {
194+ let infix = match end_kind. node {
195+ RangeEnd :: Included ( RangeSyntax :: DotDotDot ) => "..." ,
196+ RangeEnd :: Included ( RangeSyntax :: DotDotEq ) => "..=" ,
197+ RangeEnd :: Excluded => ".." ,
198+ } ;
199+ let infix = if context. config . spaces_around_ranges ( ) {
200+ let lhs_spacing = match lhs {
201+ None => "" ,
202+ Some ( _) => " " ,
188203 } ;
189- let infix = if context. config . spaces_around_ranges ( ) {
190- format ! ( " {} " , infix)
191- } else {
192- infix. to_owned ( )
204+ let rhs_spacing = match rhs {
205+ None => "" ,
206+ Some ( _) => " " ,
193207 } ;
194- rewrite_pair (
195- & * * lhs,
196- & * * rhs,
197- PairParts :: infix ( & infix) ,
198- context,
199- shape,
200- SeparatorPlace :: Front ,
201- )
202- }
203- ( _, _) => unimplemented ! ( ) ,
204- } ,
208+ format ! ( "{}{}{}" , lhs_spacing, infix, rhs_spacing)
209+ } else {
210+ infix. to_owned ( )
211+ } ;
212+ rewrite_pair (
213+ & RangeOperand ( lhs) ,
214+ & RangeOperand ( rhs) ,
215+ PairParts :: infix ( & infix) ,
216+ context,
217+ shape,
218+ SeparatorPlace :: Front ,
219+ )
220+ }
205221 PatKind :: Ref ( ref pat, mutability) => {
206222 let prefix = format ! ( "&{}" , format_mutability( mutability) ) ;
207223 rewrite_unary_prefix ( context, & prefix, & * * pat, shape)
0 commit comments