2021-12-15 18:59:00 -08:00
|
|
|
//! Looks for items missing (or incorrectly having) doctests.
|
|
|
|
|
//!
|
2020-07-27 23:03:56 -04:00
|
|
|
//! This pass is overloaded and runs two different lints.
|
|
|
|
|
//!
|
2021-10-02 14:07:08 -07:00
|
|
|
//! - MISSING_DOC_CODE_EXAMPLES: this lint is **UNSTABLE** and looks for public items missing doctests.
|
2021-02-04 13:19:01 -08:00
|
|
|
//! - PRIVATE_DOC_TESTS: this lint is **STABLE** and looks for private items with doctests.
|
2020-07-27 23:03:56 -04:00
|
|
|
|
2021-03-24 01:49:12 -04:00
|
|
|
use super::Pass;
|
2020-08-21 15:33:34 +02:00
|
|
|
use crate::clean;
|
2023-07-27 15:22:50 +02:00
|
|
|
use crate::clean::utils::inherits_doc_hidden;
|
2019-02-23 16:40:07 +09:00
|
|
|
use crate::clean::*;
|
|
|
|
|
use crate::core::DocContext;
|
2020-09-12 14:30:27 +02:00
|
|
|
use crate::html::markdown::{find_testable_code, ErrorCodes, Ignore, LangString};
|
2021-10-31 21:24:11 -07:00
|
|
|
use crate::visit::DocVisitor;
|
2021-09-08 16:03:15 +02:00
|
|
|
use rustc_hir as hir;
|
2020-12-21 14:17:53 -08:00
|
|
|
use rustc_middle::lint::LintLevelSource;
|
2020-07-27 23:03:56 -04:00
|
|
|
use rustc_session::lint;
|
2018-10-26 00:55:12 +02:00
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) const CHECK_DOC_TEST_VISIBILITY: Pass = Pass {
|
2021-10-02 14:07:08 -07:00
|
|
|
name: "check_doc_test_visibility",
|
|
|
|
|
run: check_doc_test_visibility,
|
|
|
|
|
description: "run various visibility-related lints on doctests",
|
2019-02-23 15:10:56 -05:00
|
|
|
};
|
2018-10-26 00:55:12 +02:00
|
|
|
|
2021-10-02 14:07:08 -07:00
|
|
|
struct DocTestVisibilityLinter<'a, 'tcx> {
|
2021-02-12 01:11:32 -05:00
|
|
|
cx: &'a mut DocContext<'tcx>,
|
2018-10-26 00:55:12 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) fn check_doc_test_visibility(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
|
2021-10-02 14:07:08 -07:00
|
|
|
let mut coll = DocTestVisibilityLinter { cx };
|
2021-10-31 21:24:11 -07:00
|
|
|
coll.visit_crate(&krate);
|
|
|
|
|
krate
|
2018-10-26 00:55:12 +02:00
|
|
|
}
|
|
|
|
|
|
2021-10-31 21:24:11 -07:00
|
|
|
impl<'a, 'tcx> DocVisitor for DocTestVisibilityLinter<'a, 'tcx> {
|
|
|
|
|
fn visit_item(&mut self, item: &Item) {
|
2023-05-12 20:34:24 +03:00
|
|
|
look_for_tests(self.cx, &item.doc_value(), item);
|
2018-10-26 00:55:12 +02:00
|
|
|
|
2021-10-31 21:24:11 -07:00
|
|
|
self.visit_item_recur(item)
|
2018-10-26 00:55:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-07-27 23:03:56 -04:00
|
|
|
|
2020-08-18 13:31:23 +02:00
|
|
|
pub(crate) struct Tests {
|
|
|
|
|
pub(crate) found_tests: usize,
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-27 18:20:15 +02:00
|
|
|
impl crate::doctest::Tester for Tests {
|
2020-09-12 14:30:27 +02:00
|
|
|
fn add_test(&mut self, _: String, config: LangString, _: usize) {
|
|
|
|
|
if config.rust && config.ignore == Ignore::None {
|
|
|
|
|
self.found_tests += 1;
|
|
|
|
|
}
|
2020-08-18 13:31:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> bool {
|
2022-10-19 22:37:59 +04:00
|
|
|
if !cx.cache.effective_visibilities.is_directly_public(cx.tcx, item.item_id.expect_def_id())
|
2021-02-15 14:17:43 +01:00
|
|
|
|| matches!(
|
|
|
|
|
*item.kind,
|
|
|
|
|
clean::StructFieldItem(_)
|
|
|
|
|
| clean::VariantItem(_)
|
2022-03-29 19:30:54 +02:00
|
|
|
| clean::AssocConstItem(..)
|
2022-02-14 18:14:38 -08:00
|
|
|
| clean::AssocTypeItem(..)
|
2023-08-21 13:56:22 -07:00
|
|
|
| clean::TypeAliasItem(_)
|
2021-02-15 14:17:43 +01:00
|
|
|
| clean::StaticItem(_)
|
|
|
|
|
| clean::ConstantItem(_)
|
2021-03-05 16:04:24 +01:00
|
|
|
| clean::ExternCrateItem { .. }
|
2021-02-15 14:17:43 +01:00
|
|
|
| clean::ImportItem(_)
|
|
|
|
|
| clean::PrimitiveItem(_)
|
2022-07-21 16:05:17 +02:00
|
|
|
| clean::KeywordItem
|
2021-09-08 16:03:15 +02:00
|
|
|
// check for trait impl
|
2022-07-21 23:14:12 +02:00
|
|
|
| clean::ImplItem(box clean::Impl { trait_: Some(_), .. })
|
2021-02-15 14:17:43 +01:00
|
|
|
)
|
|
|
|
|
{
|
2020-10-08 18:20:00 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2021-09-08 16:03:15 +02:00
|
|
|
|
2021-06-26 17:10:52 +02:00
|
|
|
// The `expect_def_id()` should be okay because `local_def_id_to_hir_id`
|
2021-05-08 15:03:15 -07:00
|
|
|
// would presumably panic if a fake `DefIndex` were passed.
|
2023-01-26 14:30:28 +04:00
|
|
|
let def_id = item.item_id.expect_def_id().expect_local();
|
2021-09-08 16:03:15 +02:00
|
|
|
|
|
|
|
|
// check if parent is trait impl
|
2023-11-13 08:24:55 -05:00
|
|
|
if let Some(parent_def_id) = cx.tcx.opt_local_parent(def_id)
|
2023-12-01 05:28:34 -08:00
|
|
|
&& let Some(parent_node) = cx.tcx.opt_hir_node_by_def_id(parent_def_id)
|
2023-11-13 08:24:55 -05:00
|
|
|
&& matches!(
|
2023-02-15 11:30:14 +01:00
|
|
|
parent_node,
|
|
|
|
|
hir::Node::Item(hir::Item {
|
|
|
|
|
kind: hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }),
|
|
|
|
|
..
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
2021-09-08 16:03:15 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-14 16:44:41 +02:00
|
|
|
if (!cx.render_options.document_hidden
|
|
|
|
|
&& (cx.tcx.is_doc_hidden(def_id.to_def_id()) || inherits_doc_hidden(cx.tcx, def_id, None)))
|
2023-01-26 14:30:28 +04:00
|
|
|
|| cx.tcx.def_span(def_id.to_def_id()).in_derive_expansion()
|
2021-02-13 21:44:42 +01:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-01-26 14:30:28 +04:00
|
|
|
let (level, source) = cx.tcx.lint_level_at_node(
|
|
|
|
|
crate::lint::MISSING_DOC_CODE_EXAMPLES,
|
2023-11-24 19:28:19 +03:00
|
|
|
cx.tcx.local_def_id_to_hir_id(def_id),
|
2023-01-26 14:30:28 +04:00
|
|
|
);
|
2020-12-21 14:17:53 -08:00
|
|
|
level != lint::Level::Allow || matches!(source, LintLevelSource::Default)
|
2020-08-21 15:33:34 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-20 21:06:44 -04:00
|
|
|
pub(crate) fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
|
2022-04-16 14:28:09 +02:00
|
|
|
let Some(hir_id) = DocContext::as_local_hir_id(cx.tcx, item.item_id) else {
|
2022-02-19 00:43:47 +01:00
|
|
|
// If non-local, no need to check anything.
|
|
|
|
|
return;
|
2020-07-27 23:03:56 -04:00
|
|
|
};
|
|
|
|
|
|
2020-09-12 14:30:27 +02:00
|
|
|
let mut tests = Tests { found_tests: 0 };
|
2020-07-27 23:03:56 -04:00
|
|
|
|
2023-09-17 14:52:45 +02:00
|
|
|
find_testable_code(
|
|
|
|
|
dox,
|
|
|
|
|
&mut tests,
|
|
|
|
|
ErrorCodes::No,
|
|
|
|
|
false,
|
|
|
|
|
None,
|
|
|
|
|
cx.tcx.features().custom_code_classes_in_docs,
|
|
|
|
|
);
|
2020-07-27 23:03:56 -04:00
|
|
|
|
2022-09-12 20:10:35 +02:00
|
|
|
if tests.found_tests == 0 && cx.tcx.features().rustdoc_missing_doc_code_examples {
|
2021-10-01 17:12:39 +02:00
|
|
|
if should_have_doc_example(cx, item) {
|
2023-08-14 22:25:32 +02:00
|
|
|
debug!("reporting error for {item:?} (hir_id={hir_id:?})");
|
2021-03-24 01:49:12 -04:00
|
|
|
let sp = item.attr_span(cx.tcx);
|
2020-07-27 23:03:56 -04:00
|
|
|
cx.tcx.struct_span_lint_hir(
|
2020-12-30 14:11:15 -05:00
|
|
|
crate::lint::MISSING_DOC_CODE_EXAMPLES,
|
2020-07-27 23:03:56 -04:00
|
|
|
hir_id,
|
|
|
|
|
sp,
|
2022-09-16 19:07:20 +04:00
|
|
|
"missing code example in this documentation",
|
2023-12-08 01:52:56 +00:00
|
|
|
|_| {},
|
2020-07-27 23:03:56 -04:00
|
|
|
);
|
|
|
|
|
}
|
2021-06-26 17:10:52 +02:00
|
|
|
} else if tests.found_tests > 0
|
2022-10-19 22:37:59 +04:00
|
|
|
&& !cx.cache.effective_visibilities.is_exported(cx.tcx, item.item_id.expect_def_id())
|
2021-04-29 21:36:54 +02:00
|
|
|
{
|
2020-07-27 23:03:56 -04:00
|
|
|
cx.tcx.struct_span_lint_hir(
|
2020-12-30 14:11:15 -05:00
|
|
|
crate::lint::PRIVATE_DOC_TESTS,
|
2020-07-27 23:03:56 -04:00
|
|
|
hir_id,
|
2021-03-24 01:49:12 -04:00
|
|
|
item.attr_span(cx.tcx),
|
2022-09-16 19:07:20 +04:00
|
|
|
"documentation test in private item",
|
2023-12-08 01:52:56 +00:00
|
|
|
|_| {},
|
2020-07-27 23:03:56 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|