@@ -31,9 +31,10 @@ use shape::{Indent, Shape};
31
31
use spanned:: Spanned ;
32
32
use string:: { rewrite_string, StringFormat } ;
33
33
use types:: { can_be_overflowed_type, rewrite_path, PathContext } ;
34
- use utils:: { colon_spaces, contains_skip, extra_offset, first_line_width, inner_attributes,
35
- last_line_extendable, last_line_width, mk_sp, outer_attributes, paren_overhead,
36
- ptr_vec_to_ref_vec, semicolon_for_stmt, trimmed_last_line_width, wrap_str} ;
34
+ use utils:: { colon_spaces, contains_skip, count_newlines, extra_offset, first_line_width,
35
+ inner_attributes, last_line_extendable, last_line_width, mk_sp, outer_attributes,
36
+ paren_overhead, ptr_vec_to_ref_vec, semicolon_for_stmt, trimmed_last_line_width,
37
+ wrap_str} ;
37
38
use vertical:: rewrite_with_alignment;
38
39
use visitor:: FmtVisitor ;
39
40
@@ -2053,6 +2054,26 @@ where
2053
2054
// Replace the stub with the full overflowing last argument if the rewrite
2054
2055
// succeeded and its first line fits with the other arguments.
2055
2056
match ( overflow_last, tactic, placeholder) {
2057
+ ( true , DefinitiveListTactic :: Horizontal , Some ( ref overflowed) ) if args. len ( ) == 1 => {
2058
+ // When we are rewriting a nested function call, we restrict the
2059
+ // bugdet for the inner function to avoid them being deeply nested.
2060
+ // However, when the inner function has a prefix or a suffix
2061
+ // (e.g. `foo() as u32`), this budget reduction may produce poorly
2062
+ // formatted code, where a prefix or a suffix being left on its own
2063
+ // line. Here we explicitlly check those cases.
2064
+ if count_newlines ( overflowed) == 1 {
2065
+ let rw = args. last ( )
2066
+ . and_then ( |last_arg| last_arg. rewrite ( context, nested_shape) ) ;
2067
+ let no_newline = rw. as_ref ( ) . map_or ( false , |s| !s. contains ( '\n' ) ) ;
2068
+ if no_newline {
2069
+ item_vec[ args. len ( ) - 1 ] . item = rw;
2070
+ } else {
2071
+ item_vec[ args. len ( ) - 1 ] . item = Some ( overflowed. to_owned ( ) ) ;
2072
+ }
2073
+ } else {
2074
+ item_vec[ args. len ( ) - 1 ] . item = Some ( overflowed. to_owned ( ) ) ;
2075
+ }
2076
+ }
2056
2077
( true , DefinitiveListTactic :: Horizontal , placeholder @ Some ( ..) ) => {
2057
2078
item_vec[ args. len ( ) - 1 ] . item = placeholder;
2058
2079
}
@@ -2824,7 +2845,6 @@ pub fn choose_rhs<R: Rewrite>(
2824
2845
}
2825
2846
2826
2847
fn prefer_next_line ( orig_rhs : & str , next_line_rhs : & str ) -> bool {
2827
- use utils:: count_newlines;
2828
2848
!next_line_rhs. contains ( '\n' ) || count_newlines ( orig_rhs) > count_newlines ( next_line_rhs) + 1
2829
2849
}
2830
2850
0 commit comments