Skip to content

Commit

Permalink
Rollup merge of rust-lang#69740 - mark-i-m:describe-it-3, r=eddyb
Browse files Browse the repository at this point in the history
Replace some desc logic in librustc_lint with article_and_desc

r? @eddyb @Centril @matthewjasper

Followup to rust-lang#69674

Blocked on rust-lang#69498
  • Loading branch information
Centril authored Mar 23, 2020
2 parents ed3d9e7 + 5e8b795 commit 1a5ad59
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 46 deletions.
70 changes: 37 additions & 33 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ impl MissingDoc {
id: Option<hir::HirId>,
attrs: &[ast::Attribute],
sp: Span,
article: &'static str,
desc: &'static str,
) {
// If we're building a test harness, then warning about
Expand All @@ -374,7 +375,7 @@ impl MissingDoc {
let has_doc = attrs.iter().any(|a| has_doc(a));
if !has_doc {
cx.struct_span_lint(MISSING_DOCS, cx.tcx.sess.source_map().def_span(sp), |lint| {
lint.build(&format!("missing documentation for {}", desc)).emit()
lint.build(&format!("missing documentation for {} {}", article, desc)).emit()
});
}
}
Expand All @@ -398,7 +399,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
}

fn check_crate(&mut self, cx: &LateContext<'_, '_>, krate: &hir::Crate<'_>) {
self.check_missing_docs_attrs(cx, None, &krate.item.attrs, krate.item.span, "crate");
self.check_missing_docs_attrs(cx, None, &krate.item.attrs, krate.item.span, "the", "crate");

for macro_def in krate.exported_macros {
let has_doc = macro_def.attrs.iter().any(|a| has_doc(a));
Expand All @@ -413,12 +414,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
}

fn check_item(&mut self, cx: &LateContext<'_, '_>, it: &hir::Item<'_>) {
let desc = match it.kind {
hir::ItemKind::Fn(..) => "a function",
hir::ItemKind::Mod(..) => "a module",
hir::ItemKind::Enum(..) => "an enum",
hir::ItemKind::Struct(..) => "a struct",
hir::ItemKind::Union(..) => "a union",
match it.kind {
hir::ItemKind::Trait(.., trait_item_refs) => {
// Issue #11592: traits are always considered exported, even when private.
if let hir::VisibilityKind::Inherited = it.vis.node {
Expand All @@ -428,51 +424,55 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
}
return;
}
"a trait"
}
hir::ItemKind::TyAlias(..) => "a type alias",
hir::ItemKind::Impl { of_trait: Some(ref trait_ref), items, .. } => {
// If the trait is private, add the impl items to `private_traits` so they don't get
// reported for missing docs.
let real_trait = trait_ref.path.res.def_id();
if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(real_trait) {
match cx.tcx.hir().find(hir_id) {
Some(Node::Item(item)) => {
if let hir::VisibilityKind::Inherited = item.vis.node {
for impl_item_ref in items {
self.private_traits.insert(impl_item_ref.id.hir_id);
}
if let Some(Node::Item(item)) = cx.tcx.hir().find(hir_id) {
if let hir::VisibilityKind::Inherited = item.vis.node {
for impl_item_ref in items {
self.private_traits.insert(impl_item_ref.id.hir_id);
}
}
_ => {}
}
}
return;
}
hir::ItemKind::Const(..) => "a constant",
hir::ItemKind::Static(..) => "a static",

hir::ItemKind::TyAlias(..)
| hir::ItemKind::Fn(..)
| hir::ItemKind::Mod(..)
| hir::ItemKind::Enum(..)
| hir::ItemKind::Struct(..)
| hir::ItemKind::Union(..)
| hir::ItemKind::Const(..)
| hir::ItemKind::Static(..) => {}

_ => return,
};

self.check_missing_docs_attrs(cx, Some(it.hir_id), &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);

self.check_missing_docs_attrs(cx, Some(it.hir_id), &it.attrs, it.span, article, desc);
}

fn check_trait_item(&mut self, cx: &LateContext<'_, '_>, trait_item: &hir::TraitItem<'_>) {
if self.private_traits.contains(&trait_item.hir_id) {
return;
}

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);

self.check_missing_docs_attrs(
cx,
Some(trait_item.hir_id),
&trait_item.attrs,
trait_item.span,
article,
desc,
);
}
Expand All @@ -483,29 +483,33 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
return;
}

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

fn check_struct_field(&mut self, cx: &LateContext<'_, '_>, sf: &hir::StructField<'_>) {
if !sf.is_positional() {
self.check_missing_docs_attrs(cx, Some(sf.hir_id), &sf.attrs, sf.span, "a struct field")
self.check_missing_docs_attrs(
cx,
Some(sf.hir_id),
&sf.attrs,
sf.span,
"a",
"struct field",
)
}
}

fn check_variant(&mut self, cx: &LateContext<'_, '_>, v: &hir::Variant<'_>) {
self.check_missing_docs_attrs(cx, Some(v.id), &v.attrs, v.span, "a variant");
self.check_missing_docs_attrs(cx, Some(v.id), &v.attrs, v.span, "a", "variant");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/rustdoc-ui/deny-missing-docs-crate.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/deny-missing-docs-crate.rs:1:1
|
LL | / #![deny(missing_docs)]
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-10656.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#![deny(missing_docs)]
#![crate_type="lib"]
//~^^ ERROR missing documentation for crate
//~^^ ERROR missing documentation for the crate
2 changes: 1 addition & 1 deletion src/test/ui/issues/issue-10656.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/issue-10656.rs:1:1
|
LL | / #![deny(missing_docs)]
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/lint/lint-missing-doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ trait B {
}

pub trait C { //~ ERROR: missing documentation for a trait
fn foo(&self); //~ ERROR: missing documentation for a trait method
fn foo_with_impl(&self) {} //~ ERROR: missing documentation for a trait method
fn foo(&self); //~ ERROR: missing documentation for an associated function
fn foo_with_impl(&self) {} //~ ERROR: missing documentation for an associated function
}

#[allow(missing_docs)]
Expand All @@ -78,7 +78,7 @@ impl Foo {
}

impl PubFoo {
pub fn foo() {} //~ ERROR: missing documentation for a method
pub fn foo() {} //~ ERROR: missing documentation for an associated function
/// dox
pub fn foo1() {}
fn foo2() {}
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/lint/lint-missing-doc.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ error: missing documentation for a trait
LL | pub trait C {
| ^^^^^^^^^^^

error: missing documentation for a trait method
error: missing documentation for an associated function
--> $DIR/lint-missing-doc.rs:53:5
|
LL | fn foo(&self);
| ^^^^^^^^^^^^^^

error: missing documentation for a trait method
error: missing documentation for an associated function
--> $DIR/lint-missing-doc.rs:54:5
|
LL | fn foo_with_impl(&self) {}
Expand All @@ -64,7 +64,7 @@ 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/lint-missing-doc.rs:81:5
|
LL | pub fn foo() {}
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/lint/lints-in-foreign-macros.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// aux-build:lints-in-foreign-macros.rs
// check-pass

#![warn(unused_imports)] //~ missing documentation for crate [missing_docs]
#![warn(unused_imports)] //~ missing documentation for the crate [missing_docs]
#![warn(missing_docs)]

#[macro_use]
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/lint/lints-in-foreign-macros.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ warning: unused import: `std::string::ToString`
LL | mod d { baz2!(use std::string::ToString;); }
| ^^^^^^^^^^^^^^^^^^^^^

warning: missing documentation for crate
warning: missing documentation for the crate
--> $DIR/lints-in-foreign-macros.rs:4:1
|
LL | / #![warn(unused_imports)]
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/privacy/private-in-public-non-principal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn leak_dyn_nonprincipal() -> Box<dyn PubPrincipal + PrivNonPrincipal> { loo
#[deny(missing_docs)]
fn container() {
impl dyn PubPrincipal {
pub fn check_doc_lint() {} //~ ERROR missing documentation for a method
pub fn check_doc_lint() {} //~ ERROR missing documentation for an associated function
}
impl dyn PubPrincipal + PrivNonPrincipal {
pub fn check_doc_lint() {} // OK, no missing doc lint
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/privacy/private-in-public-non-principal.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | pub fn leak_dyn_nonprincipal() -> Box<dyn PubPrincipal + PrivNonPrincipal>
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537>

error: missing documentation for a method
error: missing documentation for an associated function
--> $DIR/private-in-public-non-principal.rs:13:9
|
LL | pub fn check_doc_lint() {}
Expand Down

0 comments on commit 1a5ad59

Please sign in to comment.