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`].
|
|
|
|
//
|
2017-09-20 21:59:23 +00:00
|
|
|
// [`missing_doc`]: https://github.com/rust-lang/rust/blob/d6d05904697d89099b55da3331155392f1db9c00/src/librustc_lint/builtin.rs#L246
|
2016-12-20 17:21:30 +00:00
|
|
|
//
|
2016-08-23 17:00:56 +00:00
|
|
|
|
2019-08-19 16:30:32 +00:00
|
|
|
use crate::utils::span_lint;
|
2019-03-14 17:32:39 +00:00
|
|
|
use if_chain::if_chain;
|
2020-03-01 03:23:33 +00:00
|
|
|
use rustc_ast::ast::{self, MetaItem, MetaItemKind};
|
|
|
|
use rustc_ast::attr;
|
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};
|
2020-03-30 09:02:14 +00:00
|
|
|
use rustc_middle::ty;
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2020-01-04 10:00:00 +00:00
|
|
|
use rustc_span::source_map::Span;
|
2016-08-23 16:09:37 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **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.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
2016-08-23 16:09:37 +00:00
|
|
|
pub MISSING_DOCS_IN_PRIVATE_ITEMS,
|
2018-03-28 13:24:26 +00:00
|
|
|
restriction,
|
2016-08-23 16:09:37 +00:00
|
|
|
"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>,
|
|
|
|
}
|
|
|
|
|
2020-02-06 14:39:15 +00:00
|
|
|
impl Default for MissingDoc {
|
2019-09-18 06:37:41 +00:00
|
|
|
#[must_use]
|
2017-08-21 11:32:12 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
2016-08-23 16:09:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MissingDoc {
|
2019-09-18 06:37:41 +00:00
|
|
|
#[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],
|
|
|
|
}
|
2016-08-23 16:09:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn doc_hidden(&self) -> bool {
|
2018-11-27 20:14:15 +00:00
|
|
|
*self.doc_hidden_stack.last().expect("empty doc_hidden_stack")
|
2016-08-23 16:09:37 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 17:32:39 +00:00
|
|
|
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;
|
2019-03-14 17:32:39 +00:00
|
|
|
if let Some(meta) = list.get(0);
|
2019-03-26 09:55:03 +00:00
|
|
|
if let Some(name) = meta.ident();
|
2019-03-14 17:32:39 +00:00
|
|
|
then {
|
2019-03-26 09:55:03 +00:00
|
|
|
name.as_str() == "include"
|
2019-03-14 17:32:39 +00:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 20:14:15 +00:00
|
|
|
fn check_missing_docs_attrs(
|
|
|
|
&self,
|
|
|
|
cx: &LateContext<'_, '_>,
|
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
sp: Span,
|
|
|
|
desc: &'static str,
|
|
|
|
) {
|
2016-08-23 16:09:37 +00: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 16:30:32 +00:00
|
|
|
if sp.from_expansion() {
|
2016-08-23 17:00:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-14 17:32:39 +00:00
|
|
|
let has_doc = attrs
|
|
|
|
.iter()
|
2019-12-29 02:31:41 +00:00
|
|
|
.any(|a| a.is_doc_comment() || a.doc_str().is_some() || a.is_value_str() || Self::has_include(a.meta()));
|
2016-08-23 16:09:37 +00:00
|
|
|
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 {}", desc),
|
|
|
|
);
|
2016-08-23 16:09:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
impl_lint_pass!(MissingDoc => [MISSING_DOCS_IN_PRIVATE_ITEMS]);
|
2016-08-23 16:09:37 +00:00
|
|
|
|
2016-12-07 12:13:40 +00:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
|
|
|
|
fn enter_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, attrs: &'tcx [ast::Attribute]) {
|
2018-11-27 20:14:15 +00:00
|
|
|
let doc_hidden = self.doc_hidden()
|
|
|
|
|| attrs.iter().any(|attr| {
|
2019-05-17 21:53:54 +00:00
|
|
|
attr.check_name(sym!(doc))
|
2018-11-27 20:14:15 +00:00
|
|
|
&& match attr.meta_item_list() {
|
|
|
|
None => false,
|
2019-05-17 21:53:54 +00:00
|
|
|
Some(l) => attr::list_contains_name(&l[..], sym!(hidden)),
|
2018-11-27 20:14:15 +00:00
|
|
|
}
|
|
|
|
});
|
2016-08-23 16:09:37 +00:00
|
|
|
self.doc_hidden_stack.push(doc_hidden);
|
|
|
|
}
|
|
|
|
|
2016-12-07 12:13:40 +00:00
|
|
|
fn exit_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx [ast::Attribute]) {
|
2016-08-23 16:09:37 +00:00
|
|
|
self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
|
|
|
|
}
|
|
|
|
|
2019-12-22 14:42:41 +00:00
|
|
|
fn check_crate(&mut self, cx: &LateContext<'a, 'tcx>, krate: &'tcx hir::Crate<'_>) {
|
2020-03-15 22:41:20 +00:00
|
|
|
self.check_missing_docs_attrs(cx, &krate.item.attrs, krate.item.span, "crate");
|
2016-08-23 16:09:37 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 14:42:41 +00:00
|
|
|
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item<'_>) {
|
2019-09-27 15:16:06 +00:00
|
|
|
let desc = match it.kind {
|
2018-07-16 13:07:39 +00:00
|
|
|
hir::ItemKind::Const(..) => "a constant",
|
|
|
|
hir::ItemKind::Enum(..) => "an enum",
|
|
|
|
hir::ItemKind::Fn(..) => {
|
2018-01-14 06:05:07 +00:00
|
|
|
// ignore main()
|
2019-05-17 21:53:54 +00:00
|
|
|
if it.ident.name == sym!(main) {
|
2020-03-19 13:33:10 +00:00
|
|
|
let def_id = it.hir_id.owner;
|
2018-12-08 00:56:03 +00:00
|
|
|
let def_key = cx.tcx.hir().def_key(def_id);
|
2018-01-14 06:05:07 +00:00
|
|
|
if def_key.parent == Some(hir::def_id::CRATE_DEF_INDEX) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"a function"
|
|
|
|
},
|
2018-07-16 13:07:39 +00:00
|
|
|
hir::ItemKind::Mod(..) => "a module",
|
|
|
|
hir::ItemKind::Static(..) => "a static",
|
|
|
|
hir::ItemKind::Struct(..) => "a struct",
|
|
|
|
hir::ItemKind::Trait(..) => "a trait",
|
|
|
|
hir::ItemKind::TraitAlias(..) => "a trait alias",
|
2019-08-05 05:30:01 +00:00
|
|
|
hir::ItemKind::TyAlias(..) => "a type alias",
|
2018-07-16 13:07:39 +00:00
|
|
|
hir::ItemKind::Union(..) => "a union",
|
2019-08-03 06:44:32 +00:00
|
|
|
hir::ItemKind::OpaqueTy(..) => "an existential type",
|
2018-11-27 20:14:15 +00:00
|
|
|
hir::ItemKind::ExternCrate(..)
|
|
|
|
| hir::ItemKind::ForeignMod(..)
|
2019-01-13 16:09:58 +00:00
|
|
|
| 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,
|
2016-08-23 16:09:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
|
|
|
|
}
|
|
|
|
|
2019-12-22 14:42:41 +00:00
|
|
|
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx hir::TraitItem<'_>) {
|
2019-09-27 15:16:06 +00:00
|
|
|
let desc = match trait_item.kind {
|
2017-01-03 17:19:17 +00:00
|
|
|
hir::TraitItemKind::Const(..) => "an associated constant",
|
2020-03-12 19:56:55 +00:00
|
|
|
hir::TraitItemKind::Fn(..) => "a trait method",
|
2017-01-03 17:19:17 +00:00
|
|
|
hir::TraitItemKind::Type(..) => "an associated type",
|
2016-08-23 16:09:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
|
|
|
|
}
|
|
|
|
|
2019-12-22 14:42:41 +00:00
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
|
2016-08-23 16:09:37 +00:00
|
|
|
// If the method is an impl for a trait, don't doc.
|
2019-07-06 03:52:51 +00:00
|
|
|
let def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
|
2016-11-16 20:57:56 +00:00
|
|
|
match cx.tcx.associated_item(def_id).container {
|
2016-08-23 16:09:37 +00:00
|
|
|
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
|
|
|
},
|
2016-08-23 16:09:37 +00:00
|
|
|
}
|
|
|
|
|
2019-09-27 15:16:06 +00:00
|
|
|
let desc = match impl_item.kind {
|
2016-08-23 16:09:37 +00:00
|
|
|
hir::ImplItemKind::Const(..) => "an associated constant",
|
2020-03-16 15:00:16 +00:00
|
|
|
hir::ImplItemKind::Fn(..) => "a method",
|
2019-08-05 05:30:01 +00:00
|
|
|
hir::ImplItemKind::TyAlias(_) => "an associated type",
|
2016-08-23 16:09:37 +00:00
|
|
|
};
|
|
|
|
self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
|
|
|
|
}
|
|
|
|
|
2019-12-22 14:42:41 +00:00
|
|
|
fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, sf: &'tcx hir::StructField<'_>) {
|
2016-08-23 16:09:37 +00:00
|
|
|
if !sf.is_positional() {
|
|
|
|
self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a struct field");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 14:42:41 +00:00
|
|
|
fn check_variant(&mut self, cx: &LateContext<'a, 'tcx>, v: &'tcx hir::Variant<'_>) {
|
2019-08-15 07:59:08 +00:00
|
|
|
self.check_missing_docs_attrs(cx, &v.attrs, v.span, "a variant");
|
2016-08-23 16:09:37 +00:00
|
|
|
}
|
|
|
|
}
|