Skip to content

Commit b1cf1e7

Browse files
committed
Auto merge of rust-lang#10303 - pvdrz:pub_crate_missing_docs, r=giraffate
Add configuration to lint missing docs of `pub(crate)` items Fixes this: rust-lang/rust-clippy#5736 (comment) TODO: - [x] Needs docs - [x] Needs better names - [x] Should `pub` items be checked to when this new option is enabled? I'm saying no because `missing_docs` already exists `@flip1995` I'd like to get some input from you :) --- changelog: Enhancement: [`missing_docs_in_private_items`]: Added new configuration `missing-docs-in-crate-items` to lint on items visible within the current crate. For example, `pub(crate)` items. [rust-lang#10303](rust-lang/rust-clippy#10303) <!-- changelog_checked -->
2 parents 574c8ae + 790f28b commit b1cf1e7

File tree

8 files changed

+152
-10
lines changed

8 files changed

+152
-10
lines changed

Diff for: book/src/lint_configuration.md

+10
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Please use that command to update the file and do not edit it by hand.
5353
| [ignore-interior-mutability](#ignore-interior-mutability) | `["bytes::Bytes"]` |
5454
| [allow-mixed-uninlined-format-args](#allow-mixed-uninlined-format-args) | `true` |
5555
| [suppress-restriction-lint-in-const](#suppress-restriction-lint-in-const) | `false` |
56+
| [missing-docs-in-crate-items](#missing-docs-in-crate-items) | `false` |
5657

5758
### arithmetic-side-effects-allowed
5859
Suppress checking of the passed type names in all types of operations.
@@ -540,4 +541,13 @@ if no suggestion can be made.
540541
* [indexing_slicing](https://rust-lang.github.io/rust-clippy/master/index.html#indexing_slicing)
541542

542543

544+
### missing-docs-in-crate-items
545+
Whether to **only** check for missing documentation in items visible within the current
546+
crate. For example, `pub(crate)` items.
547+
548+
**Default Value:** `false` (`bool`)
549+
550+
* [missing_docs_in_private_items](https://rust-lang.github.io/rust-clippy/master/index.html#missing_docs_in_private_items)
551+
552+
543553

Diff for: clippy_lints/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -668,12 +668,13 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
668668
))
669669
});
670670
let doc_valid_idents = conf.doc_valid_idents.iter().cloned().collect::<FxHashSet<_>>();
671+
let missing_docs_in_crate_items = conf.missing_docs_in_crate_items;
671672
store.register_late_pass(move |_| Box::new(doc::DocMarkdown::new(doc_valid_idents.clone())));
672673
store.register_late_pass(|_| Box::new(neg_multiply::NegMultiply));
673674
store.register_late_pass(|_| Box::new(mem_forget::MemForget));
674675
store.register_late_pass(|_| Box::new(let_if_seq::LetIfSeq));
675676
store.register_late_pass(|_| Box::new(mixed_read_write_in_expression::EvalOrderDependence));
676-
store.register_late_pass(|_| Box::new(missing_doc::MissingDoc::new()));
677+
store.register_late_pass(move |_| Box::new(missing_doc::MissingDoc::new(missing_docs_in_crate_items)));
677678
store.register_late_pass(|_| Box::new(missing_inline::MissingInline));
678679
store.register_late_pass(move |_| Box::new(exhaustive_items::ExhaustiveItems));
679680
store.register_late_pass(|_| Box::new(match_result_ok::MatchResultOk));

Diff for: clippy_lints/src/missing_doc.rs

+22-9
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
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;
1415
use rustc_lint::{LateContext, LateLintPass, LintContext};
15-
use rustc_middle::ty::DefIdTree;
16+
use rustc_middle::ty::{DefIdTree, Visibility};
1617
use rustc_session::{declare_tool_lint, impl_lint_pass};
1718
use rustc_span::def_id::CRATE_DEF_ID;
1819
use rustc_span::source_map::Span;
@@ -35,6 +36,9 @@ declare_clippy_lint! {
3536
}
3637

3738
pub struct MissingDoc {
39+
/// Whether to **only** check for missing documentation in items visible within the current
40+
/// crate. For example, `pub(crate)` items.
41+
crate_items_only: bool,
3842
/// Stack of whether #[doc(hidden)] is set
3943
/// at each level which has lint attributes.
4044
doc_hidden_stack: Vec<bool>,
@@ -43,14 +47,15 @@ pub struct MissingDoc {
4347
impl Default for MissingDoc {
4448
#[must_use]
4549
fn default() -> Self {
46-
Self::new()
50+
Self::new(false)
4751
}
4852
}
4953

5054
impl MissingDoc {
5155
#[must_use]
52-
pub fn new() -> Self {
56+
pub fn new(crate_items_only: bool) -> Self {
5357
Self {
58+
crate_items_only,
5459
doc_hidden_stack: vec![false],
5560
}
5661
}
@@ -76,6 +81,7 @@ impl MissingDoc {
7681
fn check_missing_docs_attrs(
7782
&self,
7883
cx: &LateContext<'_>,
84+
def_id: LocalDefId,
7985
attrs: &[ast::Attribute],
8086
sp: Span,
8187
article: &'static str,
@@ -96,6 +102,13 @@ impl MissingDoc {
96102
return;
97103
}
98104

105+
if self.crate_items_only && def_id != CRATE_DEF_ID {
106+
let vis = cx.tcx.visibility(def_id);
107+
if vis == Visibility::Public || vis != Visibility::Restricted(CRATE_DEF_ID.into()) {
108+
return;
109+
}
110+
}
111+
99112
let has_doc = attrs
100113
.iter()
101114
.any(|a| a.doc_str().is_some() || Self::has_include(a.meta()));
@@ -124,7 +137,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
124137

125138
fn check_crate(&mut self, cx: &LateContext<'tcx>) {
126139
let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID);
127-
self.check_missing_docs_attrs(cx, attrs, cx.tcx.def_span(CRATE_DEF_ID), "the", "crate");
140+
self.check_missing_docs_attrs(cx, CRATE_DEF_ID, attrs, cx.tcx.def_span(CRATE_DEF_ID), "the", "crate");
128141
}
129142

130143
fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
@@ -160,7 +173,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
160173

161174
let attrs = cx.tcx.hir().attrs(it.hir_id());
162175
if !is_from_proc_macro(cx, it) {
163-
self.check_missing_docs_attrs(cx, attrs, it.span, article, desc);
176+
self.check_missing_docs_attrs(cx, it.owner_id.def_id, attrs, it.span, article, desc);
164177
}
165178
}
166179

@@ -169,7 +182,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
169182

170183
let attrs = cx.tcx.hir().attrs(trait_item.hir_id());
171184
if !is_from_proc_macro(cx, trait_item) {
172-
self.check_missing_docs_attrs(cx, attrs, trait_item.span, article, desc);
185+
self.check_missing_docs_attrs(cx, trait_item.owner_id.def_id, attrs, trait_item.span, article, desc);
173186
}
174187
}
175188

@@ -186,23 +199,23 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
186199
let (article, desc) = cx.tcx.article_and_description(impl_item.owner_id.to_def_id());
187200
let attrs = cx.tcx.hir().attrs(impl_item.hir_id());
188201
if !is_from_proc_macro(cx, impl_item) {
189-
self.check_missing_docs_attrs(cx, attrs, impl_item.span, article, desc);
202+
self.check_missing_docs_attrs(cx, impl_item.owner_id.def_id, attrs, impl_item.span, article, desc);
190203
}
191204
}
192205

193206
fn check_field_def(&mut self, cx: &LateContext<'tcx>, sf: &'tcx hir::FieldDef<'_>) {
194207
if !sf.is_positional() {
195208
let attrs = cx.tcx.hir().attrs(sf.hir_id);
196209
if !is_from_proc_macro(cx, sf) {
197-
self.check_missing_docs_attrs(cx, attrs, sf.span, "a", "struct field");
210+
self.check_missing_docs_attrs(cx, sf.def_id, attrs, sf.span, "a", "struct field");
198211
}
199212
}
200213
}
201214

202215
fn check_variant(&mut self, cx: &LateContext<'tcx>, v: &'tcx hir::Variant<'_>) {
203216
let attrs = cx.tcx.hir().attrs(v.hir_id);
204217
if !is_from_proc_macro(cx, v) {
205-
self.check_missing_docs_attrs(cx, attrs, v.span, "a", "variant");
218+
self.check_missing_docs_attrs(cx, v.def_id, attrs, v.span, "a", "variant");
206219
}
207220
}
208221
}

Diff for: clippy_lints/src/utils/conf.rs

+5
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,11 @@ define_Conf! {
454454
/// configuration will cause restriction lints to trigger even
455455
/// if no suggestion can be made.
456456
(suppress_restriction_lint_in_const: bool = false),
457+
/// Lint: MISSING_DOCS_IN_PRIVATE_ITEMS.
458+
///
459+
/// Whether to **only** check for missing documentation in items visible within the current
460+
/// crate. For example, `pub(crate)` items.
461+
(missing_docs_in_crate_items: bool = false),
457462
}
458463

459464
/// Search for the configuration file.

Diff for: tests/ui-toml/pub_crate_missing_docs/clippy.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
missing-docs-in-crate-items = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//! this is crate
2+
#![allow(missing_docs)]
3+
#![warn(clippy::missing_docs_in_private_items)]
4+
5+
/// this is mod
6+
mod my_mod {
7+
/// some docs
8+
fn priv_with_docs() {}
9+
fn priv_no_docs() {}
10+
/// some docs
11+
pub(crate) fn crate_with_docs() {}
12+
pub(crate) fn crate_no_docs() {}
13+
/// some docs
14+
pub(super) fn super_with_docs() {}
15+
pub(super) fn super_no_docs() {}
16+
17+
mod my_sub {
18+
/// some docs
19+
fn sub_priv_with_docs() {}
20+
fn sub_priv_no_docs() {}
21+
/// some docs
22+
pub(crate) fn sub_crate_with_docs() {}
23+
pub(crate) fn sub_crate_no_docs() {}
24+
/// some docs
25+
pub(super) fn sub_super_with_docs() {}
26+
pub(super) fn sub_super_no_docs() {}
27+
}
28+
29+
/// some docs
30+
pub(crate) struct CrateStructWithDocs {
31+
/// some docs
32+
pub(crate) crate_field_with_docs: (),
33+
pub(crate) crate_field_no_docs: (),
34+
/// some docs
35+
priv_field_with_docs: (),
36+
priv_field_no_docs: (),
37+
}
38+
39+
pub(crate) struct CrateStructNoDocs {
40+
/// some docs
41+
pub(crate) crate_field_with_docs: (),
42+
pub(crate) crate_field_no_docs: (),
43+
/// some docs
44+
priv_field_with_docs: (),
45+
priv_field_no_docs: (),
46+
}
47+
}
48+
49+
/// some docs
50+
type CrateTypedefWithDocs = String;
51+
type CrateTypedefNoDocs = String;
52+
/// some docs
53+
pub type PubTypedefWithDocs = String;
54+
pub type PubTypedefNoDocs = String;
55+
56+
fn main() {
57+
my_mod::crate_with_docs();
58+
my_mod::crate_no_docs();
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
error: missing documentation for a function
2+
--> $DIR/pub_crate_missing_doc.rs:12:5
3+
|
4+
LL | pub(crate) fn crate_no_docs() {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::missing-docs-in-private-items` implied by `-D warnings`
8+
9+
error: missing documentation for a function
10+
--> $DIR/pub_crate_missing_doc.rs:15:5
11+
|
12+
LL | pub(super) fn super_no_docs() {}
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error: missing documentation for a function
16+
--> $DIR/pub_crate_missing_doc.rs:23:9
17+
|
18+
LL | pub(crate) fn sub_crate_no_docs() {}
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
21+
error: missing documentation for a struct field
22+
--> $DIR/pub_crate_missing_doc.rs:33: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:39: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:42:9
41+
|
42+
LL | pub(crate) crate_field_no_docs: (),
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44+
45+
error: missing documentation for a type alias
46+
--> $DIR/pub_crate_missing_doc.rs:51:1
47+
|
48+
LL | type CrateTypedefNoDocs = String;
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
50+
51+
error: aborting due to 7 previous errors
52+

Diff for: tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ 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
3738
pass-by-value-size-limit
3839
single-char-binding-names-threshold

0 commit comments

Comments
 (0)