Skip to content

Commit 02e6b86

Browse files
r-52Joshua Nelson
authored andcommitted
rustdoc: skip allow missing doc in cover. report
During the document coverage reporting with ```bash rustdoc something.rs -Z unstable-options --show-coverage ``` the coverage report also includes parts of the code that are marked with `#[allow(missing_docs)]`, which outputs lower numbers in the coverage report even though these parts should be ignored for the calculation. Co-authored-by: Joshua Nelson <joshua@yottadb.com>
1 parent 0e022fc commit 02e6b86

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

src/librustdoc/passes/calculate_doc_coverage.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::fold::{self, DocFolder};
55
use crate::html::markdown::{find_testable_code, ErrorCodes};
66
use crate::passes::doc_test_lints::{should_have_doc_example, Tests};
77
use crate::passes::Pass;
8-
use rustc_span::symbol::sym;
8+
use rustc_span::symbol::{sym, Ident};
99
use rustc_span::FileName;
1010
use serde::Serialize;
1111

@@ -41,8 +41,11 @@ impl ItemCount {
4141
has_docs: bool,
4242
has_doc_example: bool,
4343
should_have_doc_examples: bool,
44+
should_have_docs: bool,
4445
) {
45-
self.total += 1;
46+
if has_docs || should_have_docs {
47+
self.total += 1;
48+
}
4649

4750
if has_docs {
4851
self.with_docs += 1;
@@ -229,6 +232,15 @@ impl fold::DocFolder for CoverageCalculator {
229232
}
230233
_ => {
231234
let has_docs = !i.attrs.doc_strings.is_empty();
235+
let should_have_docs = !i.attrs.other_attrs.iter().any(|a| {
236+
a.has_name(sym::allow)
237+
&& a.meta_item_list().iter().any(|meta_list_item| {
238+
meta_list_item.iter().any(|li| match li.ident() {
239+
Some(ident) => ident == Ident::from_str("missing_docs"),
240+
_ => false,
241+
})
242+
})
243+
});
232244
let mut tests = Tests { found_tests: 0 };
233245

234246
find_testable_code(
@@ -250,7 +262,12 @@ impl fold::DocFolder for CoverageCalculator {
250262
has_docs,
251263
has_doc_example,
252264
should_have_doc_example(&i.inner),
265+
should_have_docs,
253266
);
267+
268+
if !should_have_docs {
269+
return Some(i);
270+
}
254271
}
255272
}
256273

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// compile-flags:-Z unstable-options --show-coverage
2+
// check-pass
3+
4+
//! Make sure to have some docs on your crate root
5+
6+
#[allow(missing_docs)]
7+
pub mod mod_foo {
8+
pub struct Bar;
9+
}
10+
11+
/// This is a struct with a `#[allow(missing_docs)]`
12+
pub struct AllowTheMissingDocs {
13+
#[allow(missing_docs)]
14+
pub empty_str: String,
15+
16+
/// This has
17+
#[allow(missing_docs)]
18+
/// but also has documentation comments
19+
pub hello: usize,
20+
21+
/// The doc id just to create a boilerplate comment
22+
pub doc_id: Vec<u8>,
23+
}
24+
25+
/// A function that has a documentation
26+
pub fn this_is_func() {}
27+
28+
#[allow(missing_docs)]
29+
pub struct DemoStruct {
30+
something: usize,
31+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
+-------------------------------------+------------+------------+------------+------------+
2+
| File | Documented | Percentage | Examples | Percentage |
3+
+-------------------------------------+------------+------------+------------+------------+
4+
| ...i/coverage/allow_missing_docs.rs | 5 | 100.0% | 0 | 0.0% |
5+
+-------------------------------------+------------+------------+------------+------------+
6+
| Total | 5 | 100.0% | 0 | 0.0% |
7+
+-------------------------------------+------------+------------+------------+------------+

0 commit comments

Comments
 (0)