rust-clippy/clippy_lints/src/missing_doc.rs

199 lines
6.5 KiB
Rust
Raw Normal View History

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