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

This commit is contained in:
Andy Russell 2021-12-08 20:09:17 -05:00
parent 89b9f7b284
commit 4193f2da2d
No known key found for this signature in database
GPG Key ID: BE2221033EDBC374
3 changed files with 31 additions and 7 deletions

View File

@ -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 {

View File

@ -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,
},
}

View File

@ -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
@ -33,4 +36,15 @@ pub enum Bar {
/// hello
x: u32,
},
BazVariant(
String,
/// dox
u32,
),
OtherVariant(
/// dox
String,
u32,
),
QuuxVariant(String),
}