11use crate :: clean;
2- use crate :: config:: OutputFormat ;
32use crate :: core:: DocContext ;
43use crate :: fold:: { self , DocFolder } ;
54use crate :: html:: markdown:: { find_testable_code, ErrorCodes } ;
65use crate :: passes:: doc_test_lints:: { should_have_doc_example, Tests } ;
76use crate :: passes:: Pass ;
7+ use rustc_lint:: builtin:: MISSING_DOCS ;
8+ use rustc_middle:: lint:: LintSource ;
9+ use rustc_session:: lint;
810use rustc_span:: symbol:: sym;
911use rustc_span:: FileName ;
1012use serde:: Serialize ;
@@ -19,10 +21,10 @@ pub const CALCULATE_DOC_COVERAGE: Pass = Pass {
1921} ;
2022
2123fn calculate_doc_coverage ( krate : clean:: Crate , ctx : & DocContext < ' _ > ) -> clean:: Crate {
22- let mut calc = CoverageCalculator :: new ( ) ;
24+ let mut calc = CoverageCalculator :: new ( ctx ) ;
2325 let krate = calc. fold_crate ( krate) ;
2426
25- calc. print_results ( ctx . renderinfo . borrow ( ) . output_format ) ;
27+ calc. print_results ( ) ;
2628
2729 krate
2830}
@@ -41,8 +43,11 @@ impl ItemCount {
4143 has_docs : bool ,
4244 has_doc_example : bool ,
4345 should_have_doc_examples : bool ,
46+ should_have_docs : bool ,
4447 ) {
45- self . total += 1 ;
48+ if has_docs || should_have_docs {
49+ self . total += 1 ;
50+ }
4651
4752 if has_docs {
4853 self . with_docs += 1 ;
@@ -94,8 +99,9 @@ impl ops::AddAssign for ItemCount {
9499 }
95100}
96101
97- struct CoverageCalculator {
102+ struct CoverageCalculator < ' a , ' b > {
98103 items : BTreeMap < FileName , ItemCount > ,
104+ ctx : & ' a DocContext < ' b > ,
99105}
100106
101107fn limit_filename_len ( filename : String ) -> String {
@@ -108,9 +114,9 @@ fn limit_filename_len(filename: String) -> String {
108114 }
109115}
110116
111- impl CoverageCalculator {
112- fn new ( ) -> CoverageCalculator {
113- CoverageCalculator { items : Default :: default ( ) }
117+ impl < ' a , ' b > CoverageCalculator < ' a , ' b > {
118+ fn new ( ctx : & ' a DocContext < ' b > ) -> CoverageCalculator < ' a , ' b > {
119+ CoverageCalculator { items : Default :: default ( ) , ctx }
114120 }
115121
116122 fn to_json ( & self ) -> String {
@@ -124,7 +130,8 @@ impl CoverageCalculator {
124130 . expect ( "failed to convert JSON data to string" )
125131 }
126132
127- fn print_results ( & self , output_format : Option < OutputFormat > ) {
133+ fn print_results ( & self ) {
134+ let output_format = self . ctx . renderinfo . borrow ( ) . output_format ;
128135 if output_format. map ( |o| o. is_json ( ) ) . unwrap_or_else ( || false ) {
129136 println ! ( "{}" , self . to_json( ) ) ;
130137 return ;
@@ -178,7 +185,7 @@ impl CoverageCalculator {
178185 }
179186}
180187
181- impl fold:: DocFolder for CoverageCalculator {
188+ impl < ' a , ' b > fold:: DocFolder for CoverageCalculator < ' a , ' b > {
182189 fn fold_item ( & mut self , i : clean:: Item ) -> Option < clean:: Item > {
183190 match i. inner {
184191 _ if !i. def_id . is_local ( ) => {
@@ -245,11 +252,18 @@ impl fold::DocFolder for CoverageCalculator {
245252 ) ;
246253
247254 let has_doc_example = tests. found_tests != 0 ;
255+ let hir_id = self . ctx . tcx . hir ( ) . local_def_id_to_hir_id ( i. def_id . expect_local ( ) ) ;
256+ let ( level, source) = self . ctx . tcx . lint_level_at_node ( MISSING_DOCS , hir_id) ;
257+ // `missing_docs` is allow-by-default, so don't treat this as ignoring the item
258+ // unless the user had an explicit `allow`
259+ let should_have_docs =
260+ level != lint:: Level :: Allow || matches ! ( source, LintSource :: Default ) ;
248261 debug ! ( "counting {:?} {:?} in {}" , i. type_( ) , i. name, i. source. filename) ;
249262 self . items . entry ( i. source . filename . clone ( ) ) . or_default ( ) . count_item (
250263 has_docs,
251264 has_doc_example,
252- should_have_doc_example ( & i. inner ) ,
265+ should_have_doc_example ( self . ctx , & i) ,
266+ should_have_docs,
253267 ) ;
254268 }
255269 }
0 commit comments