@@ -23,6 +23,7 @@ use crate::macros::{MacroPosition, rewrite_macro};
2323use crate :: matches:: rewrite_match;
2424use crate :: overflow:: { self , IntoOverflowableItem , OverflowableItem } ;
2525use crate :: pairs:: { PairParts , rewrite_all_pairs, rewrite_pair} ;
26+ use crate :: range:: rewrite_range;
2627use crate :: rewrite:: { Rewrite , RewriteContext , RewriteError , RewriteErrorExt , RewriteResult } ;
2728use crate :: shape:: { Indent , Shape } ;
2829use crate :: source_map:: { LineRangeUtils , SpanUtils } ;
@@ -315,78 +316,13 @@ pub(crate) fn format_expr(
315316 shape,
316317 SeparatorPlace :: Back ,
317318 ) ,
318- ast:: ExprKind :: Range ( ref lhs, ref rhs, limits) => {
319- let delim = match limits {
320- ast:: RangeLimits :: HalfOpen => ".." ,
321- ast:: RangeLimits :: Closed => "..=" ,
322- } ;
323-
324- fn needs_space_before_range ( context : & RewriteContext < ' _ > , lhs : & ast:: Expr ) -> bool {
325- match lhs. kind {
326- ast:: ExprKind :: Lit ( token_lit) => lit_ends_in_dot ( & token_lit, context) ,
327- ast:: ExprKind :: Unary ( _, ref expr) => needs_space_before_range ( context, expr) ,
328- ast:: ExprKind :: Binary ( _, _, ref rhs_expr) => {
329- needs_space_before_range ( context, rhs_expr)
330- }
331- _ => false ,
332- }
333- }
334-
335- fn needs_space_after_range ( rhs : & ast:: Expr ) -> bool {
336- // Don't format `.. ..` into `....`, which is invalid.
337- //
338- // This check is unnecessary for `lhs`, because a range
339- // starting from another range needs parentheses as `(x ..) ..`
340- // (`x .. ..` is a range from `x` to `..`).
341- matches ! ( rhs. kind, ast:: ExprKind :: Range ( None , _, _) )
342- }
343-
344- let default_sp_delim = |lhs : Option < & ast:: Expr > , rhs : Option < & ast:: Expr > | {
345- let space_if = |b : bool | if b { " " } else { "" } ;
346-
347- format ! (
348- "{}{}{}" ,
349- lhs. map_or( "" , |lhs| space_if( needs_space_before_range( context, lhs) ) ) ,
350- delim,
351- rhs. map_or( "" , |rhs| space_if( needs_space_after_range( rhs) ) ) ,
352- )
353- } ;
354-
355- match ( lhs. as_ref ( ) . map ( |x| & * * x) , rhs. as_ref ( ) . map ( |x| & * * x) ) {
356- ( Some ( lhs) , Some ( rhs) ) => {
357- let sp_delim = if context. config . spaces_around_ranges ( ) {
358- format ! ( " {delim} " )
359- } else {
360- default_sp_delim ( Some ( lhs) , Some ( rhs) )
361- } ;
362- rewrite_pair (
363- & * lhs,
364- & * rhs,
365- PairParts :: infix ( & sp_delim) ,
366- context,
367- shape,
368- context. config . binop_separator ( ) ,
369- )
370- }
371- ( None , Some ( rhs) ) => {
372- let sp_delim = if context. config . spaces_around_ranges ( ) {
373- format ! ( "{delim} " )
374- } else {
375- default_sp_delim ( None , Some ( rhs) )
376- } ;
377- rewrite_unary_prefix ( context, & sp_delim, & * rhs, shape)
378- }
379- ( Some ( lhs) , None ) => {
380- let sp_delim = if context. config . spaces_around_ranges ( ) {
381- format ! ( " {delim}" )
382- } else {
383- default_sp_delim ( Some ( lhs) , None )
384- } ;
385- rewrite_unary_suffix ( context, & sp_delim, & * lhs, shape)
386- }
387- ( None , None ) => Ok ( delim. to_owned ( ) ) ,
388- }
389- }
319+ ast:: ExprKind :: Range ( ref lhs, ref rhs, limits) => rewrite_range (
320+ context,
321+ shape,
322+ lhs. as_deref ( ) ,
323+ rhs. as_deref ( ) ,
324+ limits. as_str ( ) ,
325+ ) ,
390326 // We do not format these expressions yet, but they should still
391327 // satisfy our width restrictions.
392328 // Style Guide RFC for InlineAsm variant pending
0 commit comments