]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/passes/propagate_doc_cfg.rs
remove unused return types such as empty Results or Options that would always be...
[rust.git] / src / librustdoc / passes / propagate_doc_cfg.rs
1 use std::sync::Arc;
2
3 use crate::clean::cfg::Cfg;
4 use crate::clean::{Crate, Item};
5 use crate::core::DocContext;
6 use crate::fold::DocFolder;
7 use crate::passes::Pass;
8
9 crate const PROPAGATE_DOC_CFG: Pass = Pass {
10     name: "propagate-doc-cfg",
11     run: propagate_doc_cfg,
12     description: "propagates `#[doc(cfg(...))]` to child items",
13 };
14
15 crate fn propagate_doc_cfg(cr: Crate, _: &DocContext<'_>) -> Crate {
16     CfgPropagator { parent_cfg: None }.fold_crate(cr)
17 }
18
19 struct CfgPropagator {
20     parent_cfg: Option<Arc<Cfg>>,
21 }
22
23 impl DocFolder for CfgPropagator {
24     fn fold_item(&mut self, mut item: Item) -> Option<Item> {
25         let old_parent_cfg = self.parent_cfg.clone();
26
27         let new_cfg = match (self.parent_cfg.take(), item.attrs.cfg.take()) {
28             (None, None) => None,
29             (Some(rc), None) | (None, Some(rc)) => Some(rc),
30             (Some(mut a), Some(b)) => {
31                 let b = Arc::try_unwrap(b).unwrap_or_else(|rc| Cfg::clone(&rc));
32                 *Arc::make_mut(&mut a) &= b;
33                 Some(a)
34             }
35         };
36         self.parent_cfg = new_cfg.clone();
37         item.attrs.cfg = new_cfg;
38
39         let result = self.fold_item_recur(item);
40         self.parent_cfg = old_parent_cfg;
41
42         Some(result)
43     }
44 }