Skip to content

Commit 062a719

Browse files
committed
Split out rustdoc pass to strip private imports
1 parent 2170479 commit 062a719

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ const PASSES: &'static [Pass] = &[
107107
"concatenates all document attributes into one document attribute"),
108108
("strip-private", passes::strip_private,
109109
"strips all private items from a crate which cannot be seen externally"),
110+
("strip-priv-imports", passes::strip_priv_imports,
111+
"strips all private import statements (`use`, `extern crate`) from a crate"),
110112
];
111113

112114
const DEFAULT_PASSES: &'static [&'static str] = &[

passes.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub fn strip_private(mut krate: clean::Crate) -> plugins::PluginResult {
106106
retained: &mut retained,
107107
access_levels: &access_levels,
108108
};
109-
krate = stripper.fold_crate(krate);
109+
krate = ImportStripper.fold_crate(stripper.fold_crate(krate));
110110
}
111111

112112
// strip all private implementations of traits
@@ -144,12 +144,6 @@ impl<'a> fold::DocFolder for Stripper<'a> {
144144
}
145145
}
146146

147-
clean::ExternCrateItem(..) | clean::ImportItem(_) => {
148-
if i.visibility != Some(hir::Public) {
149-
return None
150-
}
151-
}
152-
153147
clean::StructFieldItem(..) => {
154148
if i.visibility != Some(hir::Public) {
155149
return Some(clean::Item {
@@ -170,6 +164,9 @@ impl<'a> fold::DocFolder for Stripper<'a> {
170164
return None;
171165
}
172166
}
167+
// handled in the `strip-priv-imports` pass
168+
clean::ExternCrateItem(..) | clean::ImportItem(_) => {}
169+
173170
clean::DefaultImplItem(..) | clean::ImplItem(..) => {}
174171

175172
// tymethods/macros have no control over privacy
@@ -242,6 +239,21 @@ impl<'a> fold::DocFolder for ImplStripper<'a> {
242239
}
243240
}
244241

242+
// This stripper discards all private import statements (`use`, `extern crate`)
243+
struct ImportStripper;
244+
impl fold::DocFolder for ImportStripper {
245+
fn fold_item(&mut self, i: Item) -> Option<Item> {
246+
match i.inner {
247+
clean::ExternCrateItem(..) |
248+
clean::ImportItem(..) if i.visibility != Some(hir::Public) => None,
249+
_ => self.fold_item_recur(i)
250+
}
251+
}
252+
}
253+
254+
pub fn strip_priv_imports(krate: clean::Crate) -> plugins::PluginResult {
255+
(ImportStripper.fold_crate(krate), None)
256+
}
245257

246258
pub fn unindent_comments(krate: clean::Crate) -> plugins::PluginResult {
247259
struct CommentCleaner;

0 commit comments

Comments
 (0)