Skip to content

Commit e2e23c0

Browse files
committed
Add docs and update tests
1 parent 8eac9e3 commit e2e23c0

File tree

8 files changed

+66
-21
lines changed

8 files changed

+66
-21
lines changed

clippy_lints/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,13 +665,13 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
665665
))
666666
});
667667
let doc_valid_idents = conf.doc_valid_idents.iter().cloned().collect::<FxHashSet<_>>();
668-
let only_check_missing_docs_in_crate_items = conf.only_check_missing_docs_in_crate_items;
668+
let missing_docs_in_crate_items = conf.missing_docs_in_crate_items;
669669
store.register_late_pass(move |_| Box::new(doc::DocMarkdown::new(doc_valid_idents.clone())));
670670
store.register_late_pass(|_| Box::new(neg_multiply::NegMultiply));
671671
store.register_late_pass(|_| Box::new(mem_forget::MemForget));
672672
store.register_late_pass(|_| Box::new(let_if_seq::LetIfSeq));
673673
store.register_late_pass(|_| Box::new(mixed_read_write_in_expression::EvalOrderDependence));
674-
store.register_late_pass(move |_| Box::new(missing_doc::MissingDoc::new(only_check_missing_docs_in_crate_items)));
674+
store.register_late_pass(move |_| Box::new(missing_doc::MissingDoc::new(missing_docs_in_crate_items)));
675675
store.register_late_pass(|_| Box::new(missing_inline::MissingInline));
676676
store.register_late_pass(move |_| Box::new(exhaustive_items::ExhaustiveItems));
677677
store.register_late_pass(|_| Box::new(match_result_ok::MatchResultOk));

clippy_lints/src/missing_doc.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use clippy_utils::attrs::is_doc_hidden;
99
use clippy_utils::diagnostics::span_lint;
1010
use clippy_utils::is_from_proc_macro;
11+
use hir::def_id::LocalDefId;
1112
use if_chain::if_chain;
1213
use rustc_ast::ast::{self, MetaItem, MetaItemKind};
1314
use rustc_hir as hir;
@@ -35,7 +36,7 @@ declare_clippy_lint! {
3536
}
3637

3738
pub struct MissingDoc {
38-
/// FIXME: docs
39+
/// Whether to only check for missing docs in `pub(crate)` items.
3940
crate_items_only: bool,
4041
/// Stack of whether #[doc(hidden)] is set
4142
/// at each level which has lint attributes.
@@ -79,6 +80,7 @@ impl MissingDoc {
7980
fn check_missing_docs_attrs(
8081
&self,
8182
cx: &LateContext<'_>,
83+
def_id: LocalDefId,
8284
attrs: &[ast::Attribute],
8385
sp: Span,
8486
article: &'static str,
@@ -99,6 +101,13 @@ impl MissingDoc {
99101
return;
100102
}
101103

104+
if self.crate_items_only && def_id != CRATE_DEF_ID {
105+
let vis = cx.tcx.visibility(def_id);
106+
if vis != Visibility::Public && vis != Visibility::Restricted(CRATE_DEF_ID.into()) {
107+
return;
108+
}
109+
}
110+
102111
let has_doc = attrs
103112
.iter()
104113
.any(|a| a.doc_str().is_some() || Self::has_include(a.meta()));
@@ -127,17 +136,10 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
127136

128137
fn check_crate(&mut self, cx: &LateContext<'tcx>) {
129138
let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID);
130-
self.check_missing_docs_attrs(cx, attrs, cx.tcx.def_span(CRATE_DEF_ID), "the", "crate");
139+
self.check_missing_docs_attrs(cx, CRATE_DEF_ID, attrs, cx.tcx.def_span(CRATE_DEF_ID), "the", "crate");
131140
}
132141

133142
fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
134-
if self.crate_items_only {
135-
let vis = cx.tcx.visibility(it.owner_id.to_def_id());
136-
if vis != Visibility::Public && vis != Visibility::Restricted(CRATE_DEF_ID.into()) {
137-
return;
138-
}
139-
}
140-
141143
match it.kind {
142144
hir::ItemKind::Fn(..) => {
143145
// ignore main()
@@ -170,7 +172,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
170172

171173
let attrs = cx.tcx.hir().attrs(it.hir_id());
172174
if !is_from_proc_macro(cx, it) {
173-
self.check_missing_docs_attrs(cx, attrs, it.span, article, desc);
175+
self.check_missing_docs_attrs(cx, it.owner_id.def_id, attrs, it.span, article, desc);
174176
}
175177
}
176178

@@ -179,7 +181,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
179181

180182
let attrs = cx.tcx.hir().attrs(trait_item.hir_id());
181183
if !is_from_proc_macro(cx, trait_item) {
182-
self.check_missing_docs_attrs(cx, attrs, trait_item.span, article, desc);
184+
self.check_missing_docs_attrs(cx, trait_item.owner_id.def_id, attrs, trait_item.span, article, desc);
183185
}
184186
}
185187

@@ -196,23 +198,23 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
196198
let (article, desc) = cx.tcx.article_and_description(impl_item.owner_id.to_def_id());
197199
let attrs = cx.tcx.hir().attrs(impl_item.hir_id());
198200
if !is_from_proc_macro(cx, impl_item) {
199-
self.check_missing_docs_attrs(cx, attrs, impl_item.span, article, desc);
201+
self.check_missing_docs_attrs(cx, impl_item.owner_id.def_id, attrs, impl_item.span, article, desc);
200202
}
201203
}
202204

203205
fn check_field_def(&mut self, cx: &LateContext<'tcx>, sf: &'tcx hir::FieldDef<'_>) {
204206
if !sf.is_positional() {
205207
let attrs = cx.tcx.hir().attrs(sf.hir_id);
206208
if !is_from_proc_macro(cx, sf) {
207-
self.check_missing_docs_attrs(cx, attrs, sf.span, "a", "struct field");
209+
self.check_missing_docs_attrs(cx, sf.def_id, attrs, sf.span, "a", "struct field");
208210
}
209211
}
210212
}
211213

212214
fn check_variant(&mut self, cx: &LateContext<'tcx>, v: &'tcx hir::Variant<'_>) {
213215
let attrs = cx.tcx.hir().attrs(v.hir_id);
214216
if !is_from_proc_macro(cx, v) {
215-
self.check_missing_docs_attrs(cx, attrs, v.span, "a", "variant");
217+
self.check_missing_docs_attrs(cx, v.def_id, attrs, v.span, "a", "variant");
216218
}
217219
}
218220
}

clippy_lints/src/utils/conf.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,8 @@ define_Conf! {
456456
(suppress_restriction_lint_in_const: bool = false),
457457
/// Lint: MISSING_DOCS_IN_PRIVATE_ITEMS.
458458
///
459-
/// FIXME: docs
460-
(only_check_missing_docs_in_crate_items: bool = false),
459+
/// Whether to **only** check for missing docmuentation in `pub(crate)` items.
460+
(missing_docs_in_crate_items: bool = false),
461461
}
462462

463463
/// Search for the configuration file.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
only-check-missing-docs-in-crate-items = true
1+
missing-docs-in-crate-items = true

tests/ui-toml/pub_crate_missing_docs/pub_crate_missing_doc.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ mod my_mod {
2424
pub(super) fn sub_super_with_docs() {}
2525
pub(super) fn sub_super_no_docs() {}
2626
}
27+
28+
/// some docs
29+
pub(crate) struct CrateStructWithDocs {
30+
/// some docs
31+
pub(crate) crate_field_with_docs: (),
32+
pub(crate) crate_field_no_docs: (),
33+
/// some docs
34+
priv_field_with_docs: (),
35+
priv_field_no_docs: (),
36+
}
37+
38+
pub(crate) struct CrateStructNoDocs {
39+
/// some docs
40+
pub(crate) crate_field_with_docs: (),
41+
pub(crate) crate_field_no_docs: (),
42+
/// some docs
43+
priv_field_with_docs: (),
44+
priv_field_no_docs: (),
45+
}
2746
}
2847

2948
fn main() {

tests/ui-toml/pub_crate_missing_docs/pub_crate_missing_doc.stderr

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,29 @@ error: missing documentation for a function
1818
LL | pub(crate) fn sub_crate_no_docs() {}
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2020

21-
error: aborting due to 3 previous errors
21+
error: missing documentation for a struct field
22+
--> $DIR/pub_crate_missing_doc.rs:32:9
23+
|
24+
LL | pub(crate) crate_field_no_docs: (),
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26+
27+
error: missing documentation for a struct
28+
--> $DIR/pub_crate_missing_doc.rs:38:5
29+
|
30+
LL | / pub(crate) struct CrateStructNoDocs {
31+
LL | | /// some docs
32+
LL | | pub(crate) crate_field_with_docs: (),
33+
LL | | pub(crate) crate_field_no_docs: (),
34+
... |
35+
LL | | priv_field_no_docs: (),
36+
LL | | }
37+
| |_____^
38+
39+
error: missing documentation for a struct field
40+
--> $DIR/pub_crate_missing_doc.rs:41:9
41+
|
42+
LL | pub(crate) crate_field_no_docs: (),
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
45+
error: aborting due to 6 previous errors
2246

tests/ui-toml/pub_crate_missing_docs/pub_crate_missing_doc.stdout

Whitespace-only changes.

tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown fie
3333
max-struct-bools
3434
max-suggested-slice-pattern-length
3535
max-trait-bounds
36+
missing-docs-in-crate-items
3637
msrv
37-
only-check-missing-docs-in-crate-items
3838
pass-by-value-size-limit
3939
single-char-binding-names-threshold
4040
standard-macro-braces

0 commit comments

Comments
 (0)