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;
|
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-02-13 21:44:42 +01:00
|
|
|
use crate::visit_ast::inherits_doc_hidden;
|
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;
|
2021-02-13 21:44:42 +01:00
|
|
|
use rustc_span::symbol::sym;
|
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) {
|
2022-05-21 14:07:18 -04:00
|
|
|
let dox = item.attrs.collapsed_doc_value().unwrap_or_default();
|
2018-10-26 00:55:12 +02:00
|
|
|
|
2022-05-21 14:07:18 -04:00
|
|
|
look_for_tests(self.cx, &dox, 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-04-16 14:28:09 +02:00
|
|
|
if !cx.cache.access_levels.is_public(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(..)
|
2022-03-29 19:30:54 +02:00
|
|
|
| clean::TypedefItem(_)
|
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
|
|
|
|
|
| clean::ImplItem(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.
|
2022-04-16 14:28:09 +02:00
|
|
|
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(item.item_id.expect_def_id().expect_local());
|
2021-09-08 16:03:15 +02:00
|
|
|
|
|
|
|
|
// check if parent is trait impl
|
|
|
|
|
if let Some(parent_hir_id) = cx.tcx.hir().find_parent_node(hir_id) {
|
|
|
|
|
if let Some(parent_node) = cx.tcx.hir().find(parent_hir_id) {
|
|
|
|
|
if matches!(
|
|
|
|
|
parent_node,
|
|
|
|
|
hir::Node::Item(hir::Item {
|
|
|
|
|
kind: hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }),
|
|
|
|
|
..
|
|
|
|
|
})
|
|
|
|
|
) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-13 21:44:42 +01:00
|
|
|
if cx.tcx.hir().attrs(hir_id).lists(sym::doc).has_word(sym::hidden)
|
2021-02-23 15:19:13 +01:00
|
|
|
|| inherits_doc_hidden(cx.tcx, hir_id)
|
2021-09-08 01:09:15 +02:00
|
|
|
|| cx.tcx.hir().span(hir_id).in_derive_expansion()
|
2021-02-13 21:44:42 +01:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-12-30 14:11:15 -05:00
|
|
|
let (level, source) = cx.tcx.lint_level_at_node(crate::lint::MISSING_DOC_CODE_EXAMPLES, hir_id);
|
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)
|
2022-02-19 00:43:47 +01:00
|
|
|
else {
|
|
|
|
|
// 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
|
|
|
|
2021-10-01 17:12:39 +02:00
|
|
|
find_testable_code(dox, &mut tests, ErrorCodes::No, false, None);
|
2020-07-27 23:03:56 -04:00
|
|
|
|
2020-10-10 14:27:52 -04:00
|
|
|
if tests.found_tests == 0 && cx.tcx.sess.is_nightly_build() {
|
2021-10-01 17:12:39 +02:00
|
|
|
if should_have_doc_example(cx, item) {
|
2020-07-27 23:03:56 -04:00
|
|
|
debug!("reporting error for {:?} (hir_id={:?})", item, 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-01-22 18:49:12 -06:00
|
|
|
|lint| {
|
|
|
|
|
lint.build("missing code example in this documentation").emit();
|
|
|
|
|
},
|
2020-07-27 23:03:56 -04:00
|
|
|
);
|
|
|
|
|
}
|
2021-06-26 17:10:52 +02:00
|
|
|
} else if tests.found_tests > 0
|
2022-04-16 14:28:09 +02:00
|
|
|
&& !cx.cache.access_levels.is_exported(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-01-22 18:49:12 -06:00
|
|
|
|lint| {
|
|
|
|
|
lint.build("documentation test in private item").emit();
|
|
|
|
|
},
|
2020-07-27 23:03:56 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|