Skip to content

Commit 4b30625

Browse files
Don't warn for missing_doc_examples when item is #[doc(hidden)]
1 parent 4099208 commit 4b30625

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/librustdoc/passes/doc_test_lints.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ use crate::clean::*;
99
use crate::core::DocContext;
1010
use crate::fold::DocFolder;
1111
use crate::html::markdown::{find_testable_code, ErrorCodes, Ignore, LangString};
12+
use crate::visit_ast::inherits_doc_hidden;
1213
use rustc_middle::lint::LintLevelSource;
1314
use rustc_session::lint;
15+
use rustc_span::symbol::sym;
1416

1517
crate const CHECK_PRIVATE_ITEMS_DOC_TESTS: Pass = Pass {
1618
name: "check-private-items-doc-tests",
@@ -68,6 +70,11 @@ crate fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> boo
6870
return false;
6971
}
7072
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(item.def_id.expect_local());
73+
if cx.tcx.hir().attrs(hir_id).lists(sym::doc).has_word(sym::hidden)
74+
|| inherits_doc_hidden(cx, hir_id)
75+
{
76+
return false;
77+
}
7178
let (level, source) = cx.tcx.lint_level_at_node(crate::lint::MISSING_DOC_CODE_EXAMPLES, hir_id);
7279
level != lint::Level::Allow || matches!(source, LintLevelSource::Default)
7380
}
@@ -86,7 +93,9 @@ crate fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
8693
find_testable_code(&dox, &mut tests, ErrorCodes::No, false, None);
8794

8895
if tests.found_tests == 0 && cx.tcx.sess.is_nightly_build() {
89-
if should_have_doc_example(cx, &item) {
96+
if cx.renderinfo.borrow().access_levels.is_public(item.def_id)
97+
&& should_have_doc_example(cx, &item)
98+
{
9099
debug!("reporting error for {:?} (hir_id={:?})", item, hir_id);
91100
let sp = span_of_attrs(&item.attrs).unwrap_or(item.source.span());
92101
cx.tcx.struct_span_lint_hir(

src/librustdoc/visit_ast.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ fn def_id_to_path(tcx: TyCtxt<'_>, did: DefId) -> Vec<String> {
2929
std::iter::once(crate_name).chain(relative).collect()
3030
}
3131

32+
crate fn inherits_doc_hidden(cx: &core::DocContext<'_>, mut node: hir::HirId) -> bool {
33+
while let Some(id) = cx.tcx.hir().get_enclosing_scope(node) {
34+
node = id;
35+
if cx.tcx.hir().attrs(node).lists(sym::doc).has_word(sym::hidden) {
36+
return true;
37+
}
38+
if node == hir::CRATE_HIR_ID {
39+
break;
40+
}
41+
}
42+
false
43+
}
44+
3245
// Also, is there some reason that this doesn't use the 'visit'
3346
// framework from syntax?.
3447

@@ -158,19 +171,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
158171
om: &mut Module<'tcx>,
159172
please_inline: bool,
160173
) -> bool {
161-
fn inherits_doc_hidden(cx: &core::DocContext<'_>, mut node: hir::HirId) -> bool {
162-
while let Some(id) = cx.tcx.hir().get_enclosing_scope(node) {
163-
node = id;
164-
if cx.tcx.hir().attrs(node).lists(sym::doc).has_word(sym::hidden) {
165-
return true;
166-
}
167-
if node == hir::CRATE_HIR_ID {
168-
break;
169-
}
170-
}
171-
false
172-
}
173-
174174
debug!("maybe_inline_local res: {:?}", res);
175175

176176
let tcx = self.cx.tcx;

0 commit comments

Comments
 (0)