about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2020-08-19 22:44:21 +0200
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2020-08-20 17:41:36 +0200
commit3307ba8212a440e0f13a5dfdae57f2a24acdc025 (patch)
tree7d4801e676f8960d2559d587bcb5c063287062e8
parent1a22a0ff93d63f738151f096434e732466b4a42e (diff)
downloadrust-3307ba8212a440e0f13a5dfdae57f2a24acdc025.tar.gz
rust-3307ba8212a440e0f13a5dfdae57f2a24acdc025.zip
Ignore code examples on given items where it doesn't make sense
-rw-r--r--src/librustdoc/passes/calculate_doc_coverage.rs59
1 files changed, 40 insertions, 19 deletions
diff --git a/src/librustdoc/passes/calculate_doc_coverage.rs b/src/librustdoc/passes/calculate_doc_coverage.rs
index 0a836f46c0e..1d79fc3f191 100644
--- a/src/librustdoc/passes/calculate_doc_coverage.rs
+++ b/src/librustdoc/passes/calculate_doc_coverage.rs
@@ -31,18 +31,27 @@ fn calculate_doc_coverage(krate: clean::Crate, ctx: &DocContext<'_>) -> clean::C
 struct ItemCount {
     total: u64,
     with_docs: u64,
+    total_examples: u64,
     with_examples: u64,
 }
 
 impl ItemCount {
-    fn count_item(&mut self, has_docs: bool, has_doc_example: bool) {
+    fn count_item(
+        &mut self,
+        has_docs: bool,
+        has_doc_example: bool,
+        should_have_doc_examples: bool,
+    ) {
         self.total += 1;
 
         if has_docs {
             self.with_docs += 1;
         }
-        if has_doc_example {
-            self.with_examples += 1;
+        if should_have_doc_examples {
+            self.total_examples += 1;
+            if has_doc_example {
+                self.with_examples += 1;
+            }
         }
     }
 
@@ -55,8 +64,8 @@ impl ItemCount {
     }
 
     fn examples_percentage(&self) -> Option<f64> {
-        if self.total > 0 {
-            Some((self.with_examples as f64 * 100.0) / self.total as f64)
+        if self.total_examples > 0 {
+            Some((self.with_examples as f64 * 100.0) / self.total_examples as f64)
         } else {
             None
         }
@@ -70,6 +79,7 @@ impl ops::Sub for ItemCount {
         ItemCount {
             total: self.total - rhs.total,
             with_docs: self.with_docs - rhs.with_docs,
+            total_examples: self.total_examples - rhs.total_examples,
             with_examples: self.with_examples - rhs.with_examples,
         }
     }
@@ -79,6 +89,7 @@ impl ops::AddAssign for ItemCount {
     fn add_assign(&mut self, rhs: Self) {
         self.total += rhs.total;
         self.with_docs += rhs.with_docs;
+        self.total_examples += rhs.total_examples;
         self.with_examples += rhs.with_examples;
     }
 }
@@ -176,19 +187,6 @@ impl CoverageCalculator {
 
 impl fold::DocFolder for CoverageCalculator {
     fn fold_item(&mut self, i: clean::Item) -> Option<clean::Item> {
-        let has_docs = !i.attrs.doc_strings.is_empty();
-        let mut tests = Tests { found_tests: 0 };
-
-        find_testable_code(
-            &i.attrs.doc_strings.iter().map(|d| d.as_str()).collect::<Vec<_>>().join("\n"),
-            &mut tests,
-            ErrorCodes::No,
-            false,
-            None,
-        );
-
-        let has_doc_example = tests.found_tests != 0;
-
         match i.inner {
             _ if !i.def_id.is_local() => {
                 // non-local items are skipped because they can be out of the users control,
@@ -237,11 +235,34 @@ impl fold::DocFolder for CoverageCalculator {
                 }
             }
             _ => {
+                let has_docs = !i.attrs.doc_strings.is_empty();
+                let mut tests = Tests { found_tests: 0 };
+
+                let should_have_doc_examples = !matches!(i.inner,
+                    clean::StructFieldItem(_)
+                    | clean::VariantItem(_)
+                    | clean::AssocConstItem(_, _)
+                    | clean::AssocTypeItem(_, _)
+                    | clean::TypedefItem(_, _)
+                    | clean::StaticItem(_)
+                    | clean::ConstantItem(_)
+                );
+                if should_have_doc_examples {
+                    find_testable_code(
+                        &i.attrs.doc_strings.iter().map(|d| d.as_str()).collect::<Vec<_>>().join("\n"),
+                        &mut tests,
+                        ErrorCodes::No,
+                        false,
+                        None,
+                    );
+                }
+
+                let has_doc_example = tests.found_tests != 0;
                 debug!("counting {:?} {:?} in {}", i.type_(), i.name, i.source.filename);
                 self.items
                     .entry(i.source.filename.clone())
                     .or_default()
-                    .count_item(has_docs, has_doc_example);
+                    .count_item(has_docs, has_doc_example, should_have_doc_examples);
             }
         }