Auto merge of #6318 - camsteffen:article-description, r=Manishearth

Use article_and_description for missing docs

Moves closer to the current rustc missing_doc lint

changelog: none
This commit is contained in:
bors 2020-12-12 21:20:40 +00:00
commit 89c282fb63
3 changed files with 40 additions and 38 deletions

View File

@ -2,7 +2,7 @@
// *rustc*'s // *rustc*'s
// [`missing_doc`]. // [`missing_doc`].
// //
// [`missing_doc`]: https://github.com/rust-lang/rust/blob/d6d05904697d89099b55da3331155392f1db9c00/src/librustc_lint/builtin.rs#L246 // [`missing_doc`]: https://github.com/rust-lang/rust/blob/cf9cf7c923eb01146971429044f216a3ca905e06/compiler/rustc_lint/src/builtin.rs#L415
// //
use crate::utils::span_lint; use crate::utils::span_lint;
@ -70,7 +70,14 @@ impl MissingDoc {
} }
} }
fn check_missing_docs_attrs(&self, cx: &LateContext<'_>, attrs: &[ast::Attribute], sp: Span, desc: &'static str) { 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 // If we're building a test harness, then warning about
// documentation is probably not really relevant right now. // documentation is probably not really relevant right now.
if cx.sess().opts.test { if cx.sess().opts.test {
@ -94,7 +101,7 @@ impl MissingDoc {
cx, cx,
MISSING_DOCS_IN_PRIVATE_ITEMS, MISSING_DOCS_IN_PRIVATE_ITEMS,
sp, sp,
&format!("missing documentation for {}", desc), &format!("missing documentation for {} {}", article, desc),
); );
} }
} }
@ -120,13 +127,11 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
} }
fn check_crate(&mut self, cx: &LateContext<'tcx>, krate: &'tcx hir::Crate<'_>) { fn check_crate(&mut self, cx: &LateContext<'tcx>, krate: &'tcx hir::Crate<'_>) {
self.check_missing_docs_attrs(cx, &krate.item.attrs, krate.item.span, "crate"); self.check_missing_docs_attrs(cx, &krate.item.attrs, krate.item.span, "the", "crate");
} }
fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) { fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
let desc = match it.kind { match it.kind {
hir::ItemKind::Const(..) => "a constant",
hir::ItemKind::Enum(..) => "an enum",
hir::ItemKind::Fn(..) => { hir::ItemKind::Fn(..) => {
// ignore main() // ignore main()
if it.ident.name == sym::main { if it.ident.name == sym::main {
@ -136,16 +141,17 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
return; return;
} }
} }
"a function"
}, },
hir::ItemKind::Mod(..) => "a module", hir::ItemKind::Const(..)
hir::ItemKind::Static(..) => "a static", | hir::ItemKind::Enum(..)
hir::ItemKind::Struct(..) => "a struct", | hir::ItemKind::Mod(..)
hir::ItemKind::Trait(..) => "a trait", | hir::ItemKind::Static(..)
hir::ItemKind::TraitAlias(..) => "a trait alias", | hir::ItemKind::Struct(..)
hir::ItemKind::TyAlias(..) => "a type alias", | hir::ItemKind::Trait(..)
hir::ItemKind::Union(..) => "a union", | hir::ItemKind::TraitAlias(..)
hir::ItemKind::OpaqueTy(..) => "an existential type", | hir::ItemKind::TyAlias(..)
| hir::ItemKind::Union(..)
| hir::ItemKind::OpaqueTy(..) => {},
hir::ItemKind::ExternCrate(..) hir::ItemKind::ExternCrate(..)
| hir::ItemKind::ForeignMod { .. } | hir::ItemKind::ForeignMod { .. }
| hir::ItemKind::GlobalAsm(..) | hir::ItemKind::GlobalAsm(..)
@ -153,17 +159,17 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
| hir::ItemKind::Use(..) => return, | hir::ItemKind::Use(..) => return,
}; };
self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc); let def_id = cx.tcx.hir().local_def_id(it.hir_id);
let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id());
self.check_missing_docs_attrs(cx, &it.attrs, it.span, article, desc);
} }
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, trait_item: &'tcx hir::TraitItem<'_>) { fn check_trait_item(&mut self, cx: &LateContext<'tcx>, trait_item: &'tcx hir::TraitItem<'_>) {
let desc = match trait_item.kind { let def_id = cx.tcx.hir().local_def_id(trait_item.hir_id);
hir::TraitItemKind::Const(..) => "an associated constant", let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id());
hir::TraitItemKind::Fn(..) => "a trait method",
hir::TraitItemKind::Type(..) => "an associated type",
};
self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc); self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, article, desc);
} }
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) { fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
@ -178,21 +184,17 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
}, },
} }
let desc = match impl_item.kind { let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id());
hir::ImplItemKind::Const(..) => "an associated constant", self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, article, desc);
hir::ImplItemKind::Fn(..) => "a method",
hir::ImplItemKind::TyAlias(_) => "an associated type",
};
self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
} }
fn check_struct_field(&mut self, cx: &LateContext<'tcx>, sf: &'tcx hir::StructField<'_>) { fn check_struct_field(&mut self, cx: &LateContext<'tcx>, sf: &'tcx hir::StructField<'_>) {
if !sf.is_positional() { if !sf.is_positional() {
self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a struct field"); self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a", "struct field");
} }
} }
fn check_variant(&mut self, cx: &LateContext<'tcx>, v: &'tcx hir::Variant<'_>) { fn check_variant(&mut self, cx: &LateContext<'tcx>, v: &'tcx hir::Variant<'_>) {
self.check_missing_docs_attrs(cx, &v.attrs, v.span, "a variant"); self.check_missing_docs_attrs(cx, &v.attrs, v.span, "a", "variant");
} }
} }

View File

@ -1,4 +1,4 @@
error: missing documentation for crate error: missing documentation for the crate
--> $DIR/missing-doc-crate-missing.rs:1:1 --> $DIR/missing-doc-crate-missing.rs:1:1
| |
LL | / #![warn(clippy::missing_docs_in_private_items)] LL | / #![warn(clippy::missing_docs_in_private_items)]

View File

@ -51,13 +51,13 @@ LL | | fn foo_with_impl(&self) {}
LL | | } LL | | }
| |_^ | |_^
error: missing documentation for a trait method error: missing documentation for an associated function
--> $DIR/missing-doc-impl.rs:39:5 --> $DIR/missing-doc-impl.rs:39:5
| |
LL | fn foo(&self); LL | fn foo(&self);
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
error: missing documentation for a trait method error: missing documentation for an associated function
--> $DIR/missing-doc-impl.rs:40:5 --> $DIR/missing-doc-impl.rs:40:5
| |
LL | fn foo_with_impl(&self) {} LL | fn foo_with_impl(&self) {}
@ -75,25 +75,25 @@ error: missing documentation for an associated type
LL | type AssociatedTypeDef = Self; LL | type AssociatedTypeDef = Self;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: missing documentation for a method error: missing documentation for an associated function
--> $DIR/missing-doc-impl.rs:62:5 --> $DIR/missing-doc-impl.rs:62:5
| |
LL | pub fn foo() {} LL | pub fn foo() {}
| ^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^
error: missing documentation for a method error: missing documentation for an associated function
--> $DIR/missing-doc-impl.rs:63:5 --> $DIR/missing-doc-impl.rs:63:5
| |
LL | fn bar() {} LL | fn bar() {}
| ^^^^^^^^^^^ | ^^^^^^^^^^^
error: missing documentation for a method error: missing documentation for an associated function
--> $DIR/missing-doc-impl.rs:67:5 --> $DIR/missing-doc-impl.rs:67:5
| |
LL | pub fn foo() {} LL | pub fn foo() {}
| ^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^
error: missing documentation for a method error: missing documentation for an associated function
--> $DIR/missing-doc-impl.rs:70:5 --> $DIR/missing-doc-impl.rs:70:5
| |
LL | fn foo2() {} LL | fn foo2() {}