1212//! process.
1313
1414use rustc:: hir:: def_id:: DefId ;
15+ use rustc:: lint as lint;
1516use rustc:: middle:: privacy:: AccessLevels ;
1617use rustc:: util:: nodemap:: DefIdSet ;
1718use std:: mem;
1819use std:: fmt;
20+ use syntax:: ast:: NodeId ;
1921
2022use clean:: { self , GetDefId , Item } ;
21- use core:: DocContext ;
23+ use core:: { DocContext , DocAccessLevels } ;
2224use fold;
2325use fold:: StripItem ;
2426
27+ use html:: markdown:: { find_testable_code, ErrorCodes , LangString } ;
28+
29+ use self :: collect_intra_doc_links:: span_of_attrs;
30+
2531mod collapse_docs;
2632pub use self :: collapse_docs:: COLLAPSE_DOCS ;
2733
@@ -43,6 +49,9 @@ pub use self::propagate_doc_cfg::PROPAGATE_DOC_CFG;
4349mod collect_intra_doc_links;
4450pub use self :: collect_intra_doc_links:: COLLECT_INTRA_DOC_LINKS ;
4551
52+ mod private_items_doc_tests;
53+ pub use self :: private_items_doc_tests:: CHECK_PRIVATE_ITEMS_DOC_TESTS ;
54+
4655mod collect_trait_impls;
4756pub use self :: collect_trait_impls:: COLLECT_TRAIT_IMPLS ;
4857
@@ -128,6 +137,7 @@ impl Pass {
128137
129138/// The full list of passes.
130139pub const PASSES : & ' static [ Pass ] = & [
140+ CHECK_PRIVATE_ITEMS_DOC_TESTS ,
131141 STRIP_HIDDEN ,
132142 UNINDENT_COMMENTS ,
133143 COLLAPSE_DOCS ,
@@ -141,6 +151,7 @@ pub const PASSES: &'static [Pass] = &[
141151/// The list of passes run by default.
142152pub const DEFAULT_PASSES : & ' static [ & ' static str ] = & [
143153 "collect-trait-impls" ,
154+ "check-private-items-doc-tests" ,
144155 "strip-hidden" ,
145156 "strip-private" ,
146157 "collect-intra-doc-links" ,
@@ -152,6 +163,7 @@ pub const DEFAULT_PASSES: &'static [&'static str] = &[
152163/// The list of default passes run with `--document-private-items` is passed to rustdoc.
153164pub const DEFAULT_PRIVATE_PASSES : & ' static [ & ' static str ] = & [
154165 "collect-trait-impls" ,
166+ "check-private-items-doc-tests" ,
155167 "strip-priv-imports" ,
156168 "collect-intra-doc-links" ,
157169 "collapse-docs" ,
@@ -348,3 +360,49 @@ impl fold::DocFolder for ImportStripper {
348360 }
349361 }
350362}
363+
364+ pub fn look_for_tests < ' a , ' tcx : ' a , ' rcx : ' a , ' cstore : ' rcx > (
365+ cx : & ' a DocContext < ' a , ' tcx , ' rcx , ' cstore > ,
366+ dox : & str ,
367+ item : & Item ,
368+ check_missing_code : bool ,
369+ ) {
370+ if cx. as_local_node_id ( item. def_id ) . is_none ( ) {
371+ // If non-local, no need to check anything.
372+ return ;
373+ }
374+
375+ struct Tests {
376+ found_tests : usize ,
377+ }
378+
379+ impl :: test:: Tester for Tests {
380+ fn add_test ( & mut self , _: String , _: LangString , _: usize ) {
381+ self . found_tests += 1 ;
382+ }
383+ }
384+
385+ let mut tests = Tests {
386+ found_tests : 0 ,
387+ } ;
388+
389+ if find_testable_code ( & dox, & mut tests, ErrorCodes :: No ) . is_ok ( ) {
390+ if check_missing_code == true && tests. found_tests == 0 {
391+ let mut diag = cx. tcx . struct_span_lint_node (
392+ lint:: builtin:: MISSING_DOC_CODE_EXAMPLES ,
393+ NodeId :: from_u32 ( 0 ) ,
394+ span_of_attrs ( & item. attrs ) ,
395+ "Missing code example in this documentation" ) ;
396+ diag. emit ( ) ;
397+ } else if check_missing_code == false &&
398+ tests. found_tests > 0 &&
399+ !cx. renderinfo . borrow ( ) . access_levels . is_doc_reachable ( item. def_id ) {
400+ let mut diag = cx. tcx . struct_span_lint_node (
401+ lint:: builtin:: PRIVATE_DOC_TESTS ,
402+ NodeId :: from_u32 ( 0 ) ,
403+ span_of_attrs ( & item. attrs ) ,
404+ "Documentation test in private item" ) ;
405+ diag. emit ( ) ;
406+ }
407+ }
408+ }
0 commit comments