1
1
use crate :: clean;
2
- use crate :: config:: OutputFormat ;
3
2
use crate :: core:: DocContext ;
4
3
use crate :: fold:: { self , DocFolder } ;
5
4
use crate :: html:: markdown:: { find_testable_code, ErrorCodes } ;
6
5
use crate :: passes:: doc_test_lints:: { should_have_doc_example, Tests } ;
7
6
use crate :: passes:: Pass ;
7
+ use rustc_lint:: builtin:: MISSING_DOCS ;
8
+ use rustc_middle:: lint:: LintSource ;
9
+ use rustc_session:: lint;
8
10
use rustc_span:: symbol:: sym;
9
11
use rustc_span:: FileName ;
10
12
use serde:: Serialize ;
@@ -19,10 +21,10 @@ pub const CALCULATE_DOC_COVERAGE: Pass = Pass {
19
21
} ;
20
22
21
23
fn calculate_doc_coverage ( krate : clean:: Crate , ctx : & DocContext < ' _ > ) -> clean:: Crate {
22
- let mut calc = CoverageCalculator :: new ( ) ;
24
+ let mut calc = CoverageCalculator :: new ( ctx ) ;
23
25
let krate = calc. fold_crate ( krate) ;
24
26
25
- calc. print_results ( ctx . renderinfo . borrow ( ) . output_format ) ;
27
+ calc. print_results ( ) ;
26
28
27
29
krate
28
30
}
@@ -41,8 +43,11 @@ impl ItemCount {
41
43
has_docs : bool ,
42
44
has_doc_example : bool ,
43
45
should_have_doc_examples : bool ,
46
+ should_have_docs : bool ,
44
47
) {
45
- self . total += 1 ;
48
+ if has_docs || should_have_docs {
49
+ self . total += 1 ;
50
+ }
46
51
47
52
if has_docs {
48
53
self . with_docs += 1 ;
@@ -94,8 +99,9 @@ impl ops::AddAssign for ItemCount {
94
99
}
95
100
}
96
101
97
- struct CoverageCalculator {
102
+ struct CoverageCalculator < ' a , ' b > {
98
103
items : BTreeMap < FileName , ItemCount > ,
104
+ ctx : & ' a DocContext < ' b > ,
99
105
}
100
106
101
107
fn limit_filename_len ( filename : String ) -> String {
@@ -108,9 +114,9 @@ fn limit_filename_len(filename: String) -> String {
108
114
}
109
115
}
110
116
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 }
114
120
}
115
121
116
122
fn to_json ( & self ) -> String {
@@ -124,7 +130,8 @@ impl CoverageCalculator {
124
130
. expect ( "failed to convert JSON data to string" )
125
131
}
126
132
127
- fn print_results ( & self , output_format : Option < OutputFormat > ) {
133
+ fn print_results ( & self ) {
134
+ let output_format = self . ctx . renderinfo . borrow ( ) . output_format ;
128
135
if output_format. map ( |o| o. is_json ( ) ) . unwrap_or_else ( || false ) {
129
136
println ! ( "{}" , self . to_json( ) ) ;
130
137
return ;
@@ -178,7 +185,7 @@ impl CoverageCalculator {
178
185
}
179
186
}
180
187
181
- impl fold:: DocFolder for CoverageCalculator {
188
+ impl < ' a , ' b > fold:: DocFolder for CoverageCalculator < ' a , ' b > {
182
189
fn fold_item ( & mut self , i : clean:: Item ) -> Option < clean:: Item > {
183
190
match i. inner {
184
191
_ if !i. def_id . is_local ( ) => {
@@ -245,11 +252,18 @@ impl fold::DocFolder for CoverageCalculator {
245
252
) ;
246
253
247
254
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 ) ;
248
261
debug ! ( "counting {:?} {:?} in {}" , i. type_( ) , i. name, i. source. filename) ;
249
262
self . items . entry ( i. source . filename . clone ( ) ) . or_default ( ) . count_item (
250
263
has_docs,
251
264
has_doc_example,
252
- should_have_doc_example ( & i. inner ) ,
265
+ should_have_doc_example ( self . ctx , & i) ,
266
+ should_have_docs,
253
267
) ;
254
268
}
255
269
}
0 commit comments