]> git.lizzy.rs Git - rust.git/commitdiff
Split out rustdoc pass to strip private imports
authormitaa <mitaa.ceb@gmail.com>
Sat, 5 Mar 2016 10:20:34 +0000 (11:20 +0100)
committermitaa <mitaa.ceb@gmail.com>
Sun, 6 Mar 2016 07:18:58 +0000 (08:18 +0100)
src/librustdoc/lib.rs
src/librustdoc/passes.rs
src/test/auxiliary/empty.rs [new file with mode: 0644]
src/test/rustdoc/issue-15347.rs
src/test/rustdoc/issue-27104.rs [new file with mode: 0644]

index ffb15d157b0666e10bb3948f41344c789af8b95d..5ddab8a2862880576d6cf3faf48d7224d32ea301 100644 (file)
@@ -107,6 +107,8 @@ pub mod html {
      "concatenates all document attributes into one document attribute"),
     ("strip-private", passes::strip_private,
      "strips all private items from a crate which cannot be seen externally"),
+    ("strip-priv-imports", passes::strip_priv_imports,
+     "strips all private import statements (`use`, `extern crate`) from a crate"),
 ];
 
 const DEFAULT_PASSES: &'static [&'static str] = &[
index 957957eaec6e5a1fda9a764555ac984c0479adf4..1ce703940a62e472c351f3557796f76daa4029a5 100644 (file)
@@ -106,7 +106,7 @@ pub fn strip_private(mut krate: clean::Crate) -> plugins::PluginResult {
             retained: &mut retained,
             access_levels: &access_levels,
         };
-        krate = stripper.fold_crate(krate);
+        krate = ImportStripper.fold_crate(stripper.fold_crate(krate));
     }
 
     // strip all private implementations of traits
@@ -144,12 +144,6 @@ fn fold_item(&mut self, i: Item) -> Option<Item> {
                 }
             }
 
-            clean::ExternCrateItem(..) | clean::ImportItem(_) => {
-                if i.visibility != Some(hir::Public) {
-                    return None
-                }
-            }
-
             clean::StructFieldItem(..) => {
                 if i.visibility != Some(hir::Public) {
                     return Some(clean::Item {
@@ -170,6 +164,9 @@ fn fold_item(&mut self, i: Item) -> Option<Item> {
                     return None;
                 }
             }
+            // handled in the `strip-priv-imports` pass
+            clean::ExternCrateItem(..) | clean::ImportItem(_) => {}
+
             clean::DefaultImplItem(..) | clean::ImplItem(..) => {}
 
             // tymethods/macros have no control over privacy
@@ -242,6 +239,21 @@ fn fold_item(&mut self, i: Item) -> Option<Item> {
     }
 }
 
+// This stripper discards all private import statements (`use`, `extern crate`)
+struct ImportStripper;
+impl fold::DocFolder for ImportStripper {
+    fn fold_item(&mut self, i: Item) -> Option<Item> {
+        match i.inner {
+            clean::ExternCrateItem(..) |
+            clean::ImportItem(..) if i.visibility != Some(hir::Public) => None,
+            _ => self.fold_item_recur(i)
+        }
+    }
+}
+
+pub fn strip_priv_imports(krate: clean::Crate)  -> plugins::PluginResult {
+    (ImportStripper.fold_crate(krate), None)
+}
 
 pub fn unindent_comments(krate: clean::Crate) -> plugins::PluginResult {
     struct CommentCleaner;
diff --git a/src/test/auxiliary/empty.rs b/src/test/auxiliary/empty.rs
new file mode 100644 (file)
index 0000000..3066947
--- /dev/null
@@ -0,0 +1,9 @@
+// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
index 97c37bbc1eda0619f5925aca0ab3a1a106cb69d8..266a30891941dbfde7e88ccc05d2bb4df297cc59 100644 (file)
@@ -8,7 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-// compile-flags:--no-defaults --passes "collapse-docs" --passes "unindent-comments"
+// compile-flags:--no-defaults --passes collapse-docs --passes unindent-comments
 
 // @has issue_15347/fn.foo.html
 #[doc(hidden)]
diff --git a/src/test/rustdoc/issue-27104.rs b/src/test/rustdoc/issue-27104.rs
new file mode 100644 (file)
index 0000000..5fa093d
--- /dev/null
@@ -0,0 +1,20 @@
+// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// compile-flags:--no-defaults --passes strip-priv-imports
+// aux-build:empty.rs
+// ignore-cross-compile
+
+// @has issue_27104/index.html
+// @!has - 'extern crate std'
+// @!has - 'use std::prelude::'
+
+// @has - 'pub extern crate empty'
+pub extern crate empty;