Apply same treatment to MISSING_DOC_CODE_EXAMPLES

This commit is contained in:
Guillaume Gomez 2020-10-08 18:20:00 +02:00
parent b31f5d05b1
commit 22465b35a6
3 changed files with 34 additions and 6 deletions

View File

@ -254,12 +254,13 @@ impl<'a, 'b> fold::DocFolder for CoverageCalculator<'a, 'b> {
let has_doc_example = tests.found_tests != 0;
let hir_id = self.ctx.tcx.hir().local_def_id_to_hir_id(i.def_id.expect_local());
let (level, source) = self.ctx.tcx.lint_level_at_node(MISSING_DOCS, hir_id);
let should_have_docs = level != lint::Level::Allow || !matches!(source, LintSource::Node(..));
let should_have_docs =
level != lint::Level::Allow || !matches!(source, LintSource::Node(..));
debug!("counting {:?} {:?} in {}", i.type_(), i.name, i.source.filename);
self.items.entry(i.source.filename.clone()).or_default().count_item(
has_docs,
has_doc_example,
should_have_doc_example(&i.inner),
should_have_doc_example(self.ctx, &i),
should_have_docs,
);
}

View File

@ -9,6 +9,7 @@ use crate::clean::*;
use crate::core::DocContext;
use crate::fold::DocFolder;
use crate::html::markdown::{find_testable_code, ErrorCodes, Ignore, LangString};
use rustc_middle::lint::LintSource;
use rustc_session::lint;
pub const CHECK_PRIVATE_ITEMS_DOC_TESTS: Pass = Pass {
@ -56,8 +57,8 @@ impl crate::doctest::Tester for Tests {
}
}
pub fn should_have_doc_example(item_kind: &clean::ItemEnum) -> bool {
!matches!(item_kind,
pub fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> bool {
if matches!(item.inner,
clean::StructFieldItem(_)
| clean::VariantItem(_)
| clean::AssocConstItem(_, _)
@ -69,7 +70,13 @@ pub fn should_have_doc_example(item_kind: &clean::ItemEnum) -> bool {
| 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());
let (level, source) =
cx.tcx.lint_level_at_node(lint::builtin::MISSING_DOC_CODE_EXAMPLES, hir_id);
level != lint::Level::Allow || !matches!(source, LintSource::Node(..))
}
pub fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
@ -88,7 +95,7 @@ pub fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
if tests.found_tests == 0
&& rustc_feature::UnstableFeatures::from_environment().is_nightly_build()
{
if should_have_doc_example(&item.inner) {
if 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(

View File

@ -0,0 +1,20 @@
warning: missing documentation for a struct
--> $DIR/allow_missing_docs.rs:36:5
|
LL | pub struct Bar {
| ^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/allow_missing_docs.rs:35:12
|
LL | #[warn(missing_docs)]
| ^^^^^^^^^^^^
warning: missing documentation for a struct field
--> $DIR/allow_missing_docs.rs:37:9
|
LL | pub f: u32,
| ^^^^^^^^^^
warning: 2 warnings emitted