Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: do not emit tuple variant fields if none are documented #91687

Merged
merged 1 commit into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,18 +1133,27 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
w.write_str("</h3>");

use crate::clean::Variant;
if let Some((extra, fields)) = match *variant.kind {
clean::VariantItem(Variant::Struct(ref s)) => Some(("", &s.fields)),
clean::VariantItem(Variant::Tuple(ref fields)) => Some(("Tuple ", fields)),

let heading_and_fields = match &*variant.kind {
clean::VariantItem(Variant::Struct(s)) => Some(("Fields", &s.fields)),
// Documentation on tuple variant fields is rare, so to reduce noise we only emit
// the section if at least one field is documented.
clean::VariantItem(Variant::Tuple(fields))
if fields.iter().any(|f| f.doc_value().is_some()) =>
{
Some(("Tuple Fields", fields))
}
_ => None,
} {
};

if let Some((heading, fields)) = heading_and_fields {
let variant_id = cx.derive_id(format!(
"{}.{}.fields",
ItemType::Variant,
variant.name.as_ref().unwrap()
));
write!(w, "<div class=\"sub-variant\" id=\"{id}\">", id = variant_id);
write!(w, "<h4>{extra}Fields</h4>", extra = extra,);
write!(w, "<h4>{heading}</h4>", heading = heading);
document_non_exhaustive(w, variant);
for field in fields {
match *field.kind {
Expand Down
5 changes: 3 additions & 2 deletions src/test/rustdoc/issue-88600.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@ pub enum FooEnum {
// @has - '//*[@id="variant.MixedHiddenFirst"]//code' 'MixedHiddenFirst(_, S)'
// @count - '//*[@id="variant.MixedHiddenFirst.field.0"]' 0
// @has - '//*[@id="variant.MixedHiddenFirst.field.1"]' '1: S'
MixedHiddenFirst(#[doc(hidden)] H, S),
MixedHiddenFirst(#[doc(hidden)] H, /** dox */ S),
// @has - '//*[@id="variant.MixedHiddenLast"]//code' 'MixedHiddenLast(S, _)'
// @has - '//*[@id="variant.MixedHiddenLast.field.0"]' '0: S'
// @count - '//*[@id="variant.MixedHiddenLast.field.1"]' 0
MixedHiddenLast(S, #[doc(hidden)] H),
MixedHiddenLast(/** dox */ S, #[doc(hidden)] H),
// @has - '//*[@id="variant.HiddenStruct"]//code' 'HiddenStruct'
// @count - '//*[@id="variant.HiddenStruct.field.h"]' 0
// @has - '//*[@id="variant.HiddenStruct.field.s"]' 's: S'
HiddenStruct {
#[doc(hidden)]
h: H,
/// dox
s: S,
},
}
14 changes: 14 additions & 0 deletions src/test/rustdoc/tuple-struct-fields-doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub struct Foo(
// @has - '//*[@id="variant.BarVariant.field.0"]' '0: String'
// @has - '//*[@id="variant.BarVariant.fields"]//*[@class="docblock"]' 'Hello docs'
// @matches - '//*[@id="variant.FooVariant.fields"]/h4' '^Fields$'
// @has - '//*[@id="variant.BazVariant.fields"]//*[@class="docblock"]' 'dox'
// @has - '//*[@id="variant.OtherVariant.fields"]//*[@class="docblock"]' 'dox'
// @!matches - '//*[@id="variant.QuuxVariant.fields"]/h4' '^Tuple Fields$'
pub enum Bar {
BarVariant(
/// Hello docs
Expand All @@ -33,4 +36,15 @@ pub enum Bar {
/// hello
x: u32,
},
BazVariant(
String,
/// dox
u32,
),
OtherVariant(
/// dox
String,
u32,
),
QuuxVariant(String),
}