Skip to content

Commit

Permalink
Scope missing_docs_in_private_items to only private items
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbanchich committed Feb 12, 2023
1 parent 0f75581 commit a1c2039
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 130 deletions.
22 changes: 16 additions & 6 deletions clippy_lints/src/missing_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use clippy_utils::is_from_proc_macro;
use if_chain::if_chain;
use rustc_ast::ast::{self, MetaItem, MetaItemKind};
use rustc_hir as hir;
use rustc_hir::def_id::LocalDefId;
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::ty::DefIdTree;
use rustc_session::{declare_tool_lint, impl_lint_pass};
Expand Down Expand Up @@ -76,6 +77,7 @@ impl MissingDoc {
fn check_missing_docs_attrs(
&self,
cx: &LateContext<'_>,
def_id: LocalDefId,
attrs: &[ast::Attribute],
sp: Span,
article: &'static str,
Expand All @@ -96,9 +98,17 @@ impl MissingDoc {
return;
}

// Only check publicly-visible items, using the result from the privacy pass.
// It's an option so the crate root can also use this function (it doesn't
// have a `NodeId`).
if def_id != CRATE_DEF_ID && cx.effective_visibilities.is_exported(def_id) {
return;
}

let has_doc = attrs
.iter()
.any(|a| a.doc_str().is_some() || Self::has_include(a.meta()));

if !has_doc {
span_lint(
cx,
Expand All @@ -124,7 +134,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {

fn check_crate(&mut self, cx: &LateContext<'tcx>) {
let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID);
self.check_missing_docs_attrs(cx, attrs, cx.tcx.def_span(CRATE_DEF_ID), "the", "crate");
self.check_missing_docs_attrs(cx, CRATE_DEF_ID, attrs, cx.tcx.def_span(CRATE_DEF_ID), "the", "crate");
}

fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
Expand Down Expand Up @@ -160,7 +170,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {

let attrs = cx.tcx.hir().attrs(it.hir_id());
if !is_from_proc_macro(cx, it) {
self.check_missing_docs_attrs(cx, attrs, it.span, article, desc);
self.check_missing_docs_attrs(cx, it.owner_id.def_id, attrs, it.span, article, desc);
}
}

Expand All @@ -169,7 +179,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {

let attrs = cx.tcx.hir().attrs(trait_item.hir_id());
if !is_from_proc_macro(cx, trait_item) {
self.check_missing_docs_attrs(cx, attrs, trait_item.span, article, desc);
self.check_missing_docs_attrs(cx, trait_item.owner_id.def_id, attrs, trait_item.span, article, desc);
}
}

Expand All @@ -186,23 +196,23 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
let (article, desc) = cx.tcx.article_and_description(impl_item.owner_id.to_def_id());
let attrs = cx.tcx.hir().attrs(impl_item.hir_id());
if !is_from_proc_macro(cx, impl_item) {
self.check_missing_docs_attrs(cx, attrs, impl_item.span, article, desc);
self.check_missing_docs_attrs(cx, impl_item.owner_id.def_id, attrs, impl_item.span, article, desc);
}
}

fn check_field_def(&mut self, cx: &LateContext<'tcx>, sf: &'tcx hir::FieldDef<'_>) {
if !sf.is_positional() {
let attrs = cx.tcx.hir().attrs(sf.hir_id);
if !is_from_proc_macro(cx, sf) {
self.check_missing_docs_attrs(cx, attrs, sf.span, "a", "struct field");
self.check_missing_docs_attrs(cx, sf.def_id, attrs, sf.span, "a", "struct field");
}
}
}

fn check_variant(&mut self, cx: &LateContext<'tcx>, v: &'tcx hir::Variant<'_>) {
let attrs = cx.tcx.hir().attrs(v.hir_id);
if !is_from_proc_macro(cx, v) {
self.check_missing_docs_attrs(cx, attrs, v.span, "a", "variant");
self.check_missing_docs_attrs(cx, v.def_id, attrs, v.span, "a", "variant");
}
}
}
70 changes: 1 addition & 69 deletions tests/ui/missing_doc.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,12 @@ LL | type Typedef = String;
|
= note: `-D clippy::missing-docs-in-private-items` implied by `-D warnings`

error: missing documentation for a type alias
--> $DIR/missing_doc.rs:17:1
|
LL | pub type PubTypedef = String;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: missing documentation for a module
--> $DIR/missing_doc.rs:19:1
|
LL | mod module_no_dox {}
| ^^^^^^^^^^^^^^^^^^^^

error: missing documentation for a module
--> $DIR/missing_doc.rs:20:1
|
LL | pub mod pub_module_no_dox {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: missing documentation for a function
--> $DIR/missing_doc.rs:24:1
|
LL | pub fn foo2() {}
| ^^^^^^^^^^^^^^^^

error: missing documentation for a function
--> $DIR/missing_doc.rs:25:1
|
Expand Down Expand Up @@ -69,50 +51,18 @@ error: missing documentation for a variant
LL | BarB,
| ^^^^

error: missing documentation for an enum
--> $DIR/missing_doc.rs:44:1
|
LL | / pub enum PubBaz {
LL | | PubBazA { a: isize },
LL | | }
| |_^

error: missing documentation for a variant
--> $DIR/missing_doc.rs:45:5
|
LL | PubBazA { a: isize },
| ^^^^^^^^^^^^^^^^^^^^

error: missing documentation for a struct field
--> $DIR/missing_doc.rs:45:15
|
LL | PubBazA { a: isize },
| ^^^^^^^^

error: missing documentation for a constant
--> $DIR/missing_doc.rs:65:1
|
LL | const FOO: u32 = 0;
| ^^^^^^^^^^^^^^^^^^^

error: missing documentation for a constant
--> $DIR/missing_doc.rs:72:1
|
LL | pub const FOO4: u32 = 0;
| ^^^^^^^^^^^^^^^^^^^^^^^^

error: missing documentation for a static
--> $DIR/missing_doc.rs:74:1
|
LL | static BAR: u32 = 0;
| ^^^^^^^^^^^^^^^^^^^^

error: missing documentation for a static
--> $DIR/missing_doc.rs:81:1
|
LL | pub static BAR4: u32 = 0;
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: missing documentation for a module
--> $DIR/missing_doc.rs:83:1
|
Expand All @@ -125,35 +75,17 @@ LL | | }
LL | | }
| |_^

error: missing documentation for a function
--> $DIR/missing_doc.rs:86:5
|
LL | pub fn undocumented1() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: missing documentation for a function
--> $DIR/missing_doc.rs:87:5
|
LL | pub fn undocumented2() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: missing documentation for a function
--> $DIR/missing_doc.rs:88:5
|
LL | fn undocumented3() {}
| ^^^^^^^^^^^^^^^^^^^^^

error: missing documentation for a function
--> $DIR/missing_doc.rs:93:9
|
LL | pub fn also_undocumented1() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: missing documentation for a function
--> $DIR/missing_doc.rs:94:9
|
LL | fn also_undocumented2() {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 24 previous errors
error: aborting due to 13 previous errors

56 changes: 1 addition & 55 deletions tests/ui/missing_doc_impl.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,60 +21,12 @@ error: missing documentation for a struct field
LL | b: isize,
| ^^^^^^^^

error: missing documentation for a struct
--> $DIR/missing_doc_impl.rs:18:1
|
LL | / pub struct PubFoo {
LL | | pub a: isize,
LL | | b: isize,
LL | | }
| |_^

error: missing documentation for a struct field
--> $DIR/missing_doc_impl.rs:19:5
|
LL | pub a: isize,
| ^^^^^^^^^^^^

error: missing documentation for a struct field
--> $DIR/missing_doc_impl.rs:20:5
|
LL | b: isize,
| ^^^^^^^^

error: missing documentation for a trait
--> $DIR/missing_doc_impl.rs:43:1
|
LL | / pub trait C {
LL | | fn foo(&self);
LL | | fn foo_with_impl(&self) {}
LL | | }
| |_^

error: missing documentation for an associated function
--> $DIR/missing_doc_impl.rs:44:5
|
LL | fn foo(&self);
| ^^^^^^^^^^^^^^

error: missing documentation for an associated function
--> $DIR/missing_doc_impl.rs:45:5
|
LL | fn foo_with_impl(&self) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: missing documentation for an associated type
--> $DIR/missing_doc_impl.rs:55:5
|
LL | type AssociatedType;
| ^^^^^^^^^^^^^^^^^^^^

error: missing documentation for an associated type
--> $DIR/missing_doc_impl.rs:56:5
|
LL | type AssociatedTypeDef = Self;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: missing documentation for an associated function
--> $DIR/missing_doc_impl.rs:67:5
|
Expand All @@ -89,12 +41,6 @@ error: missing documentation for an associated function
LL | fn bar() {}
| ^^^^^^^^^^^

error: missing documentation for an associated function
--> $DIR/missing_doc_impl.rs:74:5
|
LL | pub fn foo() {}
| ^^^^^^^^^^^^^^^

error: missing documentation for an associated function
--> $DIR/missing_doc_impl.rs:78:5
|
Expand All @@ -103,5 +49,5 @@ LL | | 1
LL | | }
| |_____^

error: aborting due to 15 previous errors
error: aborting due to 7 previous errors

0 comments on commit a1c2039

Please sign in to comment.