@@ -333,19 +333,21 @@ impl<'a> FmtVisitor<'a> {
333
333
newline_brace = false ;
334
334
}
335
335
336
- // Prepare for the function body by possibly adding a newline and
337
- // indent.
338
- // FIXME we'll miss anything between the end of the signature and the
339
- // start of the body, but we need more spans from the compiler to solve
340
- // this.
341
- if newline_brace {
342
- result. push_str ( & indent. to_string_with_newline ( self . config ) ) ;
336
+ if let rw @ Some ( ..) = self . single_line_fn ( & result, block, inner_attrs) {
337
+ rw
343
338
} else {
344
- result. push ( ' ' ) ;
339
+ // Prepare for the function body by possibly adding a newline and
340
+ // indent.
341
+ // FIXME we'll miss anything between the end of the signature and the
342
+ // start of the body, but we need more spans from the compiler to solve
343
+ // this.
344
+ if newline_brace {
345
+ result. push_str ( & indent. to_string_with_newline ( self . config ) ) ;
346
+ } else {
347
+ result. push ( ' ' ) ;
348
+ }
349
+ Some ( result)
345
350
}
346
-
347
- self . single_line_fn ( & result, block, inner_attrs)
348
- . or_else ( || Some ( result) )
349
351
}
350
352
351
353
pub fn rewrite_required_fn (
@@ -390,42 +392,37 @@ impl<'a> FmtVisitor<'a> {
390
392
391
393
if self . config . empty_item_single_line ( )
392
394
&& is_empty_block ( block, None , source_map)
393
- && self . block_indent . width ( ) + fn_str. len ( ) + 2 <= self . config . max_width ( )
395
+ && self . block_indent . width ( ) + fn_str. len ( ) + 3 <= self . config . max_width ( )
396
+ && !last_line_contains_single_line_comment ( fn_str)
394
397
{
395
- return Some ( format ! ( "{}{{}}" , fn_str) ) ;
396
- }
397
-
398
- if self . config . fn_single_line ( ) && is_simple_block_stmt ( block, None , source_map) {
399
- let rewrite = {
400
- if let Some ( stmt) = block. stmts . first ( ) {
401
- match stmt_expr ( stmt) {
402
- Some ( e) => {
403
- let suffix = if semicolon_for_expr ( & self . get_context ( ) , e) {
404
- ";"
405
- } else {
406
- ""
407
- } ;
408
-
409
- format_expr ( e, ExprType :: Statement , & self . get_context ( ) , self . shape ( ) )
410
- . map ( |s| s + suffix)
411
- . or_else ( || Some ( self . snippet ( e. span ) . to_owned ( ) ) )
412
- }
413
- None => stmt. rewrite ( & self . get_context ( ) , self . shape ( ) ) ,
414
- }
398
+ return Some ( format ! ( "{} {{}}" , fn_str) ) ;
399
+ }
400
+
401
+ if !self . config . fn_single_line ( ) || !is_simple_block_stmt ( block, None , source_map) {
402
+ return None ;
403
+ }
404
+
405
+ let stmt = block. stmts . first ( ) ?;
406
+ let res = match stmt_expr ( stmt) {
407
+ Some ( e) => {
408
+ let suffix = if semicolon_for_expr ( & self . get_context ( ) , e) {
409
+ ";"
415
410
} else {
416
- None
417
- }
418
- } ;
411
+ ""
412
+ } ;
419
413
420
- if let Some ( res) = rewrite {
421
- let width = self . block_indent . width ( ) + fn_str. len ( ) + res. len ( ) + 4 ;
422
- if !res. contains ( '\n' ) && width <= self . config . max_width ( ) {
423
- return Some ( format ! ( "{}{{ {} }}" , fn_str, res) ) ;
424
- }
414
+ format_expr ( e, ExprType :: Statement , & self . get_context ( ) , self . shape ( ) )
415
+ . map ( |s| s + suffix) ?
425
416
}
426
- }
417
+ None => stmt. rewrite ( & self . get_context ( ) , self . shape ( ) ) ?,
418
+ } ;
427
419
428
- None
420
+ let width = self . block_indent . width ( ) + fn_str. len ( ) + res. len ( ) + 5 ;
421
+ if !res. contains ( '\n' ) && width <= self . config . max_width ( ) {
422
+ Some ( format ! ( "{} {{ {} }}" , fn_str, res) )
423
+ } else {
424
+ None
425
+ }
429
426
}
430
427
431
428
pub fn visit_static ( & mut self , static_parts : & StaticParts ) {
0 commit comments