1 //! This pass is overloaded and runs two different lints.
3 //! - MISSING_DOC_CODE_EXAMPLES: this lint is **UNSTABLE** and looks for public items missing doctests
4 //! - PRIVATE_DOC_TESTS: this lint is **STABLE** and looks for private items with doctests.
6 use super::{span_of_attrs, Pass};
9 use crate::core::DocContext;
10 use crate::fold::DocFolder;
11 use crate::html::markdown::{find_testable_code, ErrorCodes, Ignore, LangString};
12 use rustc_middle::lint::LintLevelSource;
13 use rustc_session::lint;
15 crate const CHECK_PRIVATE_ITEMS_DOC_TESTS: Pass = Pass {
16 name: "check-private-items-doc-tests",
17 run: check_private_items_doc_tests,
18 description: "check private items doc tests",
21 struct PrivateItemDocTestLinter<'a, 'tcx> {
22 cx: &'a mut DocContext<'tcx>,
25 crate fn check_private_items_doc_tests(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
26 let mut coll = PrivateItemDocTestLinter { cx };
28 coll.fold_crate(krate)
31 impl<'a, 'tcx> DocFolder for PrivateItemDocTestLinter<'a, 'tcx> {
32 fn fold_item(&mut self, item: Item) -> Option<Item> {
33 let dox = item.attrs.collapsed_doc_value().unwrap_or_else(String::new);
35 look_for_tests(self.cx, &dox, &item);
37 Some(self.fold_item_recur(item))
41 pub(crate) struct Tests {
42 pub(crate) found_tests: usize,
45 impl crate::doctest::Tester for Tests {
46 fn add_test(&mut self, _: String, config: LangString, _: usize) {
47 if config.rust && config.ignore == Ignore::None {
48 self.found_tests += 1;
53 crate fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> bool {
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(_)
70 let hir_id = cx.tcx.hir().local_def_id_to_hir_id(item.def_id.expect_local());
72 cx.tcx.lint_level_at_node(lint::builtin::MISSING_DOC_CODE_EXAMPLES, hir_id);
73 level != lint::Level::Allow || matches!(source, LintLevelSource::Default)
76 crate fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
77 let hir_id = match cx.as_local_hir_id(item.def_id) {
78 Some(hir_id) => hir_id,
80 // If non-local, no need to check anything.
85 let mut tests = Tests { found_tests: 0 };
87 find_testable_code(&dox, &mut tests, ErrorCodes::No, false, None);
89 if tests.found_tests == 0 && cx.tcx.sess.is_nightly_build() {
90 if should_have_doc_example(cx, &item) {
91 debug!("reporting error for {:?} (hir_id={:?})", item, hir_id);
92 let sp = span_of_attrs(&item.attrs).unwrap_or(item.source.span());
93 cx.tcx.struct_span_lint_hir(
94 lint::builtin::MISSING_DOC_CODE_EXAMPLES,
97 |lint| lint.build("missing code example in this documentation").emit(),
100 } else if tests.found_tests > 0 && !cx.renderinfo.access_levels.is_public(item.def_id) {
101 cx.tcx.struct_span_lint_hir(
102 lint::builtin::PRIVATE_DOC_TESTS,
104 span_of_attrs(&item.attrs).unwrap_or(item.source.span()),
105 |lint| lint.build("documentation test in private item").emit(),