Skip to content

Commit 807e8b8

Browse files
authored
Rollup merge of rust-lang#71581 - GuillaumeGomez:unify-lints-handling, r=kinnison
Unify lints handling in rustdoc This is a small cleanup. The goal is to unify a bit things to make the reading simpler. r? @kinnison cc @rust-lang/rustdoc
2 parents 7b80539 + 9ae85e1 commit 807e8b8

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
@@ -204,6 +204,56 @@ pub fn new_handler(
204204
)
205205
}
206206

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

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

249299
let intra_link_resolution_failure_name = lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE.name;
250-
let warnings_lint_name = lint::builtin::WARNINGS.name;
251300
let missing_docs = rustc_lint::builtin::MISSING_DOCS.name;
252301
let missing_doc_example = rustc_lint::builtin::MISSING_DOC_CODE_EXAMPLES.name;
253302
let private_doc_tests = rustc_lint::builtin::PRIVATE_DOC_TESTS.name;
@@ -256,8 +305,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
256305

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

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

303327
let crate_types =
304328
if proc_macro_crate { vec![CrateType::ProcMacro] } else { vec![CrateType::Rlib] };

src/librustdoc/test.rs

+9-33
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use std::str;
2828

2929
use crate::clean::Attributes;
3030
use crate::config::Options;
31+
use crate::core::init_lints;
3132
use crate::html::markdown::{self, ErrorCodes, Ignore, LangString};
3233
use crate::passes::span_of_attrs;
3334

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

48-
let warnings_lint_name = lint::builtin::WARNINGS.name;
4949
let invalid_codeblock_attribute_name = rustc_lint::builtin::INVALID_CODEBLOCK_ATTRIBUTE.name;
5050

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

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

8763
let crate_types =
8864
if options.proc_macro_crate { vec![CrateType::ProcMacro] } else { vec![CrateType::Rlib] };

0 commit comments

Comments
 (0)