@@ -157,7 +157,11 @@ fn format_expr(expr: &ast::Expr,
157
157
} ;
158
158
159
159
if let Some ( ref expr) = * opt_expr {
160
- rewrite_unary_prefix ( context, & format ! ( "break{} " , id_str) , & * * expr, shape)
160
+ rewrite_unary_prefix ( context,
161
+ & format ! ( "break{} " , id_str) ,
162
+ & * * expr,
163
+ shape,
164
+ expr. span )
161
165
} else {
162
166
wrap_str ( format ! ( "break{}" , id_str) , context. config . max_width , shape)
163
167
}
@@ -178,9 +182,11 @@ fn format_expr(expr: &ast::Expr,
178
182
}
179
183
ast:: ExprKind :: Ret ( None ) => wrap_str ( "return" . to_owned ( ) , context. config . max_width , shape) ,
180
184
ast:: ExprKind :: Ret ( Some ( ref expr) ) => {
181
- rewrite_unary_prefix ( context, "return " , & * * expr, shape)
185
+ rewrite_unary_prefix ( context, "return " , & * * expr, shape, expr. span )
186
+ }
187
+ ast:: ExprKind :: Box ( ref expr) => {
188
+ rewrite_unary_prefix ( context, "box " , & * * expr, shape, expr. span )
182
189
}
183
- ast:: ExprKind :: Box ( ref expr) => rewrite_unary_prefix ( context, "box " , & * * expr, shape) ,
184
190
ast:: ExprKind :: AddrOf ( mutability, ref expr) => {
185
191
rewrite_expr_addrof ( context, mutability, expr, shape)
186
192
}
@@ -222,7 +228,7 @@ fn format_expr(expr: &ast::Expr,
222
228
} else {
223
229
delim. into ( )
224
230
} ;
225
- rewrite_unary_prefix ( context, & sp_delim, & * * rhs, shape)
231
+ rewrite_unary_prefix ( context, & sp_delim, & * * rhs, shape, expr . span )
226
232
}
227
233
( Some ( ref lhs) , None ) => {
228
234
let sp_delim = if context. config . spaces_around_ranges {
@@ -1224,10 +1230,10 @@ fn rewrite_match(context: &RewriteContext,
1224
1230
1225
1231
fn arm_start_pos ( arm : & ast:: Arm ) -> BytePos {
1226
1232
let & ast:: Arm {
1227
- ref attrs,
1228
- ref pats,
1229
- ..
1230
- } = arm;
1233
+ ref attrs,
1234
+ ref pats,
1235
+ ..
1236
+ } = arm;
1231
1237
if !attrs. is_empty ( ) {
1232
1238
return attrs[ 0 ] . span . lo ;
1233
1239
}
@@ -1258,11 +1264,11 @@ impl Rewrite for ast::Arm {
1258
1264
fn rewrite ( & self , context : & RewriteContext , shape : Shape ) -> Option < String > {
1259
1265
debug ! ( "Arm::rewrite {:?} {:?}" , self , shape) ;
1260
1266
let & ast:: Arm {
1261
- ref attrs,
1262
- ref pats,
1263
- ref guard,
1264
- ref body,
1265
- } = self ;
1267
+ ref attrs,
1268
+ ref pats,
1269
+ ref guard,
1270
+ ref body,
1271
+ } = self ;
1266
1272
1267
1273
// FIXME this is all a bit grotty, would be nice to abstract out the
1268
1274
// treatment of attributes.
@@ -1997,9 +2003,22 @@ pub fn rewrite_tuple<'a, I>(context: &RewriteContext,
1997
2003
pub fn rewrite_unary_prefix < R : Rewrite > ( context : & RewriteContext ,
1998
2004
prefix : & str ,
1999
2005
rewrite : & R ,
2000
- shape : Shape )
2006
+ mut shape : Shape ,
2007
+ span : Span )
2001
2008
-> Option < String > {
2002
- let shape = try_opt ! ( shape. shrink_left( prefix. len( ) ) ) . visual_indent ( 0 ) ;
2009
+ // Heuristic: if unary is `&` and `rewrite` contains `{`,
2010
+ // it is likely that block indent is preferred to visual indent.
2011
+ if prefix == "&" {
2012
+ let snippet = String :: from ( context. snippet ( span) . trim_left_matches ( '&' ) ) ;
2013
+ let first_line = try_opt ! ( snippet. lines( ) . nth( 0 ) ) ;
2014
+ if first_line. contains ( "{" ) {
2015
+ shape = try_opt ! ( shape. sub_width( prefix. len( ) ) ) . block_indent ( 0 ) ;
2016
+ } else {
2017
+ shape = try_opt ! ( shape. shrink_left( prefix. len( ) ) ) . visual_indent ( 0 ) ;
2018
+ }
2019
+ } else {
2020
+ shape = try_opt ! ( shape. shrink_left( prefix. len( ) ) ) . visual_indent ( 0 ) ;
2021
+ }
2003
2022
rewrite
2004
2023
. rewrite ( context, shape)
2005
2024
. map ( |r| format ! ( "{}{}" , prefix, r) )
@@ -2031,7 +2050,7 @@ fn rewrite_unary_op(context: &RewriteContext,
2031
2050
ast:: UnOp :: Not => "!" ,
2032
2051
ast:: UnOp :: Neg => "-" ,
2033
2052
} ;
2034
- rewrite_unary_prefix ( context, operator_str, expr, shape)
2053
+ rewrite_unary_prefix ( context, operator_str, expr, shape, expr . span )
2035
2054
}
2036
2055
2037
2056
fn rewrite_assignment ( context : & RewriteContext ,
@@ -2126,5 +2145,5 @@ fn rewrite_expr_addrof(context: &RewriteContext,
2126
2145
ast:: Mutability :: Immutable => "&" ,
2127
2146
ast:: Mutability :: Mutable => "&mut " ,
2128
2147
} ;
2129
- rewrite_unary_prefix ( context, operator_str, expr, shape)
2148
+ rewrite_unary_prefix ( context, operator_str, expr, shape, expr . span )
2130
2149
}
0 commit comments