Skip to content

Commit 72d588a

Browse files
committed
Auto merge of #32055 - mitaa:rdoc-strip-priv-imports, r=alexcrichton
fixes #27104 r? @alexcrichton
2 parents 71f4658 + 688e522 commit 72d588a

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
@@ -109,6 +109,8 @@ const PASSES: &'static [Pass] = &[
109109
"concatenates all document attributes into one document attribute"),
110110
("strip-private", passes::strip_private,
111111
"strips all private items from a crate which cannot be seen externally"),
112+
("strip-priv-imports", passes::strip_priv_imports,
113+
"strips all private import statements (`use`, `extern crate`) from a crate"),
112114
];
113115

114116
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
@@ -239,6 +236,21 @@ impl<'a> fold::DocFolder for ImplStripper<'a> {
239236
}
240237
}
241238

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

243255
pub fn unindent_comments(krate: clean::Crate) -> plugins::PluginResult {
244256
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)