12
12
//! process.
13
13
14
14
use rustc:: hir:: def_id:: DefId ;
15
+ use rustc:: lint as lint;
15
16
use rustc:: middle:: privacy:: AccessLevels ;
16
17
use rustc:: util:: nodemap:: DefIdSet ;
17
18
use std:: mem;
18
19
use std:: fmt;
20
+ use syntax:: ast:: NodeId ;
19
21
20
22
use clean:: { self , GetDefId , Item } ;
21
- use core:: DocContext ;
23
+ use core:: { DocContext , DocAccessLevels } ;
22
24
use fold;
23
25
use fold:: StripItem ;
24
26
27
+ use html:: markdown:: { find_testable_code, ErrorCodes , LangString } ;
28
+
29
+ use self :: collect_intra_doc_links:: span_of_attrs;
30
+
25
31
mod collapse_docs;
26
32
pub use self :: collapse_docs:: COLLAPSE_DOCS ;
27
33
@@ -43,6 +49,9 @@ pub use self::propagate_doc_cfg::PROPAGATE_DOC_CFG;
43
49
mod collect_intra_doc_links;
44
50
pub use self :: collect_intra_doc_links:: COLLECT_INTRA_DOC_LINKS ;
45
51
52
+ mod private_items_doc_tests;
53
+ pub use self :: private_items_doc_tests:: CHECK_PRIVATE_ITEMS_DOC_TESTS ;
54
+
46
55
mod collect_trait_impls;
47
56
pub use self :: collect_trait_impls:: COLLECT_TRAIT_IMPLS ;
48
57
@@ -128,6 +137,7 @@ impl Pass {
128
137
129
138
/// The full list of passes.
130
139
pub const PASSES : & ' static [ Pass ] = & [
140
+ CHECK_PRIVATE_ITEMS_DOC_TESTS ,
131
141
STRIP_HIDDEN ,
132
142
UNINDENT_COMMENTS ,
133
143
COLLAPSE_DOCS ,
@@ -141,6 +151,7 @@ pub const PASSES: &'static [Pass] = &[
141
151
/// The list of passes run by default.
142
152
pub const DEFAULT_PASSES : & ' static [ & ' static str ] = & [
143
153
"collect-trait-impls" ,
154
+ "check-private-items-doc-tests" ,
144
155
"strip-hidden" ,
145
156
"strip-private" ,
146
157
"collect-intra-doc-links" ,
@@ -152,6 +163,7 @@ pub const DEFAULT_PASSES: &'static [&'static str] = &[
152
163
/// The list of default passes run with `--document-private-items` is passed to rustdoc.
153
164
pub const DEFAULT_PRIVATE_PASSES : & ' static [ & ' static str ] = & [
154
165
"collect-trait-impls" ,
166
+ "check-private-items-doc-tests" ,
155
167
"strip-priv-imports" ,
156
168
"collect-intra-doc-links" ,
157
169
"collapse-docs" ,
@@ -348,3 +360,49 @@ impl fold::DocFolder for ImportStripper {
348
360
}
349
361
}
350
362
}
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