@@ -1453,18 +1453,24 @@ fn get_parent_enum_def_id(
1453
1453
panic ! ( "No parent enum found for variant {variant_def_id:?}" ) ;
1454
1454
}
1455
1455
1456
- fn is_c_like_enum (
1456
+ /// It'll return true if all variants are C-like variants and if at least one of them has a value
1457
+ /// set.
1458
+ fn should_show_c_like_variants_value (
1457
1459
variants : & rustc_index:: IndexVec < rustc_target:: abi:: VariantIdx , clean:: Item > ,
1458
1460
) -> bool {
1459
- !variants. iter ( ) . any ( |variant| {
1460
- matches ! (
1461
- * variant. kind,
1462
- clean:: VariantItem ( clean:: Variant {
1463
- kind: clean:: VariantKind :: Tuple ( _) | clean:: VariantKind :: Struct ( _) ,
1464
- ..
1465
- } )
1466
- )
1467
- } )
1461
+ let mut nb_c_like_variants_with_value = 0 ;
1462
+ for variant in variants {
1463
+ if let clean:: VariantItem ( ref var) = * variant. kind &&
1464
+ matches ! ( var. kind, clean:: VariantKind :: CLike )
1465
+ {
1466
+ if var. discriminant . is_some ( ) {
1467
+ nb_c_like_variants_with_value += 1 ;
1468
+ }
1469
+ } else {
1470
+ return false ;
1471
+ }
1472
+ }
1473
+ nb_c_like_variants_with_value > 0
1468
1474
}
1469
1475
1470
1476
fn display_c_like_variant (
@@ -1473,12 +1479,12 @@ fn display_c_like_variant(
1473
1479
item : & clean:: Item ,
1474
1480
variant : & clean:: Variant ,
1475
1481
index : rustc_target:: abi:: VariantIdx ,
1476
- is_c_like_enum : bool ,
1482
+ should_show_c_like_variants_value : bool ,
1477
1483
) {
1478
1484
let name = item. name . unwrap ( ) ;
1479
1485
if let Some ( ref value) = variant. discriminant {
1480
1486
write ! ( w, "{} = {}" , name. as_str( ) , value. value( cx. tcx( ) , true ) ) ;
1481
- } else if is_c_like_enum &&
1487
+ } else if should_show_c_like_variants_value &&
1482
1488
let Some ( variant_def_id) = item. item_id . as_def_id ( ) &&
1483
1489
let Some ( variant_def_id) = variant_def_id. as_local ( )
1484
1490
{
@@ -1504,7 +1510,7 @@ fn render_enum_fields(
1504
1510
has_stripped_entries : bool ,
1505
1511
is_non_exhaustive : bool ,
1506
1512
) {
1507
- let is_c_like_enum = is_c_like_enum ( variants) ;
1513
+ let should_show_c_like_variants_value = should_show_c_like_variants_value ( variants) ;
1508
1514
if !g. is_some_and ( |g| print_where_clause_and_check ( w, g, cx) ) {
1509
1515
// If there wasn't a `where` clause, we add a whitespace.
1510
1516
w. write_str ( " " ) ;
@@ -1527,9 +1533,14 @@ fn render_enum_fields(
1527
1533
w. write_str ( TAB ) ;
1528
1534
match * v. kind {
1529
1535
clean:: VariantItem ( ref var) => match var. kind {
1530
- clean:: VariantKind :: CLike => {
1531
- display_c_like_variant ( w, cx, v, var, index, is_c_like_enum)
1532
- }
1536
+ clean:: VariantKind :: CLike => display_c_like_variant (
1537
+ w,
1538
+ cx,
1539
+ v,
1540
+ var,
1541
+ index,
1542
+ should_show_c_like_variants_value,
1543
+ ) ,
1533
1544
clean:: VariantKind :: Tuple ( ref s) => {
1534
1545
write ! ( w, "{}({})" , v. name. unwrap( ) , print_tuple_struct_fields( cx, s) ) ;
1535
1546
}
@@ -1569,7 +1580,7 @@ fn item_variants(
1569
1580
document_non_exhaustive_header( it) ,
1570
1581
document_non_exhaustive( it)
1571
1582
) ;
1572
- let is_c_like_enum = is_c_like_enum ( variants) ;
1583
+ let should_show_c_like_variants_value = should_show_c_like_variants_value ( variants) ;
1573
1584
for ( index, variant) in variants. iter_enumerated ( ) {
1574
1585
if variant. is_stripped ( ) {
1575
1586
continue ;
@@ -1598,7 +1609,7 @@ fn item_variants(
1598
1609
variant,
1599
1610
var,
1600
1611
index,
1601
- is_c_like_enum ,
1612
+ should_show_c_like_variants_value ,
1602
1613
) ;
1603
1614
} else {
1604
1615
w. write_str ( variant. name . unwrap ( ) . as_str ( ) ) ;
0 commit comments