Skip to content

Commit 95bbc7e

Browse files
Rollup merge of #76716 - GuillaumeGomez:stop-complains-on-doc-hidden, r=jyn514
Don't warn for `missing_doc_examples` when item is #[doc(hidden)] r? `@jyn514`
2 parents 4099208 + 1683cb1 commit 95bbc7e

File tree

4 files changed

+52
-33
lines changed

4 files changed

+52
-33
lines changed

src/librustdoc/passes/doc_test_lints.rs

+23-14
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",
@@ -51,23 +53,30 @@ impl crate::doctest::Tester for Tests {
5153
}
5254

5355
crate fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> bool {
54-
if matches!(
55-
*item.kind,
56-
clean::StructFieldItem(_)
57-
| clean::VariantItem(_)
58-
| clean::AssocConstItem(_, _)
59-
| clean::AssocTypeItem(_, _)
60-
| clean::TypedefItem(_, _)
61-
| clean::StaticItem(_)
62-
| clean::ConstantItem(_)
63-
| clean::ExternCrateItem(_, _)
64-
| clean::ImportItem(_)
65-
| clean::PrimitiveItem(_)
66-
| clean::KeywordItem(_)
67-
) {
56+
if !cx.cache.access_levels.is_public(item.def_id)
57+
|| matches!(
58+
*item.kind,
59+
clean::StructFieldItem(_)
60+
| clean::VariantItem(_)
61+
| clean::AssocConstItem(_, _)
62+
| clean::AssocTypeItem(_, _)
63+
| clean::TypedefItem(_, _)
64+
| clean::StaticItem(_)
65+
| clean::ConstantItem(_)
66+
| clean::ExternCrateItem(_, _)
67+
| clean::ImportItem(_)
68+
| clean::PrimitiveItem(_)
69+
| clean::KeywordItem(_)
70+
)
71+
{
6872
return false;
6973
}
7074
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(item.def_id.expect_local());
75+
if cx.tcx.hir().attrs(hir_id).lists(sym::doc).has_word(sym::hidden)
76+
|| inherits_doc_hidden(cx.tcx, hir_id)
77+
{
78+
return false;
79+
}
7180
let (level, source) = cx.tcx.lint_level_at_node(crate::lint::MISSING_DOC_CODE_EXAMPLES, hir_id);
7281
level != lint::Level::Allow || matches!(source, LintLevelSource::Default)
7382
}

src/librustdoc/visit_ast.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ 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(tcx: TyCtxt<'_>, mut node: hir::HirId) -> bool {
33+
while let Some(id) = tcx.hir().get_enclosing_scope(node) {
34+
node = id;
35+
if tcx.hir().attrs(node).lists(sym::doc).has_word(sym::hidden) {
36+
return true;
37+
}
38+
}
39+
false
40+
}
41+
3242
// Also, is there some reason that this doesn't use the 'visit'
3343
// framework from syntax?.
3444

@@ -158,19 +168,6 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
158168
om: &mut Module<'tcx>,
159169
please_inline: bool,
160170
) -> 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-
174171
debug!("maybe_inline_local res: {:?}", res);
175172

176173
let tcx = self.cx.tcx;
@@ -212,7 +209,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
212209
};
213210

214211
let is_private = !self.cx.cache.access_levels.is_public(res_did);
215-
let is_hidden = inherits_doc_hidden(self.cx, res_hir_id);
212+
let is_hidden = inherits_doc_hidden(self.cx.tcx, res_hir_id);
216213

217214
// Only inline if requested or if the item would otherwise be stripped.
218215
if (!please_inline && !is_private && !is_hidden) || is_no_inline {

src/test/rustdoc-ui/lint-missing-doc-code-example.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@
1212
/// ```
1313
/// println!("hello");
1414
/// ```
15-
fn test() {
15+
pub fn test() {
1616
}
1717

1818
#[allow(missing_docs)]
19-
mod module1 { //~ ERROR
19+
pub mod module1 { //~ ERROR
2020
}
2121

2222
#[allow(rustdoc::missing_doc_code_examples)]
2323
/// doc
24-
mod module2 {
24+
pub mod module2 {
2525

2626
/// doc
2727
pub fn test() {}
@@ -63,9 +63,22 @@ pub enum Enum {
6363
/// Doc
6464
//~^ ERROR
6565
#[repr(C)]
66-
union Union {
66+
pub union Union {
6767
/// Doc, but no code example and it's fine!
6868
a: i32,
6969
/// Doc, but no code example and it's fine!
7070
b: f32,
7171
}
72+
73+
74+
#[doc(hidden)]
75+
pub mod foo {
76+
pub fn bar() {}
77+
}
78+
79+
fn babar() {}
80+
81+
82+
mod fofoo {
83+
pub fn tadam() {}
84+
}

src/test/rustdoc-ui/lint-missing-doc-code-example.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
error: missing code example in this documentation
22
--> $DIR/lint-missing-doc-code-example.rs:19:1
33
|
4-
LL | / mod module1 {
4+
LL | / pub mod module1 {
55
LL | | }
66
| |_^
77
|

0 commit comments

Comments
 (0)