@@ -107,18 +107,19 @@ impl Rewrite for Pat {
107107 }
108108 PatKind :: Box ( ref pat) => rewrite_unary_prefix ( context, "box " , & * * pat, shape) ,
109109 PatKind :: Ident ( BindingAnnotation ( by_ref, mutability) , ident, ref sub_pat) => {
110- let prefix = match by_ref {
111- ByRef :: Yes => "ref" ,
112- ByRef :: No => "" ,
110+ let mut_prefix = format_mutability ( mutability) . trim ( ) ;
111+
112+ let ( ref_kw, mut_infix) = match by_ref {
113+ ByRef :: Yes ( rmutbl) => ( "ref" , format_mutability ( rmutbl) . trim ( ) ) ,
114+ ByRef :: No => ( "" , "" ) ,
113115 } ;
114- let mut_infix = format_mutability ( mutability) . trim ( ) ;
115116 let id_str = rewrite_ident ( context, ident) ;
116117 let sub_pat = match * sub_pat {
117118 Some ( ref p) => {
118119 // 2 - `@ `.
119- let width = shape
120- . width
121- . checked_sub ( prefix . len ( ) + mut_infix . len ( ) + id_str . len ( ) + 2 ) ?;
120+ let width = shape. width . checked_sub (
121+ mut_prefix . len ( ) + ref_kw . len ( ) + mut_infix . len ( ) + id_str . len ( ) + 2 ,
122+ ) ?;
122123 let lo = context. snippet_provider . span_after ( self . span , "@" ) ;
123124 combine_strs_with_missing_comments (
124125 context,
@@ -132,33 +133,55 @@ impl Rewrite for Pat {
132133 None => "" . to_owned ( ) ,
133134 } ;
134135
135- // combine prefix and mut
136- let ( first_lo, first) = if !prefix. is_empty ( ) && !mut_infix. is_empty ( ) {
137- let hi = context. snippet_provider . span_before ( self . span , "mut" ) ;
138- let lo = context. snippet_provider . span_after ( self . span , "ref" ) ;
139- (
136+ // combine prefix and ref
137+ let ( first_lo, first) = match ( mut_prefix. is_empty ( ) , ref_kw. is_empty ( ) ) {
138+ ( false , false ) => {
139+ let lo = context. snippet_provider . span_after ( self . span , "mut" ) ;
140+ let hi = context. snippet_provider . span_before ( self . span , "ref" ) ;
141+ (
142+ context. snippet_provider . span_after ( self . span , "ref" ) ,
143+ combine_strs_with_missing_comments (
144+ context,
145+ mut_prefix,
146+ ref_kw,
147+ mk_sp ( lo, hi) ,
148+ shape,
149+ true ,
150+ ) ?,
151+ )
152+ }
153+ ( false , true ) => (
140154 context. snippet_provider . span_after ( self . span , "mut" ) ,
141- combine_strs_with_missing_comments (
142- context,
143- prefix,
144- mut_infix,
145- mk_sp ( lo, hi) ,
146- shape,
147- true ,
148- ) ?,
149- )
150- } else if !prefix. is_empty ( ) {
151- (
155+ mut_prefix. to_owned ( ) ,
156+ ) ,
157+ ( true , false ) => (
152158 context. snippet_provider . span_after ( self . span , "ref" ) ,
153- prefix. to_owned ( ) ,
154- )
155- } else if !mut_infix. is_empty ( ) {
156- (
157- context. snippet_provider . span_after ( self . span , "mut" ) ,
158- mut_infix. to_owned ( ) ,
159- )
160- } else {
161- ( self . span . lo ( ) , "" . to_owned ( ) )
159+ ref_kw. to_owned ( ) ,
160+ ) ,
161+ ( true , true ) => ( self . span . lo ( ) , "" . to_owned ( ) ) ,
162+ } ;
163+
164+ // combine result of above and mut
165+ let ( second_lo, second) = match ( first. is_empty ( ) , mut_infix. is_empty ( ) ) {
166+ ( false , false ) => {
167+ let lo = context. snippet_provider . span_after ( self . span , "ref" ) ;
168+ let end_span = mk_sp ( first_lo, self . span . hi ( ) ) ;
169+ let hi = context. snippet_provider . span_before ( end_span, "mut" ) ;
170+ (
171+ context. snippet_provider . span_after ( end_span, "mut" ) ,
172+ combine_strs_with_missing_comments (
173+ context,
174+ & first,
175+ mut_infix,
176+ mk_sp ( lo, hi) ,
177+ shape,
178+ true ,
179+ ) ?,
180+ )
181+ }
182+ ( false , true ) => ( first_lo, first) ,
183+ ( true , false ) => unreachable ! ( "mut_infix necessarily follows a ref" ) ,
184+ ( true , true ) => ( self . span . lo ( ) , "" . to_owned ( ) ) ,
162185 } ;
163186
164187 let next = if !sub_pat. is_empty ( ) {
@@ -177,9 +200,9 @@ impl Rewrite for Pat {
177200
178201 combine_strs_with_missing_comments (
179202 context,
180- & first ,
203+ & second ,
181204 & next,
182- mk_sp ( first_lo , ident. span . lo ( ) ) ,
205+ mk_sp ( second_lo , ident. span . lo ( ) ) ,
183206 shape,
184207 true ,
185208 )
0 commit comments