2020-07-27 23:03:56 -04:00
|
|
|
//! This pass is overloaded and runs two different lints.
|
|
|
|
|
//!
|
2020-08-26 15:13:57 -04:00
|
|
|
//! - MISSING_DOC_CODE_EXAMPLES: this lint is **UNSTABLE** and looks for public items missing doc-tests
|
|
|
|
|
//! - PRIVATE_DOC_TESTS: this lint is **STABLE** and looks for private items with doc-tests.
|
2020-07-27 23:03:56 -04:00
|
|
|
|
2020-07-27 23:05:01 -04:00
|
|
|
use super::{span_of_attrs, 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;
|
|
|
|
|
use crate::fold::DocFolder;
|
2020-07-27 23:03:56 -04:00
|
|
|
use crate::html::markdown::{find_testable_code, ErrorCodes, LangString};
|
|
|
|
|
use rustc_session::lint;
|
2018-10-26 00:55:12 +02:00
|
|
|
|
2019-02-23 15:10:56 -05:00
|
|
|
pub const CHECK_PRIVATE_ITEMS_DOC_TESTS: Pass = Pass {
|
|
|
|
|
name: "check-private-items-doc-tests",
|
2020-01-04 10:58:32 -08:00
|
|
|
run: check_private_items_doc_tests,
|
2019-02-23 15:10:56 -05:00
|
|
|
description: "check private items doc tests",
|
|
|
|
|
};
|
2018-10-26 00:55:12 +02:00
|
|
|
|
2018-12-08 20:30:23 +01:00
|
|
|
struct PrivateItemDocTestLinter<'a, 'tcx> {
|
|
|
|
|
cx: &'a DocContext<'tcx>,
|
2018-10-26 00:55:12 +02:00
|
|
|
}
|
|
|
|
|
|
2018-12-08 20:30:23 +01:00
|
|
|
impl<'a, 'tcx> PrivateItemDocTestLinter<'a, 'tcx> {
|
|
|
|
|
fn new(cx: &'a DocContext<'tcx>) -> Self {
|
2019-12-22 17:42:04 -05:00
|
|
|
PrivateItemDocTestLinter { cx }
|
2018-10-26 00:55:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-08 20:30:23 +01:00
|
|
|
pub fn check_private_items_doc_tests(krate: Crate, cx: &DocContext<'_>) -> Crate {
|
2018-10-26 00:55:12 +02:00
|
|
|
let mut coll = PrivateItemDocTestLinter::new(cx);
|
|
|
|
|
|
|
|
|
|
coll.fold_crate(krate)
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-08 20:30:23 +01:00
|
|
|
impl<'a, 'tcx> DocFolder for PrivateItemDocTestLinter<'a, 'tcx> {
|
2018-10-26 00:55:12 +02:00
|
|
|
fn fold_item(&mut self, item: Item) -> Option<Item> {
|
|
|
|
|
let cx = self.cx;
|
|
|
|
|
let dox = item.attrs.collapsed_doc_value().unwrap_or_else(String::new);
|
|
|
|
|
|
2020-07-27 22:16:41 -04:00
|
|
|
look_for_tests(&cx, &dox, &item);
|
2018-10-26 00:55:12 +02:00
|
|
|
|
|
|
|
|
self.fold_item_recur(item)
|
|
|
|
|
}
|
|
|
|
|
}
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Tests {
|
|
|
|
|
pub(crate) fn new() -> Tests {
|
|
|
|
|
Tests { found_tests: 0 }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl crate::test::Tester for Tests {
|
|
|
|
|
fn add_test(&mut self, _: String, _: LangString, _: usize) {
|
|
|
|
|
self.found_tests += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-21 15:33:34 +02:00
|
|
|
pub fn should_have_doc_example(item_kind: &clean::ItemEnum) -> bool {
|
|
|
|
|
!matches!(item_kind,
|
|
|
|
|
clean::StructFieldItem(_)
|
|
|
|
|
| clean::VariantItem(_)
|
|
|
|
|
| clean::AssocConstItem(_, _)
|
|
|
|
|
| clean::AssocTypeItem(_, _)
|
|
|
|
|
| clean::TypedefItem(_, _)
|
|
|
|
|
| clean::StaticItem(_)
|
|
|
|
|
| clean::ConstantItem(_)
|
|
|
|
|
| clean::ExternCrateItem(_, _)
|
|
|
|
|
| clean::ImportItem(_)
|
|
|
|
|
| clean::PrimitiveItem(_)
|
|
|
|
|
| clean::KeywordItem(_)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-27 23:03:56 -04:00
|
|
|
pub fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
|
|
|
|
|
let hir_id = match cx.as_local_hir_id(item.def_id) {
|
|
|
|
|
Some(hir_id) => hir_id,
|
|
|
|
|
None => {
|
|
|
|
|
// If non-local, no need to check anything.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-08-18 13:31:23 +02:00
|
|
|
let mut tests = Tests::new();
|
2020-07-27 23:03:56 -04:00
|
|
|
|
|
|
|
|
find_testable_code(&dox, &mut tests, ErrorCodes::No, false, None);
|
|
|
|
|
|
2020-08-26 15:13:57 -04:00
|
|
|
if tests.found_tests == 0
|
|
|
|
|
&& rustc_feature::UnstableFeatures::from_environment().is_nightly_build()
|
|
|
|
|
{
|
2020-08-21 15:33:34 +02:00
|
|
|
if should_have_doc_example(&item.inner) {
|
2020-07-27 23:03:56 -04:00
|
|
|
debug!("reporting error for {:?} (hir_id={:?})", item, hir_id);
|
|
|
|
|
let sp = span_of_attrs(&item.attrs).unwrap_or(item.source.span());
|
|
|
|
|
cx.tcx.struct_span_lint_hir(
|
|
|
|
|
lint::builtin::MISSING_DOC_CODE_EXAMPLES,
|
|
|
|
|
hir_id,
|
|
|
|
|
sp,
|
|
|
|
|
|lint| lint.build("missing code example in this documentation").emit(),
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-08-26 15:13:57 -04:00
|
|
|
} else if tests.found_tests > 0 && !cx.renderinfo.borrow().access_levels.is_public(item.def_id)
|
2020-07-27 23:03:56 -04:00
|
|
|
{
|
|
|
|
|
cx.tcx.struct_span_lint_hir(
|
|
|
|
|
lint::builtin::PRIVATE_DOC_TESTS,
|
|
|
|
|
hir_id,
|
|
|
|
|
span_of_attrs(&item.attrs).unwrap_or(item.source.span()),
|
|
|
|
|
|lint| lint.build("documentation test in private item").emit(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|