Skip to content

Commit

Permalink
Rollup merge of rust-lang#76716 - GuillaumeGomez:stop-complains-on-do…
Browse files Browse the repository at this point in the history
…c-hidden, r=jyn514

Don't warn for `missing_doc_examples` when item is #[doc(hidden)]

r? ``@jyn514``
  • Loading branch information
Dylan-DPC authored Mar 5, 2021
2 parents 64c220a + 1683cb1 commit adb52a3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 33 deletions.
37 changes: 23 additions & 14 deletions src/librustdoc/passes/doc_test_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -51,23 +53,30 @@ impl crate::doctest::Tester for Tests {
}

crate fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> bool {
if matches!(
*item.kind,
clean::StructFieldItem(_)
| clean::VariantItem(_)
| clean::AssocConstItem(_, _)
| clean::AssocTypeItem(_, _)
| clean::TypedefItem(_, _)
| clean::StaticItem(_)
| clean::ConstantItem(_)
| clean::ExternCrateItem(_, _)
| clean::ImportItem(_)
| clean::PrimitiveItem(_)
| clean::KeywordItem(_)
) {
if !cx.cache.access_levels.is_public(item.def_id)
|| matches!(
*item.kind,
clean::StructFieldItem(_)
| clean::VariantItem(_)
| clean::AssocConstItem(_, _)
| clean::AssocTypeItem(_, _)
| clean::TypedefItem(_, _)
| clean::StaticItem(_)
| clean::ConstantItem(_)
| clean::ExternCrateItem(_, _)
| clean::ImportItem(_)
| clean::PrimitiveItem(_)
| clean::KeywordItem(_)
)
{
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.tcx, 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)
}
Expand Down
25 changes: 11 additions & 14 deletions src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ fn def_id_to_path(tcx: TyCtxt<'_>, did: DefId) -> Vec<String> {
std::iter::once(crate_name).chain(relative).collect()
}

crate fn inherits_doc_hidden(tcx: TyCtxt<'_>, mut node: hir::HirId) -> bool {
while let Some(id) = tcx.hir().get_enclosing_scope(node) {
node = id;
if tcx.hir().attrs(node).lists(sym::doc).has_word(sym::hidden) {
return true;
}
}
false
}

// Also, is there some reason that this doesn't use the 'visit'
// framework from syntax?.

Expand Down Expand Up @@ -158,19 +168,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;
Expand Down Expand Up @@ -212,7 +209,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
};

let is_private = !self.cx.cache.access_levels.is_public(res_did);
let is_hidden = inherits_doc_hidden(self.cx, res_hir_id);
let is_hidden = inherits_doc_hidden(self.cx.tcx, res_hir_id);

// Only inline if requested or if the item would otherwise be stripped.
if (!please_inline && !is_private && !is_hidden) || is_no_inline {
Expand Down
21 changes: 17 additions & 4 deletions src/test/rustdoc-ui/lint-missing-doc-code-example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
/// ```
/// println!("hello");
/// ```
fn test() {
pub fn test() {
}

#[allow(missing_docs)]
mod module1 { //~ ERROR
pub mod module1 { //~ ERROR
}

#[allow(rustdoc::missing_doc_code_examples)]
/// doc
mod module2 {
pub mod module2 {

/// doc
pub fn test() {}
Expand Down Expand Up @@ -63,9 +63,22 @@ pub enum Enum {
/// Doc
//~^ ERROR
#[repr(C)]
union Union {
pub union Union {
/// Doc, but no code example and it's fine!
a: i32,
/// Doc, but no code example and it's fine!
b: f32,
}


#[doc(hidden)]
pub mod foo {
pub fn bar() {}
}

fn babar() {}


mod fofoo {
pub fn tadam() {}
}
2 changes: 1 addition & 1 deletion src/test/rustdoc-ui/lint-missing-doc-code-example.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
error: missing code example in this documentation
--> $DIR/lint-missing-doc-code-example.rs:19:1
|
LL | / mod module1 {
LL | / pub mod module1 {
LL | | }
| |_^
|
Expand Down

0 comments on commit adb52a3

Please sign in to comment.