@@ -299,47 +299,28 @@ impl<'a> FmtVisitor<'a> {
299299 self . last_pos = item. span . hi ( ) ;
300300 }
301301
302- pub ( crate ) fn rewrite_fn (
302+ pub ( crate ) fn rewrite_fn_before_block (
303303 & mut self ,
304304 indent : Indent ,
305305 ident : ast:: Ident ,
306306 fn_sig : & FnSig < ' _ > ,
307307 span : Span ,
308- block : & ast:: Block ,
309- inner_attrs : Option < & [ ast:: Attribute ] > ,
310- ) -> Option < String > {
308+ ) -> Option < ( String , FnBraceStyle ) > {
311309 let context = self . get_context ( ) ;
312310
313- let mut newline_brace = newline_for_brace ( self . config , & fn_sig. generics . where_clause ) ;
314-
315- let ( mut result, force_newline_brace) =
316- rewrite_fn_base ( & context, indent, ident, fn_sig, span, newline_brace, true ) ?;
311+ let mut fn_brace_style = newline_for_brace ( self . config , & fn_sig. generics . where_clause ) ;
312+ let ( result, force_newline_brace) =
313+ rewrite_fn_base ( & context, indent, ident, fn_sig, span, fn_brace_style) ?;
317314
318315 // 2 = ` {`
319316 if self . config . brace_style ( ) == BraceStyle :: AlwaysNextLine
320317 || force_newline_brace
321318 || last_line_width ( & result) + 2 > self . shape ( ) . width
322319 {
323- newline_brace = true ;
324- } else if !result. contains ( '\n' ) {
325- newline_brace = false ;
320+ fn_brace_style = FnBraceStyle :: NextLine
326321 }
327322
328- if let rw @ Some ( ..) = self . single_line_fn ( & result, block, inner_attrs) {
329- rw
330- } else {
331- // Prepare for the function body by possibly adding a newline and
332- // indent.
333- // FIXME we'll miss anything between the end of the signature and the
334- // start of the body, but we need more spans from the compiler to solve
335- // this.
336- if newline_brace {
337- result. push_str ( & indent. to_string_with_newline ( self . config ) ) ;
338- } else {
339- result. push ( ' ' ) ;
340- }
341- Some ( result)
342- }
323+ Some ( ( result, fn_brace_style) )
343324 }
344325
345326 pub ( crate ) fn rewrite_required_fn (
@@ -360,8 +341,7 @@ impl<'a> FmtVisitor<'a> {
360341 ident,
361342 & FnSig :: from_method_sig ( sig, generics) ,
362343 span,
363- false ,
364- false ,
344+ FnBraceStyle :: None ,
365345 ) ?;
366346
367347 // Re-attach semicolon
@@ -370,7 +350,7 @@ impl<'a> FmtVisitor<'a> {
370350 Some ( result)
371351 }
372352
373- fn single_line_fn (
353+ pub ( crate ) fn single_line_fn (
374354 & self ,
375355 fn_str : & str ,
376356 block : & ast:: Block ,
@@ -1980,15 +1960,21 @@ pub(crate) fn is_named_arg(arg: &ast::Arg) -> bool {
19801960 }
19811961}
19821962
1963+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
1964+ pub ( crate ) enum FnBraceStyle {
1965+ SameLine ,
1966+ NextLine ,
1967+ None ,
1968+ }
1969+
19831970// Return type is (result, force_new_line_for_brace)
19841971fn rewrite_fn_base (
19851972 context : & RewriteContext < ' _ > ,
19861973 indent : Indent ,
19871974 ident : ast:: Ident ,
19881975 fn_sig : & FnSig < ' _ > ,
19891976 span : Span ,
1990- newline_brace : bool ,
1991- has_body : bool ,
1977+ fn_brace_style : FnBraceStyle ,
19921978) -> Option < ( String , bool ) > {
19931979 let mut force_new_line_for_brace = false ;
19941980
@@ -2001,7 +1987,7 @@ fn rewrite_fn_base(
20011987 result. push_str ( "fn " ) ;
20021988
20031989 // Generics.
2004- let overhead = if has_body && !newline_brace {
1990+ let overhead = if let FnBraceStyle :: SameLine = fn_brace_style {
20051991 // 4 = `() {`
20061992 4
20071993 } else {
@@ -2044,8 +2030,7 @@ fn rewrite_fn_base(
20442030 & result,
20452031 indent,
20462032 ret_str_len,
2047- newline_brace,
2048- has_body,
2033+ fn_brace_style,
20492034 multi_line_ret_str,
20502035 ) ?;
20512036
@@ -2237,7 +2222,7 @@ fn rewrite_fn_base(
22372222 } else {
22382223 WhereClauseSpace :: Newline
22392224 } ;
2240- let mut option = WhereClauseOption :: new ( !has_body , space) ;
2225+ let mut option = WhereClauseOption :: new ( fn_brace_style == FnBraceStyle :: None , space) ;
22412226 if is_args_multi_lined {
22422227 option. veto_single_line ( ) ;
22432228 }
@@ -2412,30 +2397,25 @@ fn compute_budgets_for_args(
24122397 result : & str ,
24132398 indent : Indent ,
24142399 ret_str_len : usize ,
2415- newline_brace : bool ,
2416- has_braces : bool ,
2400+ fn_brace_style : FnBraceStyle ,
24172401 force_vertical_layout : bool ,
24182402) -> Option < ( ( usize , usize , Indent ) ) > {
24192403 debug ! (
2420- "compute_budgets_for_args {} {:?}, {}, {}" ,
2404+ "compute_budgets_for_args {} {:?}, {}, {:? }" ,
24212405 result. len( ) ,
24222406 indent,
24232407 ret_str_len,
2424- newline_brace
2408+ fn_brace_style ,
24252409 ) ;
24262410 // Try keeping everything on the same line.
24272411 if !result. contains ( '\n' ) && !force_vertical_layout {
24282412 // 2 = `()`, 3 = `() `, space is before ret_string.
24292413 let overhead = if ret_str_len == 0 { 2 } else { 3 } ;
24302414 let mut used_space = indent. width ( ) + result. len ( ) + ret_str_len + overhead;
2431- if has_braces {
2432- if !newline_brace {
2433- // 2 = `{}`
2434- used_space += 2 ;
2435- }
2436- } else {
2437- // 1 = `;`
2438- used_space += 1 ;
2415+ match fn_brace_style {
2416+ FnBraceStyle :: None => used_space += 1 , // 1 = `;`
2417+ FnBraceStyle :: SameLine => used_space += 2 , // 2 = `{}`
2418+ FnBraceStyle :: NextLine => ( ) ,
24392419 }
24402420 let one_line_budget = context. budget ( used_space) ;
24412421
@@ -2448,7 +2428,10 @@ fn compute_budgets_for_args(
24482428 }
24492429 IndentStyle :: Visual => {
24502430 let indent = indent + result. len ( ) + 1 ;
2451- let multi_line_overhead = indent. width ( ) + if newline_brace { 2 } else { 4 } ;
2431+ let multi_line_overhead = match fn_brace_style {
2432+ FnBraceStyle :: SameLine => 4 ,
2433+ _ => 2 ,
2434+ } + indent. width ( ) ;
24522435 ( indent, context. budget ( multi_line_overhead) )
24532436 }
24542437 } ;
@@ -2468,16 +2451,21 @@ fn compute_budgets_for_args(
24682451 Some ( ( 0 , context. budget ( used_space) , new_indent) )
24692452}
24702453
2471- fn newline_for_brace ( config : & Config , where_clause : & ast:: WhereClause ) -> bool {
2454+ fn newline_for_brace ( config : & Config , where_clause : & ast:: WhereClause ) -> FnBraceStyle {
24722455 let predicate_count = where_clause. predicates . len ( ) ;
24732456
24742457 if config. where_single_line ( ) && predicate_count == 1 {
2475- return false ;
2458+ return FnBraceStyle :: SameLine ;
24762459 }
24772460 let brace_style = config. brace_style ( ) ;
24782461
2479- brace_style == BraceStyle :: AlwaysNextLine
2480- || ( brace_style == BraceStyle :: SameLineWhere && predicate_count > 0 )
2462+ let use_next_line = brace_style == BraceStyle :: AlwaysNextLine
2463+ || ( brace_style == BraceStyle :: SameLineWhere && predicate_count > 0 ) ;
2464+ if use_next_line {
2465+ FnBraceStyle :: NextLine
2466+ } else {
2467+ FnBraceStyle :: SameLine
2468+ }
24812469}
24822470
24832471fn rewrite_generics (
@@ -2919,8 +2907,7 @@ impl Rewrite for ast::ForeignItem {
29192907 self . ident ,
29202908 & FnSig :: new ( fn_decl, generics, self . vis . clone ( ) ) ,
29212909 span,
2922- false ,
2923- false ,
2910+ FnBraceStyle :: None ,
29242911 )
29252912 . map ( |( s, _) | format ! ( "{};" , s) ) ,
29262913 ast:: ForeignItemKind :: Static ( ref ty, mutability) => {
0 commit comments