Skip to content

Commit e0e3d85

Browse files
authored
Rollup merge of rust-lang#88720 - GuillaumeGomez:rustdoc-coverage-fields-count, r=Manishearth
Rustdoc coverage fields count Follow-up of rust-lang#88688. Instead of requiring enum tuple variant fields and tuple struct fields to be documented, we count them if they are documented, otherwise we don't include them in the count. r? `@Manishearth`
2 parents 8368af0 + eda4cfb commit e0e3d85

File tree

6 files changed

+108
-5
lines changed

6 files changed

+108
-5
lines changed

src/librustdoc/passes/calculate_doc_coverage.rs

+37-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ use crate::fold::{self, DocFolder};
44
use crate::html::markdown::{find_testable_code, ErrorCodes};
55
use crate::passes::doc_test_lints::{should_have_doc_example, Tests};
66
use crate::passes::Pass;
7+
use rustc_hir as hir;
78
use rustc_lint::builtin::MISSING_DOCS;
89
use rustc_middle::lint::LintLevelSource;
10+
use rustc_middle::ty::DefIdTree;
911
use rustc_session::lint;
1012
use rustc_span::FileName;
1113
use serde::Serialize;
@@ -221,10 +223,42 @@ impl<'a, 'b> fold::DocFolder for CoverageCalculator<'a, 'b> {
221223
.hir()
222224
.local_def_id_to_hir_id(i.def_id.expect_def_id().expect_local());
223225
let (level, source) = self.ctx.tcx.lint_level_at_node(MISSING_DOCS, hir_id);
226+
227+
// In case we have:
228+
//
229+
// ```
230+
// enum Foo { Bar(u32) }
231+
// // or:
232+
// struct Bar(u32);
233+
// ```
234+
//
235+
// there is no need to require documentation on the fields of tuple variants and
236+
// tuple structs.
237+
let should_be_ignored = i
238+
.def_id
239+
.as_def_id()
240+
.and_then(|def_id| self.ctx.tcx.parent(def_id))
241+
.and_then(|def_id| self.ctx.tcx.hir().get_if_local(def_id))
242+
.map(|node| {
243+
matches!(
244+
node,
245+
hir::Node::Variant(hir::Variant {
246+
data: hir::VariantData::Tuple(_, _),
247+
..
248+
}) | hir::Node::Item(hir::Item {
249+
kind: hir::ItemKind::Struct(hir::VariantData::Tuple(_, _), _),
250+
..
251+
})
252+
)
253+
})
254+
.unwrap_or(false);
255+
224256
// `missing_docs` is allow-by-default, so don't treat this as ignoring the item
225-
// unless the user had an explicit `allow`
226-
let should_have_docs =
227-
level != lint::Level::Allow || matches!(source, LintLevelSource::Default);
257+
// unless the user had an explicit `allow`.
258+
//
259+
let should_have_docs = !should_be_ignored
260+
&& (level != lint::Level::Allow || matches!(source, LintLevelSource::Default));
261+
228262
debug!("counting {:?} {:?} in {:?}", i.type_(), i.name, filename);
229263
self.items.entry(filename).or_default().count_item(
230264
has_docs,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// compile-flags:-Z unstable-options --show-coverage
2+
// check-pass
3+
4+
// The point of this test is to ensure that the number of "documented" items
5+
// is higher than in `enum-tuple.rs`.
6+
7+
//! (remember the crate root is still a module)
8+
9+
/// so check out this enum here
10+
pub enum ThisEnum {
11+
/// VarOne.
12+
VarOne(
13+
/// hello!
14+
String,
15+
),
16+
/// Var Two.
17+
VarTwo(
18+
/// Hello
19+
String,
20+
/// Bis repetita.
21+
String,
22+
),
23+
}
24+
25+
/// Struct.
26+
pub struct ThisStruct(
27+
/// hello
28+
u32,
29+
);
30+
31+
/// Struct.
32+
pub struct ThisStruct2(
33+
/// hello
34+
u32,
35+
/// Bis repetita.
36+
u8,
37+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
+-------------------------------------+------------+------------+------------+------------+
2+
| File | Documented | Percentage | Examples | Percentage |
3+
+-------------------------------------+------------+------------+------------+------------+
4+
| ...overage/enum-tuple-documented.rs | 9 | 100.0% | 0 | 0.0% |
5+
+-------------------------------------+------------+------------+------------+------------+
6+
| Total | 9 | 100.0% | 0 | 0.0% |
7+
+-------------------------------------+------------+------------+------------+------------+
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// compile-flags:-Z unstable-options --show-coverage
2+
// check-pass
3+
4+
//! (remember the crate root is still a module)
5+
6+
/// so check out this enum here
7+
pub enum ThisEnum {
8+
/// No need to document the field if there is only one in a tuple variant!
9+
VarOne(String),
10+
/// But if there is more than one... still fine!
11+
VarTwo(String, String),
12+
}
13+
14+
/// Struct.
15+
pub struct ThisStruct(u32);
16+
17+
/// Struct.
18+
pub struct ThisStruct2(u32, u8);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
+-------------------------------------+------------+------------+------------+------------+
2+
| File | Documented | Percentage | Examples | Percentage |
3+
+-------------------------------------+------------+------------+------------+------------+
4+
| ...ustdoc-ui/coverage/enum-tuple.rs | 6 | 100.0% | 0 | 0.0% |
5+
+-------------------------------------+------------+------------+------------+------------+
6+
| Total | 6 | 100.0% | 0 | 0.0% |
7+
+-------------------------------------+------------+------------+------------+------------+
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
+-------------------------------------+------------+------------+------------+------------+
22
| File | Documented | Percentage | Examples | Percentage |
33
+-------------------------------------+------------+------------+------------+------------+
4-
| ...est/rustdoc-ui/coverage/enums.rs | 6 | 66.7% | 0 | 0.0% |
4+
| ...est/rustdoc-ui/coverage/enums.rs | 6 | 75.0% | 0 | 0.0% |
55
+-------------------------------------+------------+------------+------------+------------+
6-
| Total | 6 | 66.7% | 0 | 0.0% |
6+
| Total | 6 | 75.0% | 0 | 0.0% |
77
+-------------------------------------+------------+------------+------------+------------+

0 commit comments

Comments
 (0)