@@ -162,17 +162,6 @@ struct ItemVars<'a> {
162162 src_href : Option < & ' a str > ,
163163}
164164
165- /// Calls `print_where_clause` and returns `true` if a `where` clause was generated.
166- fn print_where_clause_and_check < ' a , ' tcx : ' a > (
167- buffer : & mut String ,
168- gens : & ' a clean:: Generics ,
169- cx : & ' a Context < ' tcx > ,
170- ) -> bool {
171- let len_before = buffer. len ( ) ;
172- write_str ( buffer, format_args ! ( "{}" , print_where_clause( gens, cx, 0 , Ending :: Newline ) ) ) ;
173- len_before != buffer. len ( )
174- }
175-
176165pub ( super ) fn print_item ( cx : & Context < ' _ > , item : & clean:: Item , buf : & mut String ) {
177166 debug_assert ! ( !item. is_stripped( ) ) ;
178167 let typ = match item. kind {
@@ -638,7 +627,8 @@ fn item_function<'a, 'tcx>(
638627 abi = abi,
639628 name = name,
640629 generics = f. generics. print( cx) ,
641- where_clause = print_where_clause( & f. generics, cx, 0 , Ending :: Newline ) ,
630+ where_clause =
631+ print_where_clause( & f. generics, cx, 0 , Ending :: Newline ) . maybe_display( ) ,
642632 decl = f. decl. full_print( header_len, 0 , cx) ,
643633 )
644634 } ) ?;
@@ -682,7 +672,11 @@ fn item_trait<'a, 'tcx>(
682672 ) ?;
683673
684674 if !t. generics . where_predicates . is_empty ( ) {
685- write ! ( w, "{}" , print_where_clause( & t. generics, cx, 0 , Ending :: Newline ) ) ?;
675+ write ! (
676+ w,
677+ "{}" ,
678+ print_where_clause( & t. generics, cx, 0 , Ending :: Newline ) . maybe_display( )
679+ ) ?;
686680 } else {
687681 w. write_char ( ' ' ) ?;
688682 }
@@ -1252,7 +1246,7 @@ fn item_trait_alias(
12521246 attrs = render_attributes_in_pre( it, "" , cx) ,
12531247 name = it. name. unwrap( ) ,
12541248 generics = t. generics. print( cx) ,
1255- where_b = print_where_clause( & t. generics, cx, 0 , Ending :: Newline ) ,
1249+ where_b = print_where_clause( & t. generics, cx, 0 , Ending :: Newline ) . maybe_display ( ) ,
12561250 bounds = bounds( & t. bounds, true , cx) ,
12571251 )
12581252 . unwrap ( ) ;
@@ -1277,7 +1271,8 @@ fn item_type_alias(w: &mut String, cx: &Context<'_>, it: &clean::Item, t: &clean
12771271 vis = visibility_print_with_space( it, cx) ,
12781272 name = it. name. unwrap( ) ,
12791273 generics = t. generics. print( cx) ,
1280- where_clause = print_where_clause( & t. generics, cx, 0 , Ending :: Newline ) ,
1274+ where_clause =
1275+ print_where_clause( & t. generics, cx, 0 , Ending :: Newline ) . maybe_display( ) ,
12811276 type_ = t. type_. print( cx) ,
12821277 ) ,
12831278 ) ;
@@ -1651,9 +1646,13 @@ fn render_enum_fields(
16511646 enum_def_id : DefId ,
16521647) {
16531648 let should_show_enum_discriminant = should_show_enum_discriminant ( cx, enum_def_id, variants) ;
1654- if !g. is_some_and ( |g| print_where_clause_and_check ( w, g, cx) ) {
1649+ if let Some ( generics) = g
1650+ && let Some ( where_clause) = print_where_clause ( generics, cx, 0 , Ending :: Newline )
1651+ {
1652+ write_str ( w, format_args ! ( "{where_clause}" ) ) ;
1653+ } else {
16551654 // If there wasn't a `where` clause, we add a whitespace.
1656- w. push_str ( " " ) ;
1655+ w. push ( ' ' )
16571656 }
16581657
16591658 let variants_stripped = has_stripped_entries;
@@ -1930,7 +1929,8 @@ fn item_constant(
19301929 name = it. name. unwrap( ) ,
19311930 generics = generics. print( cx) ,
19321931 typ = ty. print( cx) ,
1933- where_clause = print_where_clause( generics, cx, 0 , Ending :: NoNewline )
1932+ where_clause =
1933+ print_where_clause( generics, cx, 0 , Ending :: NoNewline ) . maybe_display( ) ,
19341934 ) ,
19351935 ) ;
19361936
@@ -2295,14 +2295,17 @@ fn render_union<'a, 'cx: 'a>(
22952295 fmt:: from_fn ( move |mut f| {
22962296 write ! ( f, "{}union {}" , visibility_print_with_space( it, cx) , it. name. unwrap( ) , ) ?;
22972297
2298- let where_displayed = g
2299- . map ( |g| {
2300- let mut buf = g. print ( cx) . to_string ( ) ;
2301- let where_displayed = print_where_clause_and_check ( & mut buf, g, cx) ;
2302- f. write_str ( & buf) . unwrap ( ) ;
2303- where_displayed
2304- } )
2305- . unwrap_or ( false ) ;
2298+ let where_displayed = if let Some ( generics) = g {
2299+ write ! ( f, "{}" , generics. print( cx) ) ?;
2300+ if let Some ( where_clause) = print_where_clause ( generics, cx, 0 , Ending :: Newline ) {
2301+ write ! ( f, "{where_clause}" ) ?;
2302+ true
2303+ } else {
2304+ false
2305+ }
2306+ } else {
2307+ false
2308+ } ;
23062309
23072310 // If there wasn't a `where` clause, we add a whitespace.
23082311 if !where_displayed {
@@ -2386,8 +2389,14 @@ fn render_struct_fields(
23862389) {
23872390 match ty {
23882391 None => {
2389- let where_displayed =
2390- g. map ( |g| print_where_clause_and_check ( w, g, cx) ) . unwrap_or ( false ) ;
2392+ let where_displayed = if let Some ( generics) = g
2393+ && let Some ( where_clause) = print_where_clause ( generics, cx, 0 , Ending :: Newline )
2394+ {
2395+ write_str ( w, format_args ! ( "{where_clause}" ) ) ;
2396+ true
2397+ } else {
2398+ false
2399+ } ;
23912400
23922401 // If there wasn't a `where` clause, we add a whitespace.
23932402 if !where_displayed {
@@ -2467,7 +2476,13 @@ fn render_struct_fields(
24672476 }
24682477 w. push_str ( ")" ) ;
24692478 if let Some ( g) = g {
2470- write_str ( w, format_args ! ( "{}" , print_where_clause( g, cx, 0 , Ending :: NoNewline ) ) ) ;
2479+ write_str (
2480+ w,
2481+ format_args ! (
2482+ "{}" ,
2483+ print_where_clause( g, cx, 0 , Ending :: NoNewline ) . maybe_display( )
2484+ ) ,
2485+ ) ;
24712486 }
24722487 // We only want a ";" when we are displaying a tuple struct, not a variant tuple struct.
24732488 if structhead {
@@ -2477,7 +2492,13 @@ fn render_struct_fields(
24772492 Some ( CtorKind :: Const ) => {
24782493 // Needed for PhantomData.
24792494 if let Some ( g) = g {
2480- write_str ( w, format_args ! ( "{}" , print_where_clause( g, cx, 0 , Ending :: NoNewline ) ) ) ;
2495+ write_str (
2496+ w,
2497+ format_args ! (
2498+ "{}" ,
2499+ print_where_clause( g, cx, 0 , Ending :: NoNewline ) . maybe_display( )
2500+ ) ,
2501+ ) ;
24812502 }
24822503 w. push_str ( ";" ) ;
24832504 }
0 commit comments