Add docs and update tests

This commit is contained in:
Christian Poveda 2023-02-09 16:41:45 -05:00
parent 8eac9e359e
commit e2e23c0045
No known key found for this signature in database
GPG Key ID: 27525EF5E7420A50
8 changed files with 66 additions and 21 deletions

View File

@ -665,13 +665,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 only_check_missing_docs_in_crate_items = conf.only_check_missing_docs_in_crate_items;
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(move |_| Box::new(missing_doc::MissingDoc::new(only_check_missing_docs_in_crate_items)));
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));

View File

@ -8,6 +8,7 @@
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;
@ -35,7 +36,7 @@
}
pub struct MissingDoc {
/// FIXME: docs
/// Whether to only check for missing docs in `pub(crate)` items.
crate_items_only: bool,
/// Stack of whether #[doc(hidden)] is set
/// at each level which has lint attributes.
@ -79,6 +80,7 @@ fn has_include(meta: Option<MetaItem>) -> bool {
fn check_missing_docs_attrs(
&self,
cx: &LateContext<'_>,
def_id: LocalDefId,
attrs: &[ast::Attribute],
sp: Span,
article: &'static str,
@ -99,6 +101,13 @@ fn check_missing_docs_attrs(
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()));
@ -127,17 +136,10 @@ fn exit_lint_attrs(&mut self, _: &LateContext<'tcx>, _: &'tcx [ast::Attribute])
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<'_>) {
if self.crate_items_only {
let vis = cx.tcx.visibility(it.owner_id.to_def_id());
if vis != Visibility::Public && vis != Visibility::Restricted(CRATE_DEF_ID.into()) {
return;
}
}
match it.kind {
hir::ItemKind::Fn(..) => {
// ignore main()
@ -170,7 +172,7 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
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);
}
}
@ -179,7 +181,7 @@ fn check_trait_item(&mut self, cx: &LateContext<'tcx>, trait_item: &'tcx hir::Tr
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);
}
}
@ -196,7 +198,7 @@ fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::Impl
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);
}
}
@ -204,7 +206,7 @@ fn check_field_def(&mut self, cx: &LateContext<'tcx>, sf: &'tcx hir::FieldDef<'_
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");
}
}
}
@ -212,7 +214,7 @@ fn check_field_def(&mut self, cx: &LateContext<'tcx>, sf: &'tcx hir::FieldDef<'_
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");
}
}
}

View File

@ -456,8 +456,8 @@ pub(crate) fn get_configuration_metadata() -> Vec<ClippyConfiguration> {
(suppress_restriction_lint_in_const: bool = false),
/// Lint: MISSING_DOCS_IN_PRIVATE_ITEMS.
///
/// FIXME: docs
(only_check_missing_docs_in_crate_items: bool = false),
/// Whether to **only** check for missing docmuentation in `pub(crate)` items.
(missing_docs_in_crate_items: bool = false),
}
/// Search for the configuration file.

View File

@ -1 +1 @@
only-check-missing-docs-in-crate-items = true
missing-docs-in-crate-items = true

View File

@ -24,6 +24,25 @@ pub(crate) fn sub_crate_no_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: (),
}
}
fn main() {

View File

@ -18,5 +18,29 @@ error: missing documentation for a function
LL | pub(crate) fn sub_crate_no_docs() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 3 previous errors
error: missing documentation for a struct field
--> $DIR/pub_crate_missing_doc.rs:32:9
|
LL | pub(crate) crate_field_no_docs: (),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: missing documentation for a struct
--> $DIR/pub_crate_missing_doc.rs:38: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:41:9
|
LL | pub(crate) crate_field_no_docs: (),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 6 previous errors

View File

@ -33,8 +33,8 @@ 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
only-check-missing-docs-in-crate-items
pass-by-value-size-limit
single-char-binding-names-threshold
standard-macro-braces