Skip to content

Commit 64344cc

Browse files
Don't require documentation for fields in an enum tuple variant or for tuple struct fields.
1 parent 1c858ba commit 64344cc

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
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,

0 commit comments

Comments
 (0)