Skip to content

Commit def6177

Browse files
committed
Move look_for_tests to private_items_doc_tests
1 parent 617d109 commit def6177

File tree

2 files changed

+61
-56
lines changed

2 files changed

+61
-56
lines changed

src/librustdoc/passes/mod.rs

-55
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
44
use rustc_hir::def_id::{DefId, DefIdSet};
55
use rustc_middle::middle::privacy::AccessLevels;
6-
use rustc_session::lint;
76
use rustc_span::{InnerSpan, Span, DUMMY_SP};
87
use std::mem;
98
use std::ops::Range;
@@ -12,7 +11,6 @@ use self::Condition::*;
1211
use crate::clean::{self, GetDefId, Item};
1312
use crate::core::DocContext;
1413
use crate::fold::{DocFolder, StripItem};
15-
use crate::html::markdown::{find_testable_code, ErrorCodes, LangString};
1614

1715
mod collapse_docs;
1816
pub use self::collapse_docs::COLLAPSE_DOCS;
@@ -312,59 +310,6 @@ impl DocFolder for ImportStripper {
312310
}
313311
}
314312

315-
pub fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
316-
let hir_id = match cx.as_local_hir_id(item.def_id) {
317-
Some(hir_id) => hir_id,
318-
None => {
319-
// If non-local, no need to check anything.
320-
return;
321-
}
322-
};
323-
324-
struct Tests {
325-
found_tests: usize,
326-
}
327-
328-
impl crate::test::Tester for Tests {
329-
fn add_test(&mut self, _: String, _: LangString, _: usize) {
330-
self.found_tests += 1;
331-
}
332-
}
333-
334-
let mut tests = Tests { found_tests: 0 };
335-
336-
find_testable_code(&dox, &mut tests, ErrorCodes::No, false, None);
337-
338-
if tests.found_tests == 0 {
339-
use clean::ItemEnum::*;
340-
341-
let should_report = match item.inner {
342-
ExternCrateItem(_, _) | ImportItem(_) | PrimitiveItem(_) | KeywordItem(_) => false,
343-
_ => true,
344-
};
345-
if should_report {
346-
debug!("reporting error for {:?} (hir_id={:?})", item, hir_id);
347-
let sp = span_of_attrs(&item.attrs).unwrap_or(item.source.span());
348-
cx.tcx.struct_span_lint_hir(
349-
lint::builtin::MISSING_DOC_CODE_EXAMPLES,
350-
hir_id,
351-
sp,
352-
|lint| lint.build("missing code example in this documentation").emit(),
353-
);
354-
}
355-
} else if rustc_feature::UnstableFeatures::from_environment().is_nightly_build()
356-
&& tests.found_tests > 0
357-
&& !cx.renderinfo.borrow().access_levels.is_public(item.def_id)
358-
{
359-
cx.tcx.struct_span_lint_hir(
360-
lint::builtin::PRIVATE_DOC_TESTS,
361-
hir_id,
362-
span_of_attrs(&item.attrs).unwrap_or(item.source.span()),
363-
|lint| lint.build("documentation test in private item").emit(),
364-
);
365-
}
366-
}
367-
368313
/// Returns a span encompassing all the given attributes.
369314
crate fn span_of_attrs(attrs: &clean::Attributes) -> Option<Span> {
370315
if attrs.doc_strings.is_empty() {

src/librustdoc/passes/private_items_doc_tests.rs

+61-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
//! This pass is overloaded and runs two different lints.
2+
//!
3+
//! - MISSING_DOC_CODE_EXAMPLES: this looks for public items missing doc-tests
4+
//! - PRIVATE_DOC_TESTS: this looks for private items with doc-tests.
5+
16
use crate::clean::*;
27
use crate::core::DocContext;
38
use crate::fold::DocFolder;
4-
use crate::passes::{look_for_tests, Pass};
9+
use super::{span_of_attrs, Pass};
10+
use crate::html::markdown::{find_testable_code, ErrorCodes, LangString};
11+
use rustc_session::lint;
512

613
pub const CHECK_PRIVATE_ITEMS_DOC_TESTS: Pass = Pass {
714
name: "check-private-items-doc-tests",
@@ -35,3 +42,56 @@ impl<'a, 'tcx> DocFolder for PrivateItemDocTestLinter<'a, 'tcx> {
3542
self.fold_item_recur(item)
3643
}
3744
}
45+
46+
pub fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) {
47+
let hir_id = match cx.as_local_hir_id(item.def_id) {
48+
Some(hir_id) => hir_id,
49+
None => {
50+
// If non-local, no need to check anything.
51+
return;
52+
}
53+
};
54+
55+
struct Tests {
56+
found_tests: usize,
57+
}
58+
59+
impl crate::test::Tester for Tests {
60+
fn add_test(&mut self, _: String, _: LangString, _: usize) {
61+
self.found_tests += 1;
62+
}
63+
}
64+
65+
let mut tests = Tests { found_tests: 0 };
66+
67+
find_testable_code(&dox, &mut tests, ErrorCodes::No, false, None);
68+
69+
if tests.found_tests == 0 {
70+
use ItemEnum::*;
71+
72+
let should_report = match item.inner {
73+
ExternCrateItem(_, _) | ImportItem(_) | PrimitiveItem(_) | KeywordItem(_) => false,
74+
_ => true,
75+
};
76+
if should_report {
77+
debug!("reporting error for {:?} (hir_id={:?})", item, hir_id);
78+
let sp = span_of_attrs(&item.attrs).unwrap_or(item.source.span());
79+
cx.tcx.struct_span_lint_hir(
80+
lint::builtin::MISSING_DOC_CODE_EXAMPLES,
81+
hir_id,
82+
sp,
83+
|lint| lint.build("missing code example in this documentation").emit(),
84+
);
85+
}
86+
} else if rustc_feature::UnstableFeatures::from_environment().is_nightly_build()
87+
&& tests.found_tests > 0
88+
&& !cx.renderinfo.borrow().access_levels.is_public(item.def_id)
89+
{
90+
cx.tcx.struct_span_lint_hir(
91+
lint::builtin::PRIVATE_DOC_TESTS,
92+
hir_id,
93+
span_of_attrs(&item.attrs).unwrap_or(item.source.span()),
94+
|lint| lint.build("documentation test in private item").emit(),
95+
);
96+
}
97+
}

0 commit comments

Comments
 (0)