]> git.lizzy.rs Git - rust.git/commitdiff
Ignore code examples on given items where it doesn't make sense
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Wed, 19 Aug 2020 20:44:21 +0000 (22:44 +0200)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Thu, 20 Aug 2020 15:41:36 +0000 (17:41 +0200)
src/librustdoc/passes/calculate_doc_coverage.rs

index 0a836f46c0eb85bc96bc861e94f3af109e58eb1c..1d79fc3f191f2b03ca8993d448466a90c267ba0c 100644 (file)
@@ -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 @@ fn percentage(&self) -> Option<f64> {
     }
 
     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 @@ fn sub(self, rhs: Self) -> Self {
         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 @@ fn print_table_record(
 
 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 @@ 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 };
+
+                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);
             }
         }