Skip to content

Commit c6692a8

Browse files
committed
Add configuration to lint missing docs of pub(crate) items
1 parent 8a98609 commit c6692a8

File tree

6 files changed

+74
-4
lines changed

6 files changed

+74
-4
lines changed

clippy_lints/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,12 +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;
668669
store.register_late_pass(move |_| Box::new(doc::DocMarkdown::new(doc_valid_idents.clone())));
669670
store.register_late_pass(|_| Box::new(neg_multiply::NegMultiply));
670671
store.register_late_pass(|_| Box::new(mem_forget::MemForget));
671672
store.register_late_pass(|_| Box::new(let_if_seq::LetIfSeq));
672673
store.register_late_pass(|_| Box::new(mixed_read_write_in_expression::EvalOrderDependence));
673-
store.register_late_pass(|_| Box::new(missing_doc::MissingDoc::new()));
674+
store.register_late_pass(move |_| Box::new(missing_doc::MissingDoc::new(only_check_missing_docs_in_crate_items)));
674675
store.register_late_pass(|_| Box::new(missing_inline::MissingInline));
675676
store.register_late_pass(move |_| Box::new(exhaustive_items::ExhaustiveItems));
676677
store.register_late_pass(|_| Box::new(match_result_ok::MatchResultOk));

clippy_lints/src/missing_doc.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use if_chain::if_chain;
1212
use rustc_ast::ast::{self, MetaItem, MetaItemKind};
1313
use rustc_hir as hir;
1414
use rustc_lint::{LateContext, LateLintPass, LintContext};
15-
use rustc_middle::ty::DefIdTree;
15+
use rustc_middle::ty::{DefIdTree, Visibility};
1616
use rustc_session::{declare_tool_lint, impl_lint_pass};
1717
use rustc_span::def_id::CRATE_DEF_ID;
1818
use rustc_span::source_map::Span;
@@ -35,6 +35,8 @@ declare_clippy_lint! {
3535
}
3636

3737
pub struct MissingDoc {
38+
/// FIXME: docs
39+
crate_items_only: bool,
3840
/// Stack of whether #[doc(hidden)] is set
3941
/// at each level which has lint attributes.
4042
doc_hidden_stack: Vec<bool>,
@@ -43,14 +45,15 @@ pub struct MissingDoc {
4345
impl Default for MissingDoc {
4446
#[must_use]
4547
fn default() -> Self {
46-
Self::new()
48+
Self::new(false)
4749
}
4850
}
4951

5052
impl MissingDoc {
5153
#[must_use]
52-
pub fn new() -> Self {
54+
pub fn new(crate_items_only: bool) -> Self {
5355
Self {
56+
crate_items_only,
5457
doc_hidden_stack: vec![false],
5558
}
5659
}
@@ -128,6 +131,13 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
128131
}
129132

130133
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+
131141
match it.kind {
132142
hir::ItemKind::Fn(..) => {
133143
// ignore main()

clippy_lints/src/utils/conf.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,10 @@ 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+
/// FIXME: docs
460+
(only_check_missing_docs_in_crate_items: bool = false),
457461
}
458462

459463
/// Search for the configuration file.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
only-check-missing-docs-in-crate-items = true
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//! this is crate
2+
#![warn(clippy::missing_docs_in_private_items)]
3+
4+
/// this is mod
5+
mod my_mod {
6+
/// some docs
7+
fn priv_with_docs() {}
8+
fn priv_no_docs() {}
9+
/// some docs
10+
pub(crate) fn crate_with_docs() {}
11+
pub(crate) fn crate_no_docs() {}
12+
/// some docs
13+
pub(super) fn super_with_docs() {}
14+
pub(super) fn super_no_docs() {}
15+
16+
mod my_sub {
17+
/// some docs
18+
fn sub_priv_with_docs() {}
19+
fn sub_priv_no_docs() {}
20+
/// some docs
21+
pub(crate) fn sub_crate_with_docs() {}
22+
pub(crate) fn sub_crate_no_docs() {}
23+
/// some docs
24+
pub(super) fn sub_super_with_docs() {}
25+
pub(super) fn sub_super_no_docs() {}
26+
}
27+
}
28+
29+
fn main() {
30+
my_mod::crate_with_docs();
31+
my_mod::crate_no_docs();
32+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: missing documentation for a function
2+
--> $DIR/pub_crate_missing_doc.rs:11: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:14: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:22:9
17+
|
18+
LL | pub(crate) fn sub_crate_no_docs() {}
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
21+
error: aborting due to 3 previous errors
22+

0 commit comments

Comments
 (0)