Skip to content

Commit baa3f96

Browse files
Rollup merge of #118600 - GuillaumeGomez:fields-heading, r=notriddle
[rustdoc] Don't generate the "Fields" heading if there is no field displayed Fixes #118195. If no field is displayed, we should not generate the `Fields` heading in enum struct variants. r? ``@notriddle``
2 parents 89ed7fc + 8e53edb commit baa3f96

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

Diff for: src/librustdoc/html/render/print_item.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1737,7 +1737,14 @@ fn item_variants(
17371737
w.write_str("</h3></section>");
17381738

17391739
let heading_and_fields = match &variant_data.kind {
1740-
clean::VariantKind::Struct(s) => Some(("Fields", &s.fields)),
1740+
clean::VariantKind::Struct(s) => {
1741+
// If there is no field to display, no need to add the heading.
1742+
if s.fields.iter().any(|f| !f.is_doc_hidden()) {
1743+
Some(("Fields", &s.fields))
1744+
} else {
1745+
None
1746+
}
1747+
}
17411748
clean::VariantKind::Tuple(fields) => {
17421749
// Documentation on tuple variant fields is rare, so to reduce noise we only emit
17431750
// the section if at least one field is documented.

Diff for: tests/rustdoc/enum-variant-fields-heading.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// This is a regression test for <https://github.com/rust-lang/rust/issues/118195>.
2+
// It ensures that the "Fields" heading is not generated if no field is displayed.
3+
4+
#![crate_name = "foo"]
5+
6+
// @has 'foo/enum.Foo.html'
7+
// @has - '//*[@id="variant.A"]' 'A'
8+
// @count - '//*[@id="variant.A.fields"]' 0
9+
// @has - '//*[@id="variant.B"]' 'B'
10+
// @count - '//*[@id="variant.B.fields"]' 0
11+
// @snapshot variants - '//*[@id="main-content"]/*[@class="variants"]'
12+
13+
pub enum Foo {
14+
/// A variant with no fields
15+
A {},
16+
/// A variant with hidden fields
17+
B { #[doc(hidden)] a: u8 },
18+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="variants"><section id="variant.A" class="variant"><a href="#variant.A" class="anchor">&#167;</a><h3 class="code-header">A</h3></section><div class="docblock"><p>A variant with no fields</p>
2+
</div><section id="variant.B" class="variant"><a href="#variant.B" class="anchor">&#167;</a><h3 class="code-header">B</h3></section><div class="docblock"><p>A variant with hidden fields</p>
3+
</div></div>

0 commit comments

Comments
 (0)