@@ -473,7 +473,7 @@ fn rewrite_bounded_lifetime(
473
473
"{}{}{}" ,
474
474
result,
475
475
colon,
476
- join_bounds( context, shape. sub_width( overhead) ?, bounds, true , false ) ?
476
+ join_bounds( context, shape. sub_width( overhead) ?, bounds, true ) ?
477
477
) ;
478
478
Some ( result)
479
479
}
@@ -489,13 +489,15 @@ impl Rewrite for ast::GenericBound {
489
489
fn rewrite ( & self , context : & RewriteContext , shape : Shape ) -> Option < String > {
490
490
match * self {
491
491
ast:: GenericBound :: Trait ( ref poly_trait_ref, trait_bound_modifier) => {
492
- match trait_bound_modifier {
492
+ let snippet = context. snippet ( self . span ( ) ) ;
493
+ let has_paren = snippet. starts_with ( "(" ) && snippet. ends_with ( ")" ) ;
494
+ let rewrite = match trait_bound_modifier {
493
495
ast:: TraitBoundModifier :: None => poly_trait_ref. rewrite ( context, shape) ,
494
- ast:: TraitBoundModifier :: Maybe => {
495
- let rw = poly_trait_ref . rewrite ( context, shape. offset_left ( 1 ) ?) ? ;
496
- Some ( format ! ( "?{}" , rw ) )
497
- }
498
- }
496
+ ast:: TraitBoundModifier :: Maybe => poly_trait_ref
497
+ . rewrite ( context, shape. offset_left ( 1 ) ?)
498
+ . map ( |s| format ! ( "?{}" , s ) ) ,
499
+ } ;
500
+ rewrite . map ( |s| if has_paren { format ! ( "({})" , s ) } else { s } )
499
501
}
500
502
ast:: GenericBound :: Outlives ( ref lifetime) => lifetime. rewrite ( context, shape) ,
501
503
}
@@ -508,14 +510,7 @@ impl Rewrite for ast::GenericBounds {
508
510
return Some ( String :: new ( ) ) ;
509
511
}
510
512
511
- let span = mk_sp ( self . get ( 0 ) ?. span ( ) . lo ( ) , self . last ( ) ?. span ( ) . hi ( ) ) ;
512
- let has_paren = context. snippet ( span) . starts_with ( '(' ) ;
513
- let bounds_shape = if has_paren {
514
- shape. offset_left ( 1 ) ?. sub_width ( 1 ) ?
515
- } else {
516
- shape
517
- } ;
518
- join_bounds ( context, bounds_shape, self , true , has_paren)
513
+ join_bounds ( context, shape, self , true )
519
514
}
520
515
}
521
516
@@ -751,7 +746,6 @@ fn join_bounds(
751
746
shape : Shape ,
752
747
items : & [ ast:: GenericBound ] ,
753
748
need_indent : bool ,
754
- has_paren : bool ,
755
749
) -> Option < String > {
756
750
debug_assert ! ( !items. is_empty( ) ) ;
757
751
@@ -762,36 +756,9 @@ fn join_bounds(
762
756
} ;
763
757
let type_strs = items
764
758
. iter ( )
765
- . map ( |item| {
766
- item. rewrite (
767
- context,
768
- if has_paren {
769
- shape. sub_width ( 1 ) ?. offset_left ( 1 ) ?
770
- } else {
771
- shape
772
- } ,
773
- )
774
- } )
759
+ . map ( |item| item. rewrite ( context, shape) )
775
760
. collect :: < Option < Vec < _ > > > ( ) ?;
776
- let mut result = String :: with_capacity ( 128 ) ;
777
- let mut closing_paren = has_paren;
778
- if has_paren {
779
- result. push ( '(' ) ;
780
- }
781
- result. push_str ( & type_strs[ 0 ] ) ;
782
- if has_paren && type_strs. len ( ) == 1 {
783
- result. push ( ')' ) ;
784
- }
785
- for ( i, type_str) in type_strs[ 1 ..] . iter ( ) . enumerate ( ) {
786
- if closing_paren {
787
- if let ast:: GenericBound :: Outlives ( ..) = items[ i + 1 ] {
788
- result. push ( ')' ) ;
789
- closing_paren = false ;
790
- }
791
- }
792
- result. push_str ( joiner) ;
793
- result. push_str ( type_str) ;
794
- }
761
+ let result = type_strs. join ( joiner) ;
795
762
if items. len ( ) <= 1 || ( !result. contains ( '\n' ) && result. len ( ) <= shape. width ) {
796
763
return Some ( result) ;
797
764
}
@@ -814,20 +781,10 @@ fn join_bounds(
814
781
ast:: GenericBound :: Trait ( ..) => last_line_extendable ( s) ,
815
782
} ;
816
783
let mut result = String :: with_capacity ( 128 ) ;
817
- let mut closing_paren = has_paren;
818
- if has_paren {
819
- result. push ( '(' ) ;
820
- }
821
784
result. push_str ( & type_strs[ 0 ] ) ;
822
785
let mut can_be_put_on_the_same_line = is_bound_extendable ( & result, & items[ 0 ] ) ;
823
786
let generic_bounds_in_order = is_generic_bounds_in_order ( items) ;
824
787
for ( bound, bound_str) in items[ 1 ..] . iter ( ) . zip ( type_strs[ 1 ..] . iter ( ) ) {
825
- if closing_paren {
826
- if let ast:: GenericBound :: Outlives ( ..) = bound {
827
- closing_paren = false ;
828
- result. push ( ')' ) ;
829
- }
830
- }
831
788
if generic_bounds_in_order && can_be_put_on_the_same_line {
832
789
result. push_str ( joiner) ;
833
790
} else {
0 commit comments