Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit b31f5d0

Browse files
Inherit lint level from parents
1 parent 02e6b86 commit b31f5d0

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

src/librustdoc/passes/calculate_doc_coverage.rs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use crate::clean;
2-
use crate::config::OutputFormat;
32
use crate::core::DocContext;
43
use crate::fold::{self, DocFolder};
54
use crate::html::markdown::{find_testable_code, ErrorCodes};
65
use crate::passes::doc_test_lints::{should_have_doc_example, Tests};
76
use crate::passes::Pass;
8-
use rustc_span::symbol::{sym, Ident};
7+
use rustc_lint::builtin::MISSING_DOCS;
8+
use rustc_middle::lint::LintSource;
9+
use rustc_session::lint;
10+
use rustc_span::symbol::sym;
911
use rustc_span::FileName;
1012
use serde::Serialize;
1113

@@ -19,10 +21,10 @@ pub const CALCULATE_DOC_COVERAGE: Pass = Pass {
1921
};
2022

2123
fn 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
}
@@ -97,8 +99,9 @@ impl ops::AddAssign for ItemCount {
9799
}
98100
}
99101

100-
struct CoverageCalculator {
102+
struct CoverageCalculator<'a, 'b> {
101103
items: BTreeMap<FileName, ItemCount>,
104+
ctx: &'a DocContext<'b>,
102105
}
103106

104107
fn limit_filename_len(filename: String) -> String {
@@ -111,9 +114,9 @@ fn limit_filename_len(filename: String) -> String {
111114
}
112115
}
113116

114-
impl CoverageCalculator {
115-
fn new() -> CoverageCalculator {
116-
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 }
117120
}
118121

119122
fn to_json(&self) -> String {
@@ -127,7 +130,8 @@ impl CoverageCalculator {
127130
.expect("failed to convert JSON data to string")
128131
}
129132

130-
fn print_results(&self, output_format: Option<OutputFormat>) {
133+
fn print_results(&self) {
134+
let output_format = self.ctx.renderinfo.borrow().output_format;
131135
if output_format.map(|o| o.is_json()).unwrap_or_else(|| false) {
132136
println!("{}", self.to_json());
133137
return;
@@ -181,7 +185,7 @@ impl CoverageCalculator {
181185
}
182186
}
183187

184-
impl fold::DocFolder for CoverageCalculator {
188+
impl<'a, 'b> fold::DocFolder for CoverageCalculator<'a, 'b> {
185189
fn fold_item(&mut self, i: clean::Item) -> Option<clean::Item> {
186190
match i.inner {
187191
_ if !i.def_id.is_local() => {
@@ -232,15 +236,6 @@ impl fold::DocFolder for CoverageCalculator {
232236
}
233237
_ => {
234238
let has_docs = !i.attrs.doc_strings.is_empty();
235-
let should_have_docs = !i.attrs.other_attrs.iter().any(|a| {
236-
a.has_name(sym::allow)
237-
&& a.meta_item_list().iter().any(|meta_list_item| {
238-
meta_list_item.iter().any(|li| match li.ident() {
239-
Some(ident) => ident == Ident::from_str("missing_docs"),
240-
_ => false,
241-
})
242-
})
243-
});
244239
let mut tests = Tests { found_tests: 0 };
245240

246241
find_testable_code(
@@ -257,17 +252,16 @@ impl fold::DocFolder for CoverageCalculator {
257252
);
258253

259254
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+
let should_have_docs = level != lint::Level::Allow || !matches!(source, LintSource::Node(..));
260258
debug!("counting {:?} {:?} in {}", i.type_(), i.name, i.source.filename);
261259
self.items.entry(i.source.filename.clone()).or_default().count_item(
262260
has_docs,
263261
has_doc_example,
264262
should_have_doc_example(&i.inner),
265263
should_have_docs,
266264
);
267-
268-
if !should_have_docs {
269-
return Some(i);
270-
}
271265
}
272266
}
273267

src/test/rustdoc-ui/coverage/allow_missing_docs.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,11 @@ pub fn this_is_func() {}
2929
pub struct DemoStruct {
3030
something: usize,
3131
}
32+
33+
#[allow(missing_docs)]
34+
pub mod bar {
35+
#[warn(missing_docs)]
36+
pub struct Bar { //~ WARN
37+
pub f: u32, //~ WARN
38+
}
39+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
+-------------------------------------+------------+------------+------------+------------+
22
| File | Documented | Percentage | Examples | Percentage |
33
+-------------------------------------+------------+------------+------------+------------+
4-
| ...i/coverage/allow_missing_docs.rs | 5 | 100.0% | 0 | 0.0% |
4+
| ...i/coverage/allow_missing_docs.rs | 5 | 71.4% | 0 | 0.0% |
55
+-------------------------------------+------------+------------+------------+------------+
6-
| Total | 5 | 100.0% | 0 | 0.0% |
6+
| Total | 5 | 71.4% | 0 | 0.0% |
77
+-------------------------------------+------------+------------+------------+------------+

0 commit comments

Comments
 (0)