Skip to content

Commit 688e522

Browse files
committed
Split out rustdoc pass to strip private imports
1 parent f6e125f commit 688e522

File tree

5 files changed

+51
-8
lines changed

5 files changed

+51
-8
lines changed

src/librustdoc/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] = &[

src/librustdoc/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;

src/test/auxiliary/empty.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.

src/test/rustdoc/issue-15347.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags:--no-defaults --passes "collapse-docs" --passes "unindent-comments"
11+
// compile-flags:--no-defaults --passes collapse-docs --passes unindent-comments
1212

1313
// @has issue_15347/fn.foo.html
1414
#[doc(hidden)]

src/test/rustdoc/issue-27104.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags:--no-defaults --passes strip-priv-imports
12+
// aux-build:empty.rs
13+
// ignore-cross-compile
14+
15+
// @has issue_27104/index.html
16+
// @!has - 'extern crate std'
17+
// @!has - 'use std::prelude::'
18+
19+
// @has - 'pub extern crate empty'
20+
pub extern crate empty;

0 commit comments

Comments
 (0)