2017-08-09 02:30:56 -05:00
|
|
|
// Note: More specifically this lint is largely inspired (aka copied) from
|
|
|
|
// *rustc*'s
|
2016-12-20 11:21:30 -06:00
|
|
|
// [`missing_doc`].
|
|
|
|
//
|
2020-12-20 10:19:49 -06:00
|
|
|
// [`missing_doc`]: https://github.com/rust-lang/rust/blob/cf9cf7c923eb01146971429044f216a3ca905e06/compiler/rustc_lint/src/builtin.rs#L415
|
2016-12-20 11:21:30 -06:00
|
|
|
//
|
2016-08-23 12:00:56 -05:00
|
|
|
|
2021-04-08 10:50:13 -05:00
|
|
|
use clippy_utils::attrs::is_doc_hidden;
|
2021-03-25 13:29:11 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint;
|
2022-08-11 12:42:16 -05:00
|
|
|
use clippy_utils::is_from_proc_macro;
|
2023-02-25 18:08:29 -06:00
|
|
|
use hir::def_id::LocalDefId;
|
|
|
|
use if_chain::if_chain;
|
2022-08-11 12:42:16 -05:00
|
|
|
use rustc_ast::ast::{self, MetaItem, MetaItemKind};
|
2020-01-06 10:39:50 -06:00
|
|
|
use rustc_hir as hir;
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
2023-02-22 09:51:17 -06:00
|
|
|
use rustc_middle::ty::Visibility;
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2021-09-12 04:58:27 -05:00
|
|
|
use rustc_span::def_id::CRATE_DEF_ID;
|
2020-01-04 04:00:00 -06:00
|
|
|
use rustc_span::source_map::Span;
|
2020-11-05 07:29:48 -06:00
|
|
|
use rustc_span::sym;
|
2016-08-23 11:09:37 -05:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Warns if there is missing doc for any documentable item
|
2019-03-05 10:50:33 -06:00
|
|
|
/// (public or private).
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Doc is good. *rustc* has a `MISSING_DOCS`
|
2019-03-05 10:50:33 -06:00
|
|
|
/// allowed-by-default lint for
|
|
|
|
/// public members, but has no way to enforce documentation of private items.
|
|
|
|
/// This lint fixes that.
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2016-08-23 11:09:37 -05:00
|
|
|
pub MISSING_DOCS_IN_PRIVATE_ITEMS,
|
2018-03-28 08:24:26 -05:00
|
|
|
restriction,
|
2016-08-23 11:09:37 -05:00
|
|
|
"detects missing documentation for public and private members"
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct MissingDoc {
|
2023-02-25 18:08:29 -06:00
|
|
|
/// Whether to **only** check for missing documentation in items visible within the current
|
|
|
|
/// crate. For example, `pub(crate)` items.
|
|
|
|
crate_items_only: bool,
|
2016-08-23 11:09:37 -05:00
|
|
|
/// Stack of whether #[doc(hidden)] is set
|
|
|
|
/// at each level which has lint attributes.
|
|
|
|
doc_hidden_stack: Vec<bool>,
|
|
|
|
}
|
|
|
|
|
2020-02-06 08:39:15 -06:00
|
|
|
impl Default for MissingDoc {
|
2019-09-18 01:37:41 -05:00
|
|
|
#[must_use]
|
2017-08-21 06:32:12 -05:00
|
|
|
fn default() -> Self {
|
2023-02-25 18:08:29 -06:00
|
|
|
Self::new(false)
|
2016-08-23 11:09:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MissingDoc {
|
2019-09-18 01:37:41 -05:00
|
|
|
#[must_use]
|
2023-02-25 18:08:29 -06:00
|
|
|
pub fn new(crate_items_only: bool) -> Self {
|
2017-09-05 04:33:04 -05:00
|
|
|
Self {
|
2023-02-25 18:08:29 -06:00
|
|
|
crate_items_only,
|
2017-09-05 04:33:04 -05:00
|
|
|
doc_hidden_stack: vec![false],
|
|
|
|
}
|
2016-08-23 11:09:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn doc_hidden(&self) -> bool {
|
2018-11-27 14:14:15 -06:00
|
|
|
*self.doc_hidden_stack.last().expect("empty doc_hidden_stack")
|
2016-08-23 11:09:37 -05:00
|
|
|
}
|
|
|
|
|
2022-08-11 12:42:16 -05:00
|
|
|
fn has_include(meta: Option<MetaItem>) -> bool {
|
|
|
|
if_chain! {
|
|
|
|
if let Some(meta) = meta;
|
|
|
|
if let MetaItemKind::List(list) = meta.kind;
|
|
|
|
if let Some(meta) = list.get(0);
|
|
|
|
if let Some(name) = meta.ident();
|
|
|
|
then {
|
|
|
|
name.name == sym::include
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-20 10:19:49 -06:00
|
|
|
fn check_missing_docs_attrs(
|
|
|
|
&self,
|
|
|
|
cx: &LateContext<'_>,
|
2023-02-25 18:08:29 -06:00
|
|
|
def_id: LocalDefId,
|
2020-12-20 10:19:49 -06:00
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
sp: Span,
|
|
|
|
article: &'static str,
|
|
|
|
desc: &'static str,
|
|
|
|
) {
|
2016-08-23 11:09:37 -05:00
|
|
|
// If we're building a test harness, then warning about
|
|
|
|
// documentation is probably not really relevant right now.
|
|
|
|
if cx.sess().opts.test {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// `#[doc(hidden)]` disables missing_docs check.
|
|
|
|
if self.doc_hidden() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-19 11:30:32 -05:00
|
|
|
if sp.from_expansion() {
|
2016-08-23 12:00:56 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-25 18:08:29 -06:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 12:42:16 -05:00
|
|
|
let has_doc = attrs
|
|
|
|
.iter()
|
|
|
|
.any(|a| a.doc_str().is_some() || Self::has_include(a.meta()));
|
2016-08-23 11:09:37 -05:00
|
|
|
if !has_doc {
|
2018-08-28 06:13:42 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
2017-08-09 02:30:56 -05:00
|
|
|
MISSING_DOCS_IN_PRIVATE_ITEMS,
|
|
|
|
sp,
|
2022-10-06 02:44:38 -05:00
|
|
|
&format!("missing documentation for {article} {desc}"),
|
2017-08-09 02:30:56 -05:00
|
|
|
);
|
2016-08-23 11:09:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
impl_lint_pass!(MissingDoc => [MISSING_DOCS_IN_PRIVATE_ITEMS]);
|
2016-08-23 11:09:37 -05:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for MissingDoc {
|
|
|
|
fn enter_lint_attrs(&mut self, _: &LateContext<'tcx>, attrs: &'tcx [ast::Attribute]) {
|
2021-04-08 10:50:13 -05:00
|
|
|
let doc_hidden = self.doc_hidden() || is_doc_hidden(attrs);
|
2016-08-23 11:09:37 -05:00
|
|
|
self.doc_hidden_stack.push(doc_hidden);
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn exit_lint_attrs(&mut self, _: &LateContext<'tcx>, _: &'tcx [ast::Attribute]) {
|
2016-08-23 11:09:37 -05:00
|
|
|
self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
|
|
|
|
}
|
|
|
|
|
2021-09-12 04:58:27 -05:00
|
|
|
fn check_crate(&mut self, cx: &LateContext<'tcx>) {
|
2020-11-26 16:38:53 -06:00
|
|
|
let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID);
|
2023-02-25 18:08:29 -06:00
|
|
|
self.check_missing_docs_attrs(cx, CRATE_DEF_ID, attrs, cx.tcx.def_span(CRATE_DEF_ID), "the", "crate");
|
2016-08-23 11:09:37 -05:00
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
|
2020-12-20 10:19:49 -06:00
|
|
|
match it.kind {
|
2018-07-16 08:07:39 -05:00
|
|
|
hir::ItemKind::Fn(..) => {
|
2018-01-14 00:05:07 -06:00
|
|
|
// ignore main()
|
2020-11-05 07:29:48 -06:00
|
|
|
if it.ident.name == sym::main {
|
2022-10-26 22:02:18 -05:00
|
|
|
let at_root = cx.tcx.local_parent(it.owner_id.def_id) == CRATE_DEF_ID;
|
2022-04-15 12:27:53 -05:00
|
|
|
if at_root {
|
2018-01-14 00:05:07 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-12-20 10:19:49 -06:00
|
|
|
hir::ItemKind::Const(..)
|
|
|
|
| hir::ItemKind::Enum(..)
|
2021-07-31 01:50:57 -05:00
|
|
|
| hir::ItemKind::Macro(..)
|
2021-08-05 18:58:46 -05:00
|
|
|
| hir::ItemKind::Mod(..)
|
2020-12-20 10:19:49 -06:00
|
|
|
| hir::ItemKind::Static(..)
|
|
|
|
| hir::ItemKind::Struct(..)
|
|
|
|
| hir::ItemKind::Trait(..)
|
|
|
|
| hir::ItemKind::TraitAlias(..)
|
|
|
|
| hir::ItemKind::TyAlias(..)
|
|
|
|
| hir::ItemKind::Union(..)
|
2022-09-06 12:27:47 -05:00
|
|
|
| hir::ItemKind::OpaqueTy(..) => {},
|
2018-11-27 14:14:15 -06:00
|
|
|
hir::ItemKind::ExternCrate(..)
|
2020-11-11 15:40:09 -06:00
|
|
|
| hir::ItemKind::ForeignMod { .. }
|
2019-01-13 10:09:58 -06:00
|
|
|
| hir::ItemKind::GlobalAsm(..)
|
2020-01-17 23:14:36 -06:00
|
|
|
| hir::ItemKind::Impl { .. }
|
2018-11-27 14:14:15 -06:00
|
|
|
| hir::ItemKind::Use(..) => return,
|
2016-08-23 11:09:37 -05:00
|
|
|
};
|
|
|
|
|
2022-10-26 22:02:18 -05:00
|
|
|
let (article, desc) = cx.tcx.article_and_description(it.owner_id.to_def_id());
|
2020-12-20 10:19:49 -06:00
|
|
|
|
2021-01-24 06:17:54 -06:00
|
|
|
let attrs = cx.tcx.hir().attrs(it.hir_id());
|
2022-08-11 12:42:16 -05:00
|
|
|
if !is_from_proc_macro(cx, it) {
|
2023-02-25 18:08:29 -06:00
|
|
|
self.check_missing_docs_attrs(cx, it.owner_id.def_id, attrs, it.span, article, desc);
|
2022-08-11 12:42:16 -05:00
|
|
|
}
|
2016-08-23 11:09:37 -05:00
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, trait_item: &'tcx hir::TraitItem<'_>) {
|
2022-10-26 22:02:18 -05:00
|
|
|
let (article, desc) = cx.tcx.article_and_description(trait_item.owner_id.to_def_id());
|
2016-08-23 11:09:37 -05:00
|
|
|
|
2020-11-27 02:41:53 -06:00
|
|
|
let attrs = cx.tcx.hir().attrs(trait_item.hir_id());
|
2022-08-11 12:42:16 -05:00
|
|
|
if !is_from_proc_macro(cx, trait_item) {
|
2023-02-25 18:08:29 -06:00
|
|
|
self.check_missing_docs_attrs(cx, trait_item.owner_id.def_id, attrs, trait_item.span, article, desc);
|
2022-08-11 12:42:16 -05:00
|
|
|
}
|
2016-08-23 11:09:37 -05:00
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
|
2016-08-23 11:09:37 -05:00
|
|
|
// If the method is an impl for a trait, don't doc.
|
2022-10-26 22:02:18 -05:00
|
|
|
if let Some(cid) = cx.tcx.associated_item(impl_item.owner_id).impl_container(cx.tcx) {
|
2023-01-10 15:57:22 -06:00
|
|
|
if cx.tcx.impl_trait_ref(cid).is_some() {
|
2022-03-12 17:52:25 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return;
|
2016-08-23 11:09:37 -05:00
|
|
|
}
|
|
|
|
|
2022-10-26 22:02:18 -05:00
|
|
|
let (article, desc) = cx.tcx.article_and_description(impl_item.owner_id.to_def_id());
|
2020-11-27 02:55:10 -06:00
|
|
|
let attrs = cx.tcx.hir().attrs(impl_item.hir_id());
|
2022-08-11 12:42:16 -05:00
|
|
|
if !is_from_proc_macro(cx, impl_item) {
|
2023-02-25 18:08:29 -06:00
|
|
|
self.check_missing_docs_attrs(cx, impl_item.owner_id.def_id, attrs, impl_item.span, article, desc);
|
2022-08-11 12:42:16 -05:00
|
|
|
}
|
2016-08-23 11:09:37 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 16:36:07 -05:00
|
|
|
fn check_field_def(&mut self, cx: &LateContext<'tcx>, sf: &'tcx hir::FieldDef<'_>) {
|
2016-08-23 11:09:37 -05:00
|
|
|
if !sf.is_positional() {
|
2020-11-26 17:27:34 -06:00
|
|
|
let attrs = cx.tcx.hir().attrs(sf.hir_id);
|
2022-08-11 12:42:16 -05:00
|
|
|
if !is_from_proc_macro(cx, sf) {
|
2023-02-25 18:08:29 -06:00
|
|
|
self.check_missing_docs_attrs(cx, sf.def_id, attrs, sf.span, "a", "struct field");
|
2022-08-11 12:42:16 -05:00
|
|
|
}
|
2016-08-23 11:09:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
fn check_variant(&mut self, cx: &LateContext<'tcx>, v: &'tcx hir::Variant<'_>) {
|
2022-11-06 13:46:55 -06:00
|
|
|
let attrs = cx.tcx.hir().attrs(v.hir_id);
|
2022-08-11 12:42:16 -05:00
|
|
|
if !is_from_proc_macro(cx, v) {
|
2023-02-25 18:08:29 -06:00
|
|
|
self.check_missing_docs_attrs(cx, v.def_id, attrs, v.span, "a", "variant");
|
2022-08-11 12:42:16 -05:00
|
|
|
}
|
2016-08-23 11:09:37 -05:00
|
|
|
}
|
|
|
|
}
|