1 //! Strip all doc(hidden) items from the output.
2 use rustc_span::symbol::sym;
6 use crate::clean::{Item, ItemIdSet, NestedAttributesExt};
7 use crate::core::DocContext;
8 use crate::fold::{strip_item, DocFolder};
9 use crate::passes::{ImplStripper, Pass};
11 pub(crate) const STRIP_HIDDEN: Pass = Pass {
14 description: "strips all `#[doc(hidden)]` items from the output",
17 /// Strip items marked `#[doc(hidden)]`
18 pub(crate) fn strip_hidden(krate: clean::Crate, cx: &mut DocContext<'_>) -> clean::Crate {
19 let mut retained = ItemIdSet::default();
20 let is_json_output = cx.output_format.is_json() && !cx.show_coverage;
22 // strip all #[doc(hidden)] items
24 let mut stripper = Stripper { retained: &mut retained, update_retained: true };
25 stripper.fold_crate(krate)
28 // strip all impls referencing stripped items
29 let mut stripper = ImplStripper {
33 document_private: cx.render_options.document_private,
35 stripper.fold_crate(krate)
39 retained: &'a mut ItemIdSet,
40 update_retained: bool,
43 impl<'a> DocFolder for Stripper<'a> {
44 fn fold_item(&mut self, i: Item) -> Option<Item> {
45 if i.attrs.lists(sym::doc).has_word(sym::hidden) {
46 debug!("strip_hidden: stripping {:?} {:?}", i.type_(), i.name);
47 // Use a dedicated hidden item for fields, variants, and modules.
48 // We need to keep private fields and variants, so that the docs
49 // can show a placeholder "// some variants omitted". We need to keep
50 // private modules, because they can contain impl blocks, and impl
51 // block privacy is inherited from the type and trait, not from the
52 // module it's defined in. Both of these are marked "stripped," and
53 // not included in the final docs, but since they still have an effect
54 // on the final doc, cannot be completely removed from the Clean IR.
56 clean::StructFieldItem(..) | clean::ModuleItem(..) | clean::VariantItem(..) => {
57 // We need to recurse into stripped modules to
58 // strip things like impl methods but when doing so
59 // we must not add any items to the `retained` set.
60 let old = mem::replace(&mut self.update_retained, false);
61 let ret = strip_item(self.fold_item_recur(i));
62 self.update_retained = old;
68 if self.update_retained {
69 self.retained.insert(i.item_id);
72 Some(self.fold_item_recur(i))