Skip to content

Commit 9ae85e1

Browse files
Unify lints handling in rustdoc
1 parent 019ab73 commit 9ae85e1

File tree

2 files changed

+69
-69
lines changed

2 files changed

+69
-69
lines changed

src/librustdoc/core.rs

+60-36
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,56 @@ pub fn new_handler(
205205
)
206206
}
207207

208+
/// This function is used to setup the lint initialization. By default, in rustdoc, everything
209+
/// is "allowed". Depending if we run in test mode or not, we want some of them to be at their
210+
/// default level. For example, the "INVALID_CODEBLOCK_ATTRIBUTE" lint is activated in both
211+
/// modes.
212+
///
213+
/// A little detail easy to forget is that there is a way to set the lint level for all lints
214+
/// through the "WARNINGS" lint. To prevent this to happen, we set it back to its "normal" level
215+
/// inside this function.
216+
///
217+
/// It returns a tuple containing:
218+
/// * Vector of tuples of lints' name and their associated "max" level
219+
/// * HashMap of lint id with their associated "max" level
220+
pub fn init_lints<F>(
221+
mut whitelisted_lints: Vec<String>,
222+
lint_opts: Vec<(String, lint::Level)>,
223+
filter_call: F,
224+
) -> (Vec<(String, lint::Level)>, FxHashMap<lint::LintId, lint::Level>)
225+
where
226+
F: Fn(&lint::Lint) -> Option<(String, lint::Level)>,
227+
{
228+
let warnings_lint_name = lint::builtin::WARNINGS.name;
229+
230+
whitelisted_lints.push(warnings_lint_name.to_owned());
231+
whitelisted_lints.extend(lint_opts.iter().map(|(lint, _)| lint).cloned());
232+
233+
let lints = || {
234+
lint::builtin::HardwiredLints::get_lints()
235+
.into_iter()
236+
.chain(rustc_lint::SoftLints::get_lints().into_iter())
237+
};
238+
239+
let lint_opts = lints()
240+
.filter_map(|lint| if lint.name == warnings_lint_name { None } else { filter_call(lint) })
241+
.chain(lint_opts.into_iter())
242+
.collect::<Vec<_>>();
243+
244+
let lint_caps = lints()
245+
.filter_map(|lint| {
246+
// We don't want to whitelist *all* lints so let's
247+
// ignore those ones.
248+
if whitelisted_lints.iter().any(|l| lint.name == l) {
249+
None
250+
} else {
251+
Some((lint::LintId::of(lint), lint::Allow))
252+
}
253+
})
254+
.collect();
255+
(lint_opts, lint_caps)
256+
}
257+
208258
pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOptions) {
209259
// Parse, resolve, and typecheck the given crate.
210260

@@ -248,7 +298,6 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
248298
let input = Input::File(input);
249299

250300
let intra_link_resolution_failure_name = lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE.name;
251-
let warnings_lint_name = lint::builtin::WARNINGS.name;
252301
let missing_docs = rustc_lint::builtin::MISSING_DOCS.name;
253302
let missing_doc_example = rustc_lint::builtin::MISSING_DOC_CODE_EXAMPLES.name;
254303
let private_doc_tests = rustc_lint::builtin::PRIVATE_DOC_TESTS.name;
@@ -257,8 +306,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
257306

258307
// In addition to those specific lints, we also need to whitelist those given through
259308
// command line, otherwise they'll get ignored and we don't want that.
260-
let mut whitelisted_lints = vec![
261-
warnings_lint_name.to_owned(),
309+
let whitelisted_lints = vec![
262310
intra_link_resolution_failure_name.to_owned(),
263311
missing_docs.to_owned(),
264312
missing_doc_example.to_owned(),
@@ -267,39 +315,15 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
267315
invalid_codeblock_attribute_name.to_owned(),
268316
];
269317

270-
whitelisted_lints.extend(lint_opts.iter().map(|(lint, _)| lint).cloned());
271-
272-
let lints = || {
273-
lint::builtin::HardwiredLints::get_lints()
274-
.into_iter()
275-
.chain(rustc_lint::SoftLints::get_lints().into_iter())
276-
};
277-
278-
let lint_opts = lints()
279-
.filter_map(|lint| {
280-
if lint.name == warnings_lint_name
281-
|| lint.name == intra_link_resolution_failure_name
282-
|| lint.name == invalid_codeblock_attribute_name
283-
{
284-
None
285-
} else {
286-
Some((lint.name_lower(), lint::Allow))
287-
}
288-
})
289-
.chain(lint_opts.into_iter())
290-
.collect::<Vec<_>>();
291-
292-
let lint_caps = lints()
293-
.filter_map(|lint| {
294-
// We don't want to whitelist *all* lints so let's
295-
// ignore those ones.
296-
if whitelisted_lints.iter().any(|l| lint.name == l) {
297-
None
298-
} else {
299-
Some((lint::LintId::of(lint), lint::Allow))
300-
}
301-
})
302-
.collect();
318+
let (lint_opts, lint_caps) = init_lints(whitelisted_lints, lint_opts, |lint| {
319+
if lint.name == intra_link_resolution_failure_name
320+
|| lint.name == invalid_codeblock_attribute_name
321+
{
322+
None
323+
} else {
324+
Some((lint.name_lower(), lint::Allow))
325+
}
326+
});
303327

304328
let crate_types = if proc_macro_crate {
305329
vec![config::CrateType::ProcMacro]

src/librustdoc/test.rs

+9-33
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use tempfile::Builder as TempFileBuilder;
2626

2727
use crate::clean::Attributes;
2828
use crate::config::Options;
29+
use crate::core::init_lints;
2930
use crate::html::markdown::{self, ErrorCodes, Ignore, LangString};
3031
use crate::passes::span_of_attrs;
3132

@@ -43,44 +44,19 @@ pub struct TestOptions {
4344
pub fn run(options: Options) -> i32 {
4445
let input = config::Input::File(options.input.clone());
4546

46-
let warnings_lint_name = lint::builtin::WARNINGS.name;
4747
let invalid_codeblock_attribute_name = rustc_lint::builtin::INVALID_CODEBLOCK_ATTRIBUTE.name;
4848

4949
// In addition to those specific lints, we also need to whitelist those given through
5050
// command line, otherwise they'll get ignored and we don't want that.
51-
let mut whitelisted_lints =
52-
vec![warnings_lint_name.to_owned(), invalid_codeblock_attribute_name.to_owned()];
51+
let whitelisted_lints = vec![invalid_codeblock_attribute_name.to_owned()];
5352

54-
whitelisted_lints.extend(options.lint_opts.iter().map(|(lint, _)| lint).cloned());
55-
56-
let lints = || {
57-
lint::builtin::HardwiredLints::get_lints()
58-
.into_iter()
59-
.chain(rustc_lint::SoftLints::get_lints().into_iter())
60-
};
61-
62-
let lint_opts = lints()
63-
.filter_map(|lint| {
64-
if lint.name == warnings_lint_name || lint.name == invalid_codeblock_attribute_name {
65-
None
66-
} else {
67-
Some((lint.name_lower(), lint::Allow))
68-
}
69-
})
70-
.chain(options.lint_opts.clone().into_iter())
71-
.collect::<Vec<_>>();
72-
73-
let lint_caps = lints()
74-
.filter_map(|lint| {
75-
// We don't want to whitelist *all* lints so let's
76-
// ignore those ones.
77-
if whitelisted_lints.iter().any(|l| lint.name == l) {
78-
None
79-
} else {
80-
Some((lint::LintId::of(lint), lint::Allow))
81-
}
82-
})
83-
.collect();
53+
let (lint_opts, lint_caps) = init_lints(whitelisted_lints, options.lint_opts.clone(), |lint| {
54+
if lint.name == invalid_codeblock_attribute_name {
55+
None
56+
} else {
57+
Some((lint.name_lower(), lint::Allow))
58+
}
59+
});
8460

8561
let crate_types = if options.proc_macro_crate {
8662
vec![config::CrateType::ProcMacro]

0 commit comments

Comments
 (0)