Skip to content

Commit 22465b3

Browse files
Apply same treatment to MISSING_DOC_CODE_EXAMPLES
1 parent b31f5d0 commit 22465b3

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/librustdoc/passes/calculate_doc_coverage.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,13 @@ impl<'a, 'b> fold::DocFolder for CoverageCalculator<'a, 'b> {
254254
let has_doc_example = tests.found_tests != 0;
255255
let hir_id = self.ctx.tcx.hir().local_def_id_to_hir_id(i.def_id.expect_local());
256256
let (level, source) = self.ctx.tcx.lint_level_at_node(MISSING_DOCS, hir_id);
257-
let should_have_docs = level != lint::Level::Allow || !matches!(source, LintSource::Node(..));
257+
let should_have_docs =
258+
level != lint::Level::Allow || !matches!(source, LintSource::Node(..));
258259
debug!("counting {:?} {:?} in {}", i.type_(), i.name, i.source.filename);
259260
self.items.entry(i.source.filename.clone()).or_default().count_item(
260261
has_docs,
261262
has_doc_example,
262-
should_have_doc_example(&i.inner),
263+
should_have_doc_example(self.ctx, &i),
263264
should_have_docs,
264265
);
265266
}

src/librustdoc/passes/doc_test_lints.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ 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 rustc_middle::lint::LintSource;
1213
use rustc_session::lint;
1314

1415
pub const CHECK_PRIVATE_ITEMS_DOC_TESTS: Pass = Pass {
@@ -56,8 +57,8 @@ impl crate::doctest::Tester for Tests {
5657
}
5758
}
5859

59-
pub fn should_have_doc_example(item_kind: &clean::ItemEnum) -> bool {
60-
!matches!(item_kind,
60+
pub fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> bool {
61+
if matches!(item.inner,
6162
clean::StructFieldItem(_)
6263
| clean::VariantItem(_)
6364
| clean::AssocConstItem(_, _)
@@ -69,7 +70,13 @@ pub fn should_have_doc_example(item_kind: &clean::ItemEnum) -> bool {
6970
| clean::ImportItem(_)
7071
| clean::PrimitiveItem(_)
7172
| clean::KeywordItem(_)
72-
)
73+
) {
74+
return false;
75+
}
76+
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(item.def_id.expect_local());
77+
let (level, source) =
78+
cx.tcx.lint_level_at_node(lint::builtin::MISSING_DOC_CODE_EXAMPLES, hir_id);
79+
level != lint::Level::Allow || !matches!(source, LintSource::Node(..))
7380
}
7481

7582
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) {
8895
if tests.found_tests == 0
8996
&& rustc_feature::UnstableFeatures::from_environment().is_nightly_build()
9097
{
91-
if should_have_doc_example(&item.inner) {
98+
if should_have_doc_example(cx, &item) {
9299
debug!("reporting error for {:?} (hir_id={:?})", item, hir_id);
93100
let sp = span_of_attrs(&item.attrs).unwrap_or(item.source.span());
94101
cx.tcx.struct_span_lint_hir(
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
warning: missing documentation for a struct
2+
--> $DIR/allow_missing_docs.rs:36:5
3+
|
4+
LL | pub struct Bar {
5+
| ^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/allow_missing_docs.rs:35:12
9+
|
10+
LL | #[warn(missing_docs)]
11+
| ^^^^^^^^^^^^
12+
13+
warning: missing documentation for a struct field
14+
--> $DIR/allow_missing_docs.rs:37:9
15+
|
16+
LL | pub f: u32,
17+
| ^^^^^^^^^^
18+
19+
warning: 2 warnings emitted
20+

0 commit comments

Comments
 (0)