2016-12-20 11:21:30 -06:00
|
|
|
// This file incorporates work covered by the following copyright and
|
|
|
|
// permission notice:
|
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
//
|
|
|
|
|
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`].
|
|
|
|
//
|
2017-09-20 16:59:23 -05:00
|
|
|
// [`missing_doc`]: https://github.com/rust-lang/rust/blob/d6d05904697d89099b55da3331155392f1db9c00/src/librustc_lint/builtin.rs#L246
|
2016-12-20 11:21:30 -06:00
|
|
|
//
|
2016-08-23 12:00:56 -05:00
|
|
|
|
2016-08-23 11:09:37 -05:00
|
|
|
use rustc::hir;
|
2018-08-28 06:13:42 -05:00
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass, LintContext};
|
2018-07-29 04:04:40 -05:00
|
|
|
use rustc::{declare_tool_lint, lint_array};
|
2016-08-23 11:09:37 -05:00
|
|
|
use rustc::ty;
|
|
|
|
use syntax::ast;
|
2016-08-28 10:54:32 -05:00
|
|
|
use syntax::attr;
|
2018-08-19 21:06:53 -05:00
|
|
|
use syntax::source_map::Span;
|
2018-08-28 06:13:42 -05:00
|
|
|
use crate::utils::{span_lint, in_macro};
|
2016-08-23 11:09:37 -05:00
|
|
|
|
2017-08-09 02:30:56 -05:00
|
|
|
/// **What it does:** Warns if there is missing doc for any documentable item
|
|
|
|
/// (public or private).
|
2016-08-23 11:09:37 -05:00
|
|
|
///
|
2017-08-09 02:30:56 -05:00
|
|
|
/// **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.
|
2016-08-23 11:09:37 -05:00
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
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 {
|
|
|
|
/// Stack of whether #[doc(hidden)] is set
|
|
|
|
/// at each level which has lint attributes.
|
|
|
|
doc_hidden_stack: Vec<bool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::default::Default for MissingDoc {
|
2017-08-21 06:32:12 -05:00
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
2016-08-23 11:09:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MissingDoc {
|
2017-08-21 06:32:12 -05:00
|
|
|
pub fn new() -> Self {
|
2017-09-05 04:33:04 -05:00
|
|
|
Self {
|
|
|
|
doc_hidden_stack: vec![false],
|
|
|
|
}
|
2016-08-23 11:09:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn doc_hidden(&self) -> bool {
|
2017-09-05 04:33:04 -05:00
|
|
|
*self.doc_hidden_stack
|
|
|
|
.last()
|
|
|
|
.expect("empty doc_hidden_stack")
|
2016-08-23 11:09:37 -05:00
|
|
|
}
|
|
|
|
|
2018-07-23 06:01:12 -05:00
|
|
|
fn check_missing_docs_attrs(&self, cx: &LateContext<'_, '_>, attrs: &[ast::Attribute], sp: Span, 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;
|
|
|
|
}
|
|
|
|
|
2017-03-31 17:14:04 -05:00
|
|
|
if in_macro(sp) {
|
2016-08-23 12:00:56 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-05 04:33:04 -05:00
|
|
|
let has_doc = attrs
|
|
|
|
.iter()
|
2018-05-03 17:28:02 -05:00
|
|
|
.any(|a| a.is_value_str() && a.name() == "doc");
|
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,
|
|
|
|
&format!("missing documentation for {}", desc),
|
|
|
|
);
|
2016-08-23 11:09:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for MissingDoc {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array![MISSING_DOCS_IN_PRIVATE_ITEMS]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 06:13:40 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
|
|
|
|
fn enter_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, attrs: &'tcx [ast::Attribute]) {
|
2017-09-05 04:33:04 -05:00
|
|
|
let doc_hidden = self.doc_hidden() || attrs.iter().any(|attr| {
|
|
|
|
attr.check_name("doc") && match attr.meta_item_list() {
|
|
|
|
None => false,
|
|
|
|
Some(l) => attr::list_contains_name(&l[..], "hidden"),
|
|
|
|
}
|
|
|
|
});
|
2016-08-23 11:09:37 -05:00
|
|
|
self.doc_hidden_stack.push(doc_hidden);
|
|
|
|
}
|
|
|
|
|
2016-12-07 06:13:40 -06:00
|
|
|
fn exit_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx [ast::Attribute]) {
|
2016-08-23 11:09:37 -05:00
|
|
|
self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
|
|
|
|
}
|
|
|
|
|
2016-12-07 06:13:40 -06:00
|
|
|
fn check_crate(&mut self, cx: &LateContext<'a, 'tcx>, krate: &'tcx hir::Crate) {
|
2016-08-23 11:09:37 -05:00
|
|
|
self.check_missing_docs_attrs(cx, &krate.attrs, krate.span, "crate");
|
|
|
|
}
|
|
|
|
|
2016-12-07 06:13:40 -06:00
|
|
|
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
|
2016-08-23 11:09:37 -05:00
|
|
|
let desc = match it.node {
|
2018-07-16 08:07:39 -05:00
|
|
|
hir::ItemKind::Const(..) => "a constant",
|
|
|
|
hir::ItemKind::Enum(..) => "an enum",
|
|
|
|
hir::ItemKind::Fn(..) => {
|
2018-01-14 00:05:07 -06:00
|
|
|
// ignore main()
|
|
|
|
if it.name == "main" {
|
|
|
|
let def_id = cx.tcx.hir.local_def_id(it.id);
|
|
|
|
let def_key = cx.tcx.hir.def_key(def_id);
|
|
|
|
if def_key.parent == Some(hir::def_id::CRATE_DEF_INDEX) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"a function"
|
|
|
|
},
|
2018-07-16 08:07:39 -05: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",
|
|
|
|
hir::ItemKind::GlobalAsm(..) => "an assembly blob",
|
|
|
|
hir::ItemKind::Ty(..) => "a type alias",
|
|
|
|
hir::ItemKind::Union(..) => "a union",
|
|
|
|
hir::ItemKind::Existential(..) => "an existential type",
|
|
|
|
hir::ItemKind::ExternCrate(..) |
|
|
|
|
hir::ItemKind::ForeignMod(..) |
|
|
|
|
hir::ItemKind::Impl(..) |
|
|
|
|
hir::ItemKind::Use(..) => return,
|
2016-08-23 11:09:37 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
|
|
|
|
}
|
|
|
|
|
2016-12-07 06:13:40 -06:00
|
|
|
fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx hir::TraitItem) {
|
2016-08-23 11:09:37 -05:00
|
|
|
let desc = match trait_item.node {
|
2017-01-03 11:19:17 -06:00
|
|
|
hir::TraitItemKind::Const(..) => "an associated constant",
|
|
|
|
hir::TraitItemKind::Method(..) => "a trait method",
|
|
|
|
hir::TraitItemKind::Type(..) => "an associated type",
|
2016-08-23 11:09:37 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
|
|
|
|
}
|
|
|
|
|
2016-12-07 06:13:40 -06:00
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'a, '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.
|
2017-02-02 10:53:28 -06:00
|
|
|
let def_id = cx.tcx.hir.local_def_id(impl_item.id);
|
2016-11-16 14:57:56 -06:00
|
|
|
match cx.tcx.associated_item(def_id).container {
|
2016-08-23 11:09:37 -05:00
|
|
|
ty::TraitContainer(_) => return,
|
2017-09-05 04:33:04 -05:00
|
|
|
ty::ImplContainer(cid) => if cx.tcx.impl_trait_ref(cid).is_some() {
|
|
|
|
return;
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2016-08-23 11:09:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let desc = match impl_item.node {
|
|
|
|
hir::ImplItemKind::Const(..) => "an associated constant",
|
|
|
|
hir::ImplItemKind::Method(..) => "a method",
|
|
|
|
hir::ImplItemKind::Type(_) => "an associated type",
|
2018-07-19 21:59:07 -05:00
|
|
|
hir::ImplItemKind::Existential(_) => "an existential type",
|
2016-08-23 11:09:37 -05:00
|
|
|
};
|
|
|
|
self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
|
|
|
|
}
|
|
|
|
|
2016-12-07 06:13:40 -06:00
|
|
|
fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, sf: &'tcx hir::StructField) {
|
2016-08-23 11:09:37 -05:00
|
|
|
if !sf.is_positional() {
|
|
|
|
self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a struct field");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 06:13:40 -06:00
|
|
|
fn check_variant(&mut self, cx: &LateContext<'a, 'tcx>, v: &'tcx hir::Variant, _: &hir::Generics) {
|
2016-08-23 11:09:37 -05:00
|
|
|
self.check_missing_docs_attrs(cx, &v.node.attrs, v.span, "a variant");
|
|
|
|
}
|
|
|
|
}
|