Skip to content

Commit

Permalink
Auto merge of rust-lang#6318 - camsteffen:article-description, r=Mani…
Browse files Browse the repository at this point in the history
…shearth

Use article_and_description for missing docs

Moves closer to the current rustc missing_doc lint

changelog: none
  • Loading branch information
bors committed Dec 12, 2020
2 parents 3b89a67 + a8c4744 commit 89c282f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 38 deletions.
64 changes: 33 additions & 31 deletions clippy_lints/src/missing_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// *rustc*'s
// [`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;
Expand Down Expand Up @@ -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
// documentation is probably not really relevant right now.
if cx.sess().opts.test {
Expand All @@ -94,7 +101,7 @@ impl MissingDoc {
cx,
MISSING_DOCS_IN_PRIVATE_ITEMS,
sp,
&format!("missing documentation for {}", desc),
&format!("missing documentation for {} {}", article, desc),
);
}
}
Expand All @@ -120,13 +127,11 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
}

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<'_>) {
let desc = match it.kind {
hir::ItemKind::Const(..) => "a constant",
hir::ItemKind::Enum(..) => "an enum",
match it.kind {
hir::ItemKind::Fn(..) => {
// ignore main()
if it.ident.name == sym::main {
Expand All @@ -136,34 +141,35 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
return;
}
}
"a function"
},
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::TyAlias(..) => "a type alias",
hir::ItemKind::Union(..) => "a union",
hir::ItemKind::OpaqueTy(..) => "an existential type",
hir::ItemKind::Const(..)
| hir::ItemKind::Enum(..)
| hir::ItemKind::Mod(..)
| hir::ItemKind::Static(..)
| hir::ItemKind::Struct(..)
| hir::ItemKind::Trait(..)
| hir::ItemKind::TraitAlias(..)
| hir::ItemKind::TyAlias(..)
| hir::ItemKind::Union(..)
| hir::ItemKind::OpaqueTy(..) => {},
hir::ItemKind::ExternCrate(..)
| hir::ItemKind::ForeignMod { .. }
| hir::ItemKind::GlobalAsm(..)
| hir::ItemKind::Impl { .. }
| 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<'_>) {
let desc = match trait_item.kind {
hir::TraitItemKind::Const(..) => "an associated constant",
hir::TraitItemKind::Fn(..) => "a trait method",
hir::TraitItemKind::Type(..) => "an associated type",
};
let def_id = cx.tcx.hir().local_def_id(trait_item.hir_id);
let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id());

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<'_>) {
Expand All @@ -178,21 +184,17 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
},
}

let desc = match impl_item.kind {
hir::ImplItemKind::Const(..) => "an associated constant",
hir::ImplItemKind::Fn(..) => "a method",
hir::ImplItemKind::TyAlias(_) => "an associated type",
};
self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id());
self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, article, desc);
}

fn check_struct_field(&mut self, cx: &LateContext<'tcx>, sf: &'tcx hir::StructField<'_>) {
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<'_>) {
self.check_missing_docs_attrs(cx, &v.attrs, v.span, "a variant");
self.check_missing_docs_attrs(cx, &v.attrs, v.span, "a", "variant");
}
}
2 changes: 1 addition & 1 deletion tests/ui/missing-doc-crate-missing.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: missing documentation for crate
error: missing documentation for the crate
--> $DIR/missing-doc-crate-missing.rs:1:1
|
LL | / #![warn(clippy::missing_docs_in_private_items)]
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/missing-doc-impl.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ LL | | fn foo_with_impl(&self) {}
LL | | }
| |_^

error: missing documentation for a trait method
error: missing documentation for an associated function
--> $DIR/missing-doc-impl.rs:39:5
|
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
|
LL | fn foo_with_impl(&self) {}
Expand All @@ -75,25 +75,25 @@ error: missing documentation for an associated type
LL | type AssociatedTypeDef = Self;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: missing documentation for a method
error: missing documentation for an associated function
--> $DIR/missing-doc-impl.rs:62:5
|
LL | pub fn foo() {}
| ^^^^^^^^^^^^^^^

error: missing documentation for a method
error: missing documentation for an associated function
--> $DIR/missing-doc-impl.rs:63:5
|
LL | fn bar() {}
| ^^^^^^^^^^^

error: missing documentation for a method
error: missing documentation for an associated function
--> $DIR/missing-doc-impl.rs:67:5
|
LL | pub fn foo() {}
| ^^^^^^^^^^^^^^^

error: missing documentation for a method
error: missing documentation for an associated function
--> $DIR/missing-doc-impl.rs:70:5
|
LL | fn foo2() {}
Expand Down

0 comments on commit 89c282f

Please sign in to comment.