@@ -1453,18 +1453,22 @@ 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 has_variants_with_value = false ;
1462
+ for variant in variants {
1463
+ if let clean:: VariantItem ( ref var) = * variant. kind &&
1464
+ matches ! ( var. kind, clean:: VariantKind :: CLike )
1465
+ {
1466
+ has_variants_with_value |= var. discriminant . is_some ( ) ;
1467
+ } else {
1468
+ return false ;
1469
+ }
1470
+ }
1471
+ has_variants_with_value
1468
1472
}
1469
1473
1470
1474
fn display_c_like_variant (
@@ -1473,12 +1477,12 @@ fn display_c_like_variant(
1473
1477
item : & clean:: Item ,
1474
1478
variant : & clean:: Variant ,
1475
1479
index : rustc_target:: abi:: VariantIdx ,
1476
- is_c_like_enum : bool ,
1480
+ should_show_c_like_variants_value : bool ,
1477
1481
) {
1478
1482
let name = item. name . unwrap ( ) ;
1479
1483
if let Some ( ref value) = variant. discriminant {
1480
1484
write ! ( w, "{} = {}" , name. as_str( ) , value. value( cx. tcx( ) , true ) ) ;
1481
- } else if is_c_like_enum &&
1485
+ } else if should_show_c_like_variants_value &&
1482
1486
let Some ( variant_def_id) = item. item_id . as_def_id ( ) &&
1483
1487
let Some ( variant_def_id) = variant_def_id. as_local ( )
1484
1488
{
@@ -1504,7 +1508,7 @@ fn render_enum_fields(
1504
1508
has_stripped_entries : bool ,
1505
1509
is_non_exhaustive : bool ,
1506
1510
) {
1507
- let is_c_like_enum = is_c_like_enum ( variants) ;
1511
+ let should_show_c_like_variants_value = should_show_c_like_variants_value ( variants) ;
1508
1512
if !g. is_some_and ( |g| print_where_clause_and_check ( w, g, cx) ) {
1509
1513
// If there wasn't a `where` clause, we add a whitespace.
1510
1514
w. write_str ( " " ) ;
@@ -1527,9 +1531,14 @@ fn render_enum_fields(
1527
1531
w. write_str ( TAB ) ;
1528
1532
match * v. kind {
1529
1533
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
- }
1534
+ clean:: VariantKind :: CLike => display_c_like_variant (
1535
+ w,
1536
+ cx,
1537
+ v,
1538
+ var,
1539
+ index,
1540
+ should_show_c_like_variants_value,
1541
+ ) ,
1533
1542
clean:: VariantKind :: Tuple ( ref s) => {
1534
1543
write ! ( w, "{}({})" , v. name. unwrap( ) , print_tuple_struct_fields( cx, s) ) ;
1535
1544
}
@@ -1569,7 +1578,7 @@ fn item_variants(
1569
1578
document_non_exhaustive_header( it) ,
1570
1579
document_non_exhaustive( it)
1571
1580
) ;
1572
- let is_c_like_enum = is_c_like_enum ( variants) ;
1581
+ let should_show_c_like_variants_value = should_show_c_like_variants_value ( variants) ;
1573
1582
for ( index, variant) in variants. iter_enumerated ( ) {
1574
1583
if variant. is_stripped ( ) {
1575
1584
continue ;
@@ -1598,7 +1607,7 @@ fn item_variants(
1598
1607
variant,
1599
1608
var,
1600
1609
index,
1601
- is_c_like_enum ,
1610
+ should_show_c_like_variants_value ,
1602
1611
) ;
1603
1612
} else {
1604
1613
w. write_str ( variant. name . unwrap ( ) . as_str ( ) ) ;
0 commit comments