88use clippy_utils:: attrs:: is_doc_hidden;
99use clippy_utils:: diagnostics:: span_lint;
1010use clippy_utils:: is_from_proc_macro;
11+ use hir:: def_id:: LocalDefId ;
1112use if_chain:: if_chain;
1213use rustc_ast:: ast:: { self , MetaItem , MetaItemKind } ;
1314use rustc_hir as hir;
@@ -35,7 +36,7 @@ declare_clippy_lint! {
3536}
3637
3738pub struct MissingDoc {
38- /// FIXME: docs
39+ /// Whether to only check for missing docs in `pub(crate)` items.
3940 crate_items_only : bool ,
4041 /// Stack of whether #[doc(hidden)] is set
4142 /// at each level which has lint attributes.
@@ -79,6 +80,7 @@ impl MissingDoc {
7980 fn check_missing_docs_attrs (
8081 & self ,
8182 cx : & LateContext < ' _ > ,
83+ def_id : LocalDefId ,
8284 attrs : & [ ast:: Attribute ] ,
8385 sp : Span ,
8486 article : & ' static str ,
@@ -99,6 +101,13 @@ impl MissingDoc {
99101 return ;
100102 }
101103
104+ if self . crate_items_only && def_id != CRATE_DEF_ID {
105+ let vis = cx. tcx . visibility ( def_id) ;
106+ if vis != Visibility :: Public && vis != Visibility :: Restricted ( CRATE_DEF_ID . into ( ) ) {
107+ return ;
108+ }
109+ }
110+
102111 let has_doc = attrs
103112 . iter ( )
104113 . any ( |a| a. doc_str ( ) . is_some ( ) || Self :: has_include ( a. meta ( ) ) ) ;
@@ -127,17 +136,10 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
127136
128137 fn check_crate ( & mut self , cx : & LateContext < ' tcx > ) {
129138 let attrs = cx. tcx . hir ( ) . attrs ( hir:: CRATE_HIR_ID ) ;
130- self . check_missing_docs_attrs ( cx, attrs, cx. tcx . def_span ( CRATE_DEF_ID ) , "the" , "crate" ) ;
139+ self . check_missing_docs_attrs ( cx, CRATE_DEF_ID , attrs, cx. tcx . def_span ( CRATE_DEF_ID ) , "the" , "crate" ) ;
131140 }
132141
133142 fn check_item ( & mut self , cx : & LateContext < ' tcx > , it : & ' tcx hir:: Item < ' _ > ) {
134- if self . crate_items_only {
135- let vis = cx. tcx . visibility ( it. owner_id . to_def_id ( ) ) ;
136- if vis != Visibility :: Public && vis != Visibility :: Restricted ( CRATE_DEF_ID . into ( ) ) {
137- return ;
138- }
139- }
140-
141143 match it. kind {
142144 hir:: ItemKind :: Fn ( ..) => {
143145 // ignore main()
@@ -170,7 +172,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
170172
171173 let attrs = cx. tcx . hir ( ) . attrs ( it. hir_id ( ) ) ;
172174 if !is_from_proc_macro ( cx, it) {
173- self . check_missing_docs_attrs ( cx, attrs, it. span , article, desc) ;
175+ self . check_missing_docs_attrs ( cx, it . owner_id . def_id , attrs, it. span , article, desc) ;
174176 }
175177 }
176178
@@ -179,7 +181,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
179181
180182 let attrs = cx. tcx . hir ( ) . attrs ( trait_item. hir_id ( ) ) ;
181183 if !is_from_proc_macro ( cx, trait_item) {
182- self . check_missing_docs_attrs ( cx, attrs, trait_item. span , article, desc) ;
184+ self . check_missing_docs_attrs ( cx, trait_item . owner_id . def_id , attrs, trait_item. span , article, desc) ;
183185 }
184186 }
185187
@@ -196,23 +198,23 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
196198 let ( article, desc) = cx. tcx . article_and_description ( impl_item. owner_id . to_def_id ( ) ) ;
197199 let attrs = cx. tcx . hir ( ) . attrs ( impl_item. hir_id ( ) ) ;
198200 if !is_from_proc_macro ( cx, impl_item) {
199- self . check_missing_docs_attrs ( cx, attrs, impl_item. span , article, desc) ;
201+ self . check_missing_docs_attrs ( cx, impl_item . owner_id . def_id , attrs, impl_item. span , article, desc) ;
200202 }
201203 }
202204
203205 fn check_field_def ( & mut self , cx : & LateContext < ' tcx > , sf : & ' tcx hir:: FieldDef < ' _ > ) {
204206 if !sf. is_positional ( ) {
205207 let attrs = cx. tcx . hir ( ) . attrs ( sf. hir_id ) ;
206208 if !is_from_proc_macro ( cx, sf) {
207- self . check_missing_docs_attrs ( cx, attrs, sf. span , "a" , "struct field" ) ;
209+ self . check_missing_docs_attrs ( cx, sf . def_id , attrs, sf. span , "a" , "struct field" ) ;
208210 }
209211 }
210212 }
211213
212214 fn check_variant ( & mut self , cx : & LateContext < ' tcx > , v : & ' tcx hir:: Variant < ' _ > ) {
213215 let attrs = cx. tcx . hir ( ) . attrs ( v. hir_id ) ;
214216 if !is_from_proc_macro ( cx, v) {
215- self . check_missing_docs_attrs ( cx, attrs, v. span , "a" , "variant" ) ;
217+ self . check_missing_docs_attrs ( cx, v . def_id , attrs, v. span , "a" , "variant" ) ;
216218 }
217219 }
218220}
0 commit comments