@@ -31,18 +31,27 @@ fn calculate_doc_coverage(krate: clean::Crate, ctx: &DocContext<'_>) -> clean::C
31
31
struct ItemCount {
32
32
total : u64 ,
33
33
with_docs : u64 ,
34
+ total_examples : u64 ,
34
35
with_examples : u64 ,
35
36
}
36
37
37
38
impl ItemCount {
38
- fn count_item ( & mut self , has_docs : bool , has_doc_example : bool ) {
39
+ fn count_item (
40
+ & mut self ,
41
+ has_docs : bool ,
42
+ has_doc_example : bool ,
43
+ should_have_doc_examples : bool ,
44
+ ) {
39
45
self . total += 1 ;
40
46
41
47
if has_docs {
42
48
self . with_docs += 1 ;
43
49
}
44
- if has_doc_example {
45
- self . with_examples += 1 ;
50
+ if should_have_doc_examples {
51
+ self . total_examples += 1 ;
52
+ if has_doc_example {
53
+ self . with_examples += 1 ;
54
+ }
46
55
}
47
56
}
48
57
@@ -55,8 +64,8 @@ impl ItemCount {
55
64
}
56
65
57
66
fn examples_percentage ( & self ) -> Option < f64 > {
58
- if self . total > 0 {
59
- Some ( ( self . with_examples as f64 * 100.0 ) / self . total as f64 )
67
+ if self . total_examples > 0 {
68
+ Some ( ( self . with_examples as f64 * 100.0 ) / self . total_examples as f64 )
60
69
} else {
61
70
None
62
71
}
@@ -70,6 +79,7 @@ impl ops::Sub for ItemCount {
70
79
ItemCount {
71
80
total : self . total - rhs. total ,
72
81
with_docs : self . with_docs - rhs. with_docs ,
82
+ total_examples : self . total_examples - rhs. total_examples ,
73
83
with_examples : self . with_examples - rhs. with_examples ,
74
84
}
75
85
}
@@ -79,6 +89,7 @@ impl ops::AddAssign for ItemCount {
79
89
fn add_assign ( & mut self , rhs : Self ) {
80
90
self . total += rhs. total ;
81
91
self . with_docs += rhs. with_docs ;
92
+ self . total_examples += rhs. total_examples ;
82
93
self . with_examples += rhs. with_examples ;
83
94
}
84
95
}
@@ -176,19 +187,6 @@ impl CoverageCalculator {
176
187
177
188
impl fold:: DocFolder for CoverageCalculator {
178
189
fn fold_item ( & mut self , i : clean:: Item ) -> Option < clean:: Item > {
179
- let has_docs = !i. attrs . doc_strings . is_empty ( ) ;
180
- let mut tests = Tests { found_tests : 0 } ;
181
-
182
- find_testable_code (
183
- & i. attrs . doc_strings . iter ( ) . map ( |d| d. as_str ( ) ) . collect :: < Vec < _ > > ( ) . join ( "\n " ) ,
184
- & mut tests,
185
- ErrorCodes :: No ,
186
- false ,
187
- None ,
188
- ) ;
189
-
190
- let has_doc_example = tests. found_tests != 0 ;
191
-
192
190
match i. inner {
193
191
_ if !i. def_id . is_local ( ) => {
194
192
// non-local items are skipped because they can be out of the users control,
@@ -237,11 +235,34 @@ impl fold::DocFolder for CoverageCalculator {
237
235
}
238
236
}
239
237
_ => {
238
+ let has_docs = !i. attrs . doc_strings . is_empty ( ) ;
239
+ let mut tests = Tests { found_tests : 0 } ;
240
+
241
+ let should_have_doc_examples = !matches ! ( i. inner,
242
+ clean:: StructFieldItem ( _)
243
+ | clean:: VariantItem ( _)
244
+ | clean:: AssocConstItem ( _, _)
245
+ | clean:: AssocTypeItem ( _, _)
246
+ | clean:: TypedefItem ( _, _)
247
+ | clean:: StaticItem ( _)
248
+ | clean:: ConstantItem ( _)
249
+ ) ;
250
+ if should_have_doc_examples {
251
+ find_testable_code (
252
+ & i. attrs . doc_strings . iter ( ) . map ( |d| d. as_str ( ) ) . collect :: < Vec < _ > > ( ) . join ( "\n " ) ,
253
+ & mut tests,
254
+ ErrorCodes :: No ,
255
+ false ,
256
+ None ,
257
+ ) ;
258
+ }
259
+
260
+ let has_doc_example = tests. found_tests != 0 ;
240
261
debug ! ( "counting {:?} {:?} in {}" , i. type_( ) , i. name, i. source. filename) ;
241
262
self . items
242
263
. entry ( i. source . filename . clone ( ) )
243
264
. or_default ( )
244
- . count_item ( has_docs, has_doc_example) ;
265
+ . count_item ( has_docs, has_doc_example, should_have_doc_examples ) ;
245
266
}
246
267
}
247
268
0 commit comments