about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2021-02-13 21:44:42 +0100
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2021-03-04 21:22:42 +0100
commit4b30625094eec6b339c83bb41b92eb206df7280d (patch)
tree49ef3aba336c3abd043d674e24565c5701424781
parent409920873cf8a95739a55dc5fe5adb05e1b4758e (diff)
downloadrust-4b30625094eec6b339c83bb41b92eb206df7280d.tar.gz
rust-4b30625094eec6b339c83bb41b92eb206df7280d.zip
Don't warn for `missing_doc_examples` when item is #[doc(hidden)]
-rw-r--r--src/librustdoc/passes/doc_test_lints.rs11
-rw-r--r--src/librustdoc/visit_ast.rs26
2 files changed, 23 insertions, 14 deletions
diff --git a/src/librustdoc/passes/doc_test_lints.rs b/src/librustdoc/passes/doc_test_lints.rs
index 81104236314..19815c7cd5f 100644
--- a/src/librustdoc/passes/doc_test_lints.rs
+++ b/src/librustdoc/passes/doc_test_lints.rs
@@ -9,8 +9,10 @@ use crate::clean::*;
 use crate::core::DocContext;
 use crate::fold::DocFolder;
 use crate::html::markdown::{find_testable_code, ErrorCodes, Ignore, LangString};
+use crate::visit_ast::inherits_doc_hidden;
 use rustc_middle::lint::LintLevelSource;
 use rustc_session::lint;
+use rustc_span::symbol::sym;
 
 crate const CHECK_PRIVATE_ITEMS_DOC_TESTS: Pass = Pass {
     name: "check-private-items-doc-tests",
@@ -68,6 +70,11 @@ crate fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> boo
         return false;
     }
     let hir_id = cx.tcx.hir().local_def_id_to_hir_id(item.def_id.expect_local());
+    if cx.tcx.hir().attrs(hir_id).lists(sym::doc).has_word(sym::hidden)
+        || inherits_doc_hidden(cx, hir_id)
+    {
+        return false;
+    }
     let (level, source) = cx.tcx.lint_level_at_node(crate::lint::MISSING_DOC_CODE_EXAMPLES, hir_id);
     level != lint::Level::Allow || matches!(source, LintLevelSource::Default)
 }
@@ -86,7 +93,9 @@ crate fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
     find_testable_code(&dox, &mut tests, ErrorCodes::No, false, None);
 
     if tests.found_tests == 0 && cx.tcx.sess.is_nightly_build() {
-        if should_have_doc_example(cx, &item) {
+        if cx.renderinfo.borrow().access_levels.is_public(item.def_id)
+            && should_have_doc_example(cx, &item)
+        {
             debug!("reporting error for {:?} (hir_id={:?})", item, hir_id);
             let sp = span_of_attrs(&item.attrs).unwrap_or(item.source.span());
             cx.tcx.struct_span_lint_hir(
diff --git a/src/librustdoc/visit_ast.rs b/src/librustdoc/visit_ast.rs
index 5da7d2f1e9b..b9b2be1db80 100644
--- a/src/librustdoc/visit_ast.rs
+++ b/src/librustdoc/visit_ast.rs
@@ -29,6 +29,19 @@ fn def_id_to_path(tcx: TyCtxt<'_>, did: DefId) -> Vec<String> {
     std::iter::once(crate_name).chain(relative).collect()
 }
 
+crate fn inherits_doc_hidden(cx: &core::DocContext<'_>, mut node: hir::HirId) -> bool {
+    while let Some(id) = cx.tcx.hir().get_enclosing_scope(node) {
+        node = id;
+        if cx.tcx.hir().attrs(node).lists(sym::doc).has_word(sym::hidden) {
+            return true;
+        }
+        if node == hir::CRATE_HIR_ID {
+            break;
+        }
+    }
+    false
+}
+
 // Also, is there some reason that this doesn't use the 'visit'
 // framework from syntax?.
 
@@ -158,19 +171,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
         om: &mut Module<'tcx>,
         please_inline: bool,
     ) -> bool {
-        fn inherits_doc_hidden(cx: &core::DocContext<'_>, mut node: hir::HirId) -> bool {
-            while let Some(id) = cx.tcx.hir().get_enclosing_scope(node) {
-                node = id;
-                if cx.tcx.hir().attrs(node).lists(sym::doc).has_word(sym::hidden) {
-                    return true;
-                }
-                if node == hir::CRATE_HIR_ID {
-                    break;
-                }
-            }
-            false
-        }
-
         debug!("maybe_inline_local res: {:?}", res);
 
         let tcx = self.cx.tcx;