Skip to content

Commit a1c2039

Browse files
Scope missing_docs_in_private_items to only private items
1 parent 0f75581 commit a1c2039

File tree

3 files changed

+18
-130
lines changed

3 files changed

+18
-130
lines changed

clippy_lints/src/missing_doc.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use clippy_utils::is_from_proc_macro;
1111
use if_chain::if_chain;
1212
use rustc_ast::ast::{self, MetaItem, MetaItemKind};
1313
use rustc_hir as hir;
14+
use rustc_hir::def_id::LocalDefId;
1415
use rustc_lint::{LateContext, LateLintPass, LintContext};
1516
use rustc_middle::ty::DefIdTree;
1617
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -76,6 +77,7 @@ impl MissingDoc {
7677
fn check_missing_docs_attrs(
7778
&self,
7879
cx: &LateContext<'_>,
80+
def_id: LocalDefId,
7981
attrs: &[ast::Attribute],
8082
sp: Span,
8183
article: &'static str,
@@ -96,9 +98,17 @@ impl MissingDoc {
9698
return;
9799
}
98100

101+
// Only check publicly-visible items, using the result from the privacy pass.
102+
// It's an option so the crate root can also use this function (it doesn't
103+
// have a `NodeId`).
104+
if def_id != CRATE_DEF_ID && cx.effective_visibilities.is_exported(def_id) {
105+
return;
106+
}
107+
99108
let has_doc = attrs
100109
.iter()
101110
.any(|a| a.doc_str().is_some() || Self::has_include(a.meta()));
111+
102112
if !has_doc {
103113
span_lint(
104114
cx,
@@ -124,7 +134,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
124134

125135
fn check_crate(&mut self, cx: &LateContext<'tcx>) {
126136
let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID);
127-
self.check_missing_docs_attrs(cx, attrs, cx.tcx.def_span(CRATE_DEF_ID), "the", "crate");
137+
self.check_missing_docs_attrs(cx, CRATE_DEF_ID, attrs, cx.tcx.def_span(CRATE_DEF_ID), "the", "crate");
128138
}
129139

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

161171
let attrs = cx.tcx.hir().attrs(it.hir_id());
162172
if !is_from_proc_macro(cx, it) {
163-
self.check_missing_docs_attrs(cx, attrs, it.span, article, desc);
173+
self.check_missing_docs_attrs(cx, it.owner_id.def_id, attrs, it.span, article, desc);
164174
}
165175
}
166176

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

170180
let attrs = cx.tcx.hir().attrs(trait_item.hir_id());
171181
if !is_from_proc_macro(cx, trait_item) {
172-
self.check_missing_docs_attrs(cx, attrs, trait_item.span, article, desc);
182+
self.check_missing_docs_attrs(cx, trait_item.owner_id.def_id, attrs, trait_item.span, article, desc);
173183
}
174184
}
175185

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

193203
fn check_field_def(&mut self, cx: &LateContext<'tcx>, sf: &'tcx hir::FieldDef<'_>) {
194204
if !sf.is_positional() {
195205
let attrs = cx.tcx.hir().attrs(sf.hir_id);
196206
if !is_from_proc_macro(cx, sf) {
197-
self.check_missing_docs_attrs(cx, attrs, sf.span, "a", "struct field");
207+
self.check_missing_docs_attrs(cx, sf.def_id, attrs, sf.span, "a", "struct field");
198208
}
199209
}
200210
}
201211

202212
fn check_variant(&mut self, cx: &LateContext<'tcx>, v: &'tcx hir::Variant<'_>) {
203213
let attrs = cx.tcx.hir().attrs(v.hir_id);
204214
if !is_from_proc_macro(cx, v) {
205-
self.check_missing_docs_attrs(cx, attrs, v.span, "a", "variant");
215+
self.check_missing_docs_attrs(cx, v.def_id, attrs, v.span, "a", "variant");
206216
}
207217
}
208218
}

tests/ui/missing_doc.stderr

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,12 @@ LL | type Typedef = String;
66
|
77
= note: `-D clippy::missing-docs-in-private-items` implied by `-D warnings`
88

9-
error: missing documentation for a type alias
10-
--> $DIR/missing_doc.rs:17:1
11-
|
12-
LL | pub type PubTypedef = String;
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14-
159
error: missing documentation for a module
1610
--> $DIR/missing_doc.rs:19:1
1711
|
1812
LL | mod module_no_dox {}
1913
| ^^^^^^^^^^^^^^^^^^^^
2014

21-
error: missing documentation for a module
22-
--> $DIR/missing_doc.rs:20:1
23-
|
24-
LL | pub mod pub_module_no_dox {}
25-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
26-
27-
error: missing documentation for a function
28-
--> $DIR/missing_doc.rs:24:1
29-
|
30-
LL | pub fn foo2() {}
31-
| ^^^^^^^^^^^^^^^^
32-
3315
error: missing documentation for a function
3416
--> $DIR/missing_doc.rs:25:1
3517
|
@@ -69,50 +51,18 @@ error: missing documentation for a variant
6951
LL | BarB,
7052
| ^^^^
7153

72-
error: missing documentation for an enum
73-
--> $DIR/missing_doc.rs:44:1
74-
|
75-
LL | / pub enum PubBaz {
76-
LL | | PubBazA { a: isize },
77-
LL | | }
78-
| |_^
79-
80-
error: missing documentation for a variant
81-
--> $DIR/missing_doc.rs:45:5
82-
|
83-
LL | PubBazA { a: isize },
84-
| ^^^^^^^^^^^^^^^^^^^^
85-
86-
error: missing documentation for a struct field
87-
--> $DIR/missing_doc.rs:45:15
88-
|
89-
LL | PubBazA { a: isize },
90-
| ^^^^^^^^
91-
9254
error: missing documentation for a constant
9355
--> $DIR/missing_doc.rs:65:1
9456
|
9557
LL | const FOO: u32 = 0;
9658
| ^^^^^^^^^^^^^^^^^^^
9759

98-
error: missing documentation for a constant
99-
--> $DIR/missing_doc.rs:72:1
100-
|
101-
LL | pub const FOO4: u32 = 0;
102-
| ^^^^^^^^^^^^^^^^^^^^^^^^
103-
10460
error: missing documentation for a static
10561
--> $DIR/missing_doc.rs:74:1
10662
|
10763
LL | static BAR: u32 = 0;
10864
| ^^^^^^^^^^^^^^^^^^^^
10965

110-
error: missing documentation for a static
111-
--> $DIR/missing_doc.rs:81:1
112-
|
113-
LL | pub static BAR4: u32 = 0;
114-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
115-
11666
error: missing documentation for a module
11767
--> $DIR/missing_doc.rs:83:1
11868
|
@@ -125,35 +75,17 @@ LL | | }
12575
LL | | }
12676
| |_^
12777

128-
error: missing documentation for a function
129-
--> $DIR/missing_doc.rs:86:5
130-
|
131-
LL | pub fn undocumented1() {}
132-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
133-
134-
error: missing documentation for a function
135-
--> $DIR/missing_doc.rs:87:5
136-
|
137-
LL | pub fn undocumented2() {}
138-
| ^^^^^^^^^^^^^^^^^^^^^^^^^
139-
14078
error: missing documentation for a function
14179
--> $DIR/missing_doc.rs:88:5
14280
|
14381
LL | fn undocumented3() {}
14482
| ^^^^^^^^^^^^^^^^^^^^^
14583

146-
error: missing documentation for a function
147-
--> $DIR/missing_doc.rs:93:9
148-
|
149-
LL | pub fn also_undocumented1() {}
150-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
151-
15284
error: missing documentation for a function
15385
--> $DIR/missing_doc.rs:94:9
15486
|
15587
LL | fn also_undocumented2() {}
15688
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
15789

158-
error: aborting due to 24 previous errors
90+
error: aborting due to 13 previous errors
15991

tests/ui/missing_doc_impl.stderr

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -21,60 +21,12 @@ error: missing documentation for a struct field
2121
LL | b: isize,
2222
| ^^^^^^^^
2323

24-
error: missing documentation for a struct
25-
--> $DIR/missing_doc_impl.rs:18:1
26-
|
27-
LL | / pub struct PubFoo {
28-
LL | | pub a: isize,
29-
LL | | b: isize,
30-
LL | | }
31-
| |_^
32-
33-
error: missing documentation for a struct field
34-
--> $DIR/missing_doc_impl.rs:19:5
35-
|
36-
LL | pub a: isize,
37-
| ^^^^^^^^^^^^
38-
3924
error: missing documentation for a struct field
4025
--> $DIR/missing_doc_impl.rs:20:5
4126
|
4227
LL | b: isize,
4328
| ^^^^^^^^
4429

45-
error: missing documentation for a trait
46-
--> $DIR/missing_doc_impl.rs:43:1
47-
|
48-
LL | / pub trait C {
49-
LL | | fn foo(&self);
50-
LL | | fn foo_with_impl(&self) {}
51-
LL | | }
52-
| |_^
53-
54-
error: missing documentation for an associated function
55-
--> $DIR/missing_doc_impl.rs:44:5
56-
|
57-
LL | fn foo(&self);
58-
| ^^^^^^^^^^^^^^
59-
60-
error: missing documentation for an associated function
61-
--> $DIR/missing_doc_impl.rs:45:5
62-
|
63-
LL | fn foo_with_impl(&self) {}
64-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
65-
66-
error: missing documentation for an associated type
67-
--> $DIR/missing_doc_impl.rs:55:5
68-
|
69-
LL | type AssociatedType;
70-
| ^^^^^^^^^^^^^^^^^^^^
71-
72-
error: missing documentation for an associated type
73-
--> $DIR/missing_doc_impl.rs:56:5
74-
|
75-
LL | type AssociatedTypeDef = Self;
76-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
77-
7830
error: missing documentation for an associated function
7931
--> $DIR/missing_doc_impl.rs:67:5
8032
|
@@ -89,12 +41,6 @@ error: missing documentation for an associated function
8941
LL | fn bar() {}
9042
| ^^^^^^^^^^^
9143

92-
error: missing documentation for an associated function
93-
--> $DIR/missing_doc_impl.rs:74:5
94-
|
95-
LL | pub fn foo() {}
96-
| ^^^^^^^^^^^^^^^
97-
9844
error: missing documentation for an associated function
9945
--> $DIR/missing_doc_impl.rs:78:5
10046
|
@@ -103,5 +49,5 @@ LL | | 1
10349
LL | | }
10450
| |_____^
10551

106-
error: aborting due to 15 previous errors
52+
error: aborting due to 7 previous errors
10753

0 commit comments

Comments
 (0)