Skip to content

Commit 3307ba8

Browse files
Ignore code examples on given items where it doesn't make sense
1 parent 1a22a0f commit 3307ba8

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

src/librustdoc/passes/calculate_doc_coverage.rs

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,27 @@ fn calculate_doc_coverage(krate: clean::Crate, ctx: &DocContext<'_>) -> clean::C
3131
struct ItemCount {
3232
total: u64,
3333
with_docs: u64,
34+
total_examples: u64,
3435
with_examples: u64,
3536
}
3637

3738
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+
) {
3945
self.total += 1;
4046

4147
if has_docs {
4248
self.with_docs += 1;
4349
}
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+
}
4655
}
4756
}
4857

@@ -55,8 +64,8 @@ impl ItemCount {
5564
}
5665

5766
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)
6069
} else {
6170
None
6271
}
@@ -70,6 +79,7 @@ impl ops::Sub for ItemCount {
7079
ItemCount {
7180
total: self.total - rhs.total,
7281
with_docs: self.with_docs - rhs.with_docs,
82+
total_examples: self.total_examples - rhs.total_examples,
7383
with_examples: self.with_examples - rhs.with_examples,
7484
}
7585
}
@@ -79,6 +89,7 @@ impl ops::AddAssign for ItemCount {
7989
fn add_assign(&mut self, rhs: Self) {
8090
self.total += rhs.total;
8191
self.with_docs += rhs.with_docs;
92+
self.total_examples += rhs.total_examples;
8293
self.with_examples += rhs.with_examples;
8394
}
8495
}
@@ -176,19 +187,6 @@ impl CoverageCalculator {
176187

177188
impl fold::DocFolder for CoverageCalculator {
178189
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-
192190
match i.inner {
193191
_ if !i.def_id.is_local() => {
194192
// non-local items are skipped because they can be out of the users control,
@@ -237,11 +235,34 @@ impl fold::DocFolder for CoverageCalculator {
237235
}
238236
}
239237
_ => {
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;
240261
debug!("counting {:?} {:?} in {}", i.type_(), i.name, i.source.filename);
241262
self.items
242263
.entry(i.source.filename.clone())
243264
.or_default()
244-
.count_item(has_docs, has_doc_example);
265+
.count_item(has_docs, has_doc_example, should_have_doc_examples);
245266
}
246267
}
247268

0 commit comments

Comments
 (0)