about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-02-20 23:59:07 +0000
committerbors <bors@rust-lang.org>2023-02-20 23:59:07 +0000
commitb1cf1e7b6acecfdf773dd24c09cb4cf27507481f (patch)
tree4af682816e1539c1698092e6e59b5cbfed129968
parent574c8aeeb5320a672c4956a8749d577637ad7712 (diff)
parent790f28b1533cbc1aec7ee343b98ea5d570f9928e (diff)
downloadrust-b1cf1e7b6acecfdf773dd24c09cb4cf27507481f.tar.gz
rust-b1cf1e7b6acecfdf773dd24c09cb4cf27507481f.zip
Auto merge of #10303 - pvdrz:pub_crate_missing_docs, r=giraffate
Add configuration to lint missing docs of `pub(crate)` items

Fixes this: https://github.com/rust-lang/rust-clippy/issues/5736#issuecomment-1412442404

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.
[#10303](https://github.com/rust-lang/rust-clippy/pull/10303)
<!-- changelog_checked -->
-rw-r--r--book/src/lint_configuration.md10
-rw-r--r--clippy_lints/src/lib.rs3
-rw-r--r--clippy_lints/src/missing_doc.rs31
-rw-r--r--clippy_lints/src/utils/conf.rs5
-rw-r--r--tests/ui-toml/pub_crate_missing_docs/clippy.toml1
-rw-r--r--tests/ui-toml/pub_crate_missing_docs/pub_crate_missing_doc.rs59
-rw-r--r--tests/ui-toml/pub_crate_missing_docs/pub_crate_missing_doc.stderr52
-rw-r--r--tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr1
8 files changed, 152 insertions, 10 deletions
diff --git a/book/src/lint_configuration.md b/book/src/lint_configuration.md
index 32e8e218c40..f74431bee82 100644
--- a/book/src/lint_configuration.md
+++ b/book/src/lint_configuration.md
@@ -53,6 +53,7 @@ Please use that command to update the file and do not edit it by hand.
 | [ignore-interior-mutability](#ignore-interior-mutability) | `["bytes::Bytes"]` |
 | [allow-mixed-uninlined-format-args](#allow-mixed-uninlined-format-args) | `true` |
 | [suppress-restriction-lint-in-const](#suppress-restriction-lint-in-const) | `false` |
+| [missing-docs-in-crate-items](#missing-docs-in-crate-items) | `false` |
 
 ### arithmetic-side-effects-allowed
 Suppress checking of the passed type names in all types of operations.
@@ -540,4 +541,13 @@ if no suggestion can be made.
 * [indexing_slicing](https://rust-lang.github.io/rust-clippy/master/index.html#indexing_slicing)
 
 
+### missing-docs-in-crate-items
+Whether to **only** check for missing documentation in items visible within the current
+crate. For example, `pub(crate)` items.
+
+**Default Value:** `false` (`bool`)
+
+* [missing_docs_in_private_items](https://rust-lang.github.io/rust-clippy/master/index.html#missing_docs_in_private_items)
+
+
 
diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs
index ce5df8c7a7b..7336fa19b12 100644
--- a/clippy_lints/src/lib.rs
+++ b/clippy_lints/src/lib.rs
@@ -668,12 +668,13 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
         ))
     });
     let doc_valid_idents = conf.doc_valid_idents.iter().cloned().collect::<FxHashSet<_>>();
+    let missing_docs_in_crate_items = conf.missing_docs_in_crate_items;
     store.register_late_pass(move |_| Box::new(doc::DocMarkdown::new(doc_valid_idents.clone())));
     store.register_late_pass(|_| Box::new(neg_multiply::NegMultiply));
     store.register_late_pass(|_| Box::new(mem_forget::MemForget));
     store.register_late_pass(|_| Box::new(let_if_seq::LetIfSeq));
     store.register_late_pass(|_| Box::new(mixed_read_write_in_expression::EvalOrderDependence));
-    store.register_late_pass(|_| Box::new(missing_doc::MissingDoc::new()));
+    store.register_late_pass(move |_| Box::new(missing_doc::MissingDoc::new(missing_docs_in_crate_items)));
     store.register_late_pass(|_| Box::new(missing_inline::MissingInline));
     store.register_late_pass(move |_| Box::new(exhaustive_items::ExhaustiveItems));
     store.register_late_pass(|_| Box::new(match_result_ok::MatchResultOk));
diff --git a/clippy_lints/src/missing_doc.rs b/clippy_lints/src/missing_doc.rs
index 9942e8115b7..9659ca8ced2 100644
--- a/clippy_lints/src/missing_doc.rs
+++ b/clippy_lints/src/missing_doc.rs
@@ -8,11 +8,12 @@
 use clippy_utils::attrs::is_doc_hidden;
 use clippy_utils::diagnostics::span_lint;
 use clippy_utils::is_from_proc_macro;
+use hir::def_id::LocalDefId;
 use if_chain::if_chain;
 use rustc_ast::ast::{self, MetaItem, MetaItemKind};
 use rustc_hir as hir;
 use rustc_lint::{LateContext, LateLintPass, LintContext};
-use rustc_middle::ty::DefIdTree;
+use rustc_middle::ty::{DefIdTree, Visibility};
 use rustc_session::{declare_tool_lint, impl_lint_pass};
 use rustc_span::def_id::CRATE_DEF_ID;
 use rustc_span::source_map::Span;
@@ -35,6 +36,9 @@ declare_clippy_lint! {
 }
 
 pub struct MissingDoc {
+    /// Whether to **only** check for missing documentation in items visible within the current
+    /// crate. For example, `pub(crate)` items.
+    crate_items_only: bool,
     /// Stack of whether #[doc(hidden)] is set
     /// at each level which has lint attributes.
     doc_hidden_stack: Vec<bool>,
@@ -43,14 +47,15 @@ pub struct MissingDoc {
 impl Default for MissingDoc {
     #[must_use]
     fn default() -> Self {
-        Self::new()
+        Self::new(false)
     }
 }
 
 impl MissingDoc {
     #[must_use]
-    pub fn new() -> Self {
+    pub fn new(crate_items_only: bool) -> Self {
         Self {
+            crate_items_only,
             doc_hidden_stack: vec![false],
         }
     }
@@ -76,6 +81,7 @@ impl MissingDoc {
     fn check_missing_docs_attrs(
         &self,
         cx: &LateContext<'_>,
+        def_id: LocalDefId,
         attrs: &[ast::Attribute],
         sp: Span,
         article: &'static str,
@@ -96,6 +102,13 @@ impl MissingDoc {
             return;
         }
 
+        if self.crate_items_only && def_id != CRATE_DEF_ID {
+            let vis = cx.tcx.visibility(def_id);
+            if vis == Visibility::Public || vis != Visibility::Restricted(CRATE_DEF_ID.into()) {
+                return;
+            }
+        }
+
         let has_doc = attrs
             .iter()
             .any(|a| a.doc_str().is_some() || Self::has_include(a.meta()));
@@ -124,7 +137,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
 
     fn check_crate(&mut self, cx: &LateContext<'tcx>) {
         let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID);
-        self.check_missing_docs_attrs(cx, attrs, cx.tcx.def_span(CRATE_DEF_ID), "the", "crate");
+        self.check_missing_docs_attrs(cx, CRATE_DEF_ID, attrs, cx.tcx.def_span(CRATE_DEF_ID), "the", "crate");
     }
 
     fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
@@ -160,7 +173,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
 
         let attrs = cx.tcx.hir().attrs(it.hir_id());
         if !is_from_proc_macro(cx, it) {
-            self.check_missing_docs_attrs(cx, attrs, it.span, article, desc);
+            self.check_missing_docs_attrs(cx, it.owner_id.def_id, attrs, it.span, article, desc);
         }
     }
 
@@ -169,7 +182,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
 
         let attrs = cx.tcx.hir().attrs(trait_item.hir_id());
         if !is_from_proc_macro(cx, trait_item) {
-            self.check_missing_docs_attrs(cx, attrs, trait_item.span, article, desc);
+            self.check_missing_docs_attrs(cx, trait_item.owner_id.def_id, attrs, trait_item.span, article, desc);
         }
     }
 
@@ -186,7 +199,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
         let (article, desc) = cx.tcx.article_and_description(impl_item.owner_id.to_def_id());
         let attrs = cx.tcx.hir().attrs(impl_item.hir_id());
         if !is_from_proc_macro(cx, impl_item) {
-            self.check_missing_docs_attrs(cx, attrs, impl_item.span, article, desc);
+            self.check_missing_docs_attrs(cx, impl_item.owner_id.def_id, attrs, impl_item.span, article, desc);
         }
     }
 
@@ -194,7 +207,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
         if !sf.is_positional() {
             let attrs = cx.tcx.hir().attrs(sf.hir_id);
             if !is_from_proc_macro(cx, sf) {
-                self.check_missing_docs_attrs(cx, attrs, sf.span, "a", "struct field");
+                self.check_missing_docs_attrs(cx, sf.def_id, attrs, sf.span, "a", "struct field");
             }
         }
     }
@@ -202,7 +215,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
     fn check_variant(&mut self, cx: &LateContext<'tcx>, v: &'tcx hir::Variant<'_>) {
         let attrs = cx.tcx.hir().attrs(v.hir_id);
         if !is_from_proc_macro(cx, v) {
-            self.check_missing_docs_attrs(cx, attrs, v.span, "a", "variant");
+            self.check_missing_docs_attrs(cx, v.def_id, attrs, v.span, "a", "variant");
         }
     }
 }
diff --git a/clippy_lints/src/utils/conf.rs b/clippy_lints/src/utils/conf.rs
index 1d78c7cfae0..c3c73ac6e37 100644
--- a/clippy_lints/src/utils/conf.rs
+++ b/clippy_lints/src/utils/conf.rs
@@ -454,6 +454,11 @@ define_Conf! {
     /// configuration will cause restriction lints to trigger even
     /// if no suggestion can be made.
     (suppress_restriction_lint_in_const: bool = false),
+    /// Lint: MISSING_DOCS_IN_PRIVATE_ITEMS.
+    ///
+    /// Whether to **only** check for missing documentation in items visible within the current
+    /// crate. For example, `pub(crate)` items.
+    (missing_docs_in_crate_items: bool = false),
 }
 
 /// Search for the configuration file.
diff --git a/tests/ui-toml/pub_crate_missing_docs/clippy.toml b/tests/ui-toml/pub_crate_missing_docs/clippy.toml
new file mode 100644
index 00000000000..ec210a98783
--- /dev/null
+++ b/tests/ui-toml/pub_crate_missing_docs/clippy.toml
@@ -0,0 +1 @@
+missing-docs-in-crate-items = true
diff --git a/tests/ui-toml/pub_crate_missing_docs/pub_crate_missing_doc.rs b/tests/ui-toml/pub_crate_missing_docs/pub_crate_missing_doc.rs
new file mode 100644
index 00000000000..830d71f61dd
--- /dev/null
+++ b/tests/ui-toml/pub_crate_missing_docs/pub_crate_missing_doc.rs
@@ -0,0 +1,59 @@
+//! this is crate
+#![allow(missing_docs)]
+#![warn(clippy::missing_docs_in_private_items)]
+
+/// this is mod
+mod my_mod {
+    /// some docs
+    fn priv_with_docs() {}
+    fn priv_no_docs() {}
+    /// some docs
+    pub(crate) fn crate_with_docs() {}
+    pub(crate) fn crate_no_docs() {}
+    /// some docs
+    pub(super) fn super_with_docs() {}
+    pub(super) fn super_no_docs() {}
+
+    mod my_sub {
+        /// some docs
+        fn sub_priv_with_docs() {}
+        fn sub_priv_no_docs() {}
+        /// some docs
+        pub(crate) fn sub_crate_with_docs() {}
+        pub(crate) fn sub_crate_no_docs() {}
+        /// some docs
+        pub(super) fn sub_super_with_docs() {}
+        pub(super) fn sub_super_no_docs() {}
+    }
+
+    /// some docs
+    pub(crate) struct CrateStructWithDocs {
+        /// some docs
+        pub(crate) crate_field_with_docs: (),
+        pub(crate) crate_field_no_docs: (),
+        /// some docs
+        priv_field_with_docs: (),
+        priv_field_no_docs: (),
+    }
+
+    pub(crate) struct CrateStructNoDocs {
+        /// some docs
+        pub(crate) crate_field_with_docs: (),
+        pub(crate) crate_field_no_docs: (),
+        /// some docs
+        priv_field_with_docs: (),
+        priv_field_no_docs: (),
+    }
+}
+
+/// some docs
+type CrateTypedefWithDocs = String;
+type CrateTypedefNoDocs = String;
+/// some docs
+pub type PubTypedefWithDocs = String;
+pub type PubTypedefNoDocs = String;
+
+fn main() {
+    my_mod::crate_with_docs();
+    my_mod::crate_no_docs();
+}
diff --git a/tests/ui-toml/pub_crate_missing_docs/pub_crate_missing_doc.stderr b/tests/ui-toml/pub_crate_missing_docs/pub_crate_missing_doc.stderr
new file mode 100644
index 00000000000..a474187050c
--- /dev/null
+++ b/tests/ui-toml/pub_crate_missing_docs/pub_crate_missing_doc.stderr
@@ -0,0 +1,52 @@
+error: missing documentation for a function
+  --> $DIR/pub_crate_missing_doc.rs:12:5
+   |
+LL |     pub(crate) fn crate_no_docs() {}
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: `-D clippy::missing-docs-in-private-items` implied by `-D warnings`
+
+error: missing documentation for a function
+  --> $DIR/pub_crate_missing_doc.rs:15:5
+   |
+LL |     pub(super) fn super_no_docs() {}
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: missing documentation for a function
+  --> $DIR/pub_crate_missing_doc.rs:23:9
+   |
+LL |         pub(crate) fn sub_crate_no_docs() {}
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: missing documentation for a struct field
+  --> $DIR/pub_crate_missing_doc.rs:33:9
+   |
+LL |         pub(crate) crate_field_no_docs: (),
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: missing documentation for a struct
+  --> $DIR/pub_crate_missing_doc.rs:39:5
+   |
+LL | /     pub(crate) struct CrateStructNoDocs {
+LL | |         /// some docs
+LL | |         pub(crate) crate_field_with_docs: (),
+LL | |         pub(crate) crate_field_no_docs: (),
+...  |
+LL | |         priv_field_no_docs: (),
+LL | |     }
+   | |_____^
+
+error: missing documentation for a struct field
+  --> $DIR/pub_crate_missing_doc.rs:42:9
+   |
+LL |         pub(crate) crate_field_no_docs: (),
+   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: missing documentation for a type alias
+  --> $DIR/pub_crate_missing_doc.rs:51:1
+   |
+LL | type CrateTypedefNoDocs = String;
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: aborting due to 7 previous errors
+
diff --git a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr
index a22c6a5a060..6a246afac76 100644
--- a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr
+++ b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr
@@ -33,6 +33,7 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown fie
            max-struct-bools
            max-suggested-slice-pattern-length
            max-trait-bounds
+           missing-docs-in-crate-items
            msrv
            pass-by-value-size-limit
            single-char-binding-names-threshold