Skip to content

Commit dadde88

Browse files
Unify checks for lint missing_doc_code_examples and --show-coverage
1 parent adeedf5 commit dadde88

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

src/librustdoc/passes/calculate_doc_coverage.rs

+2-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::config::OutputFormat;
33
use crate::core::DocContext;
44
use crate::fold::{self, DocFolder};
55
use crate::html::markdown::{find_testable_code, ErrorCodes};
6-
use crate::passes::doc_test_lints::Tests;
6+
use crate::passes::doc_test_lints::{should_have_doc_example, Tests};
77
use crate::passes::Pass;
88
use rustc_span::symbol::sym;
99
use rustc_span::FileName;
@@ -231,19 +231,6 @@ impl fold::DocFolder for CoverageCalculator {
231231
let has_docs = !i.attrs.doc_strings.is_empty();
232232
let mut tests = Tests { found_tests: 0 };
233233

234-
let should_have_doc_examples = !matches!(i.inner,
235-
clean::StructFieldItem(_)
236-
| clean::VariantItem(_)
237-
| clean::AssocConstItem(_, _)
238-
| clean::AssocTypeItem(_, _)
239-
| clean::TypedefItem(_, _)
240-
| clean::StaticItem(_)
241-
| clean::ConstantItem(_)
242-
| clean::ExternCrateItem(_, _)
243-
| clean::ImportItem(_)
244-
| clean::PrimitiveItem(_)
245-
| clean::KeywordItem(_)
246-
);
247234
find_testable_code(
248235
&i.attrs.doc_strings.iter().map(|d| d.as_str()).collect::<Vec<_>>().join("\n"),
249236
&mut tests,
@@ -257,7 +244,7 @@ impl fold::DocFolder for CoverageCalculator {
257244
self.items.entry(i.source.filename.clone()).or_default().count_item(
258245
has_docs,
259246
has_doc_example,
260-
should_have_doc_examples,
247+
should_have_doc_example(&i.inner),
261248
);
262249
}
263250
}

src/librustdoc/passes/doc_test_lints.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//! - PRIVATE_DOC_TESTS: this looks for private items with doc-tests.
55
66
use super::{span_of_attrs, Pass};
7+
use crate::clean;
78
use crate::clean::*;
89
use crate::core::DocContext;
910
use crate::fold::DocFolder;
@@ -59,6 +60,22 @@ impl crate::test::Tester for Tests {
5960
}
6061
}
6162

63+
pub fn should_have_doc_example(item_kind: &clean::ItemEnum) -> bool {
64+
!matches!(item_kind,
65+
clean::StructFieldItem(_)
66+
| clean::VariantItem(_)
67+
| clean::AssocConstItem(_, _)
68+
| clean::AssocTypeItem(_, _)
69+
| clean::TypedefItem(_, _)
70+
| clean::StaticItem(_)
71+
| clean::ConstantItem(_)
72+
| clean::ExternCrateItem(_, _)
73+
| clean::ImportItem(_)
74+
| clean::PrimitiveItem(_)
75+
| clean::KeywordItem(_)
76+
)
77+
}
78+
6279
pub fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
6380
let hir_id = match cx.as_local_hir_id(item.def_id) {
6481
Some(hir_id) => hir_id,
@@ -73,13 +90,7 @@ pub fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
7390
find_testable_code(&dox, &mut tests, ErrorCodes::No, false, None);
7491

7592
if tests.found_tests == 0 {
76-
use ItemEnum::*;
77-
78-
let should_report = match item.inner {
79-
ExternCrateItem(_, _) | ImportItem(_) | PrimitiveItem(_) | KeywordItem(_) => false,
80-
_ => true,
81-
};
82-
if should_report {
93+
if should_have_doc_example(&item.inner) {
8394
debug!("reporting error for {:?} (hir_id={:?})", item, hir_id);
8495
let sp = span_of_attrs(&item.attrs).unwrap_or(item.source.span());
8596
cx.tcx.struct_span_lint_hir(

0 commit comments

Comments
 (0)