Skip to content

Commit 37e4cfe

Browse files
committed
Move lint machinery into a separate file
1 parent cc62018 commit 37e4cfe

File tree

9 files changed

+183
-180
lines changed

9 files changed

+183
-180
lines changed

src/librustdoc/core.rs

+7-168
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ use rustc_hir::{
1212
Path,
1313
};
1414
use rustc_interface::{interface, Queries};
15-
use rustc_lint::LintStore;
16-
use rustc_lint_defs::{declare_tool_lint, Lint, LintId};
1715
use rustc_middle::hir::map::Map;
1816
use rustc_middle::middle::privacy::AccessLevels;
1917
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
@@ -28,7 +26,6 @@ use rustc_span::DUMMY_SP;
2826

2927
use std::cell::RefCell;
3028
use std::collections::hash_map::Entry;
31-
use std::lazy::SyncLazy as Lazy;
3229
use std::mem;
3330
use std::rc::Rc;
3431

@@ -232,164 +229,6 @@ crate fn new_handler(
232229
)
233230
}
234231

235-
/// This function is used to setup the lint initialization. By default, in rustdoc, everything
236-
/// is "allowed". Depending if we run in test mode or not, we want some of them to be at their
237-
/// default level. For example, the "INVALID_CODEBLOCK_ATTRIBUTES" lint is activated in both
238-
/// modes.
239-
///
240-
/// A little detail easy to forget is that there is a way to set the lint level for all lints
241-
/// through the "WARNINGS" lint. To prevent this to happen, we set it back to its "normal" level
242-
/// inside this function.
243-
///
244-
/// It returns a tuple containing:
245-
/// * Vector of tuples of lints' name and their associated "max" level
246-
/// * HashMap of lint id with their associated "max" level
247-
pub(crate) fn init_lints<F>(
248-
mut allowed_lints: Vec<String>,
249-
lint_opts: Vec<(String, lint::Level)>,
250-
filter_call: F,
251-
) -> (Vec<(String, lint::Level)>, FxHashMap<lint::LintId, lint::Level>)
252-
where
253-
F: Fn(&lint::Lint) -> Option<(String, lint::Level)>,
254-
{
255-
let warnings_lint_name = lint::builtin::WARNINGS.name;
256-
257-
allowed_lints.push(warnings_lint_name.to_owned());
258-
allowed_lints.extend(lint_opts.iter().map(|(lint, _)| lint).cloned());
259-
260-
let lints = || {
261-
lint::builtin::HardwiredLints::get_lints()
262-
.into_iter()
263-
.chain(rustc_lint::SoftLints::get_lints().into_iter())
264-
};
265-
266-
let lint_opts = lints()
267-
.filter_map(|lint| {
268-
// Permit feature-gated lints to avoid feature errors when trying to
269-
// allow all lints.
270-
if lint.feature_gate.is_some() || allowed_lints.iter().any(|l| lint.name == l) {
271-
None
272-
} else {
273-
filter_call(lint)
274-
}
275-
})
276-
.chain(lint_opts.into_iter())
277-
.collect::<Vec<_>>();
278-
279-
let lint_caps = lints()
280-
.filter_map(|lint| {
281-
// We don't want to allow *all* lints so let's ignore
282-
// those ones.
283-
if allowed_lints.iter().any(|l| lint.name == l) {
284-
None
285-
} else {
286-
Some((lint::LintId::of(lint), lint::Allow))
287-
}
288-
})
289-
.collect();
290-
(lint_opts, lint_caps)
291-
}
292-
293-
declare_tool_lint! {
294-
/// The `broken_intra_doc_links` lint detects failures in resolving
295-
/// intra-doc link targets. This is a `rustdoc` only lint, see the
296-
/// documentation in the [rustdoc book].
297-
///
298-
/// [rustdoc book]: ../../../rustdoc/lints.html#broken_intra_doc_links
299-
pub rustdoc::BROKEN_INTRA_DOC_LINKS,
300-
Warn,
301-
"failures in resolving intra-doc link targets"
302-
}
303-
304-
declare_tool_lint! {
305-
/// This is a subset of `broken_intra_doc_links` that warns when linking from
306-
/// a public item to a private one. This is a `rustdoc` only lint, see the
307-
/// documentation in the [rustdoc book].
308-
///
309-
/// [rustdoc book]: ../../../rustdoc/lints.html#private_intra_doc_links
310-
pub rustdoc::PRIVATE_INTRA_DOC_LINKS,
311-
Warn,
312-
"linking from a public item to a private one"
313-
}
314-
315-
declare_tool_lint! {
316-
/// The `invalid_codeblock_attributes` lint detects code block attributes
317-
/// in documentation examples that have potentially mis-typed values. This
318-
/// is a `rustdoc` only lint, see the documentation in the [rustdoc book].
319-
///
320-
/// [rustdoc book]: ../../../rustdoc/lints.html#invalid_codeblock_attributes
321-
pub rustdoc::INVALID_CODEBLOCK_ATTRIBUTES,
322-
Warn,
323-
"codeblock attribute looks a lot like a known one"
324-
}
325-
326-
declare_tool_lint! {
327-
/// The `missing_doc_code_examples` lint detects publicly-exported items
328-
/// without code samples in their documentation. This is a `rustdoc` only
329-
/// lint, see the documentation in the [rustdoc book].
330-
///
331-
/// [rustdoc book]: ../../../rustdoc/lints.html#missing_doc_code_examples
332-
pub rustdoc::MISSING_DOC_CODE_EXAMPLES,
333-
Allow,
334-
"detects publicly-exported items without code samples in their documentation"
335-
}
336-
337-
declare_tool_lint! {
338-
/// The `private_doc_tests` lint detects code samples in docs of private
339-
/// items not documented by `rustdoc`. This is a `rustdoc` only lint, see
340-
/// the documentation in the [rustdoc book].
341-
///
342-
/// [rustdoc book]: ../../../rustdoc/lints.html#private_doc_tests
343-
pub rustdoc::PRIVATE_DOC_TESTS,
344-
Allow,
345-
"detects code samples in docs of private items not documented by rustdoc"
346-
}
347-
348-
declare_tool_lint! {
349-
/// The `invalid_html_tags` lint detects invalid HTML tags. This is a
350-
/// `rustdoc` only lint, see the documentation in the [rustdoc book].
351-
///
352-
/// [rustdoc book]: ../../../rustdoc/lints.html#invalid_html_tags
353-
pub rustdoc::INVALID_HTML_TAGS,
354-
Allow,
355-
"detects invalid HTML tags in doc comments"
356-
}
357-
358-
declare_tool_lint! {
359-
/// The `non_autolinks` lint detects when a URL could be written using
360-
/// only angle brackets. This is a `rustdoc` only lint, see the
361-
/// documentation in the [rustdoc book].
362-
///
363-
/// [rustdoc book]: ../../../rustdoc/lints.html#non_autolinks
364-
pub rustdoc::NON_AUTOLINKS,
365-
Warn,
366-
"detects URLs that could be written using only angle brackets"
367-
}
368-
369-
static RUSTDOC_LINTS: Lazy<Vec<&'static Lint>> = Lazy::new(|| {
370-
vec![
371-
BROKEN_INTRA_DOC_LINKS,
372-
PRIVATE_INTRA_DOC_LINKS,
373-
MISSING_DOC_CODE_EXAMPLES,
374-
PRIVATE_DOC_TESTS,
375-
INVALID_CODEBLOCK_ATTRIBUTES,
376-
INVALID_HTML_TAGS,
377-
NON_AUTOLINKS,
378-
]
379-
});
380-
381-
crate fn register_lints(_sess: &Session, lint_store: &mut LintStore) {
382-
lint_store.register_lints(&**RUSTDOC_LINTS);
383-
lint_store.register_group(
384-
true,
385-
"rustdoc",
386-
None,
387-
RUSTDOC_LINTS.iter().map(|&lint| LintId::of(lint)).collect(),
388-
);
389-
lint_store
390-
.register_renamed("intra_doc_link_resolution_failure", "rustdoc::broken_intra_doc_links");
391-
}
392-
393232
/// Parse, resolve, and typecheck the given crate.
394233
crate fn create_config(
395234
RustdocOptions {
@@ -418,8 +257,8 @@ crate fn create_config(
418257
let cpath = Some(input.clone());
419258
let input = Input::File(input);
420259

421-
// In addition to those specific lints, we also need to allow those given through
422-
// command line, otherwise they'll get ignored and we don't want that.
260+
// By default, rustdoc ignores all lints.
261+
// Specifically unblock lints relevant to documentation or the lint machinery itself.
423262
let mut lints_to_show = vec![
424263
// it's unclear whether these should be part of rustdoc directly
425264
rustc_lint::builtin::MISSING_DOCS.name.to_string(),
@@ -428,12 +267,12 @@ crate fn create_config(
428267
rustc_lint::builtin::RENAMED_AND_REMOVED_LINTS.name.to_string(),
429268
rustc_lint::builtin::UNKNOWN_LINTS.name.to_string(),
430269
];
431-
lints_to_show.extend(RUSTDOC_LINTS.iter().map(|lint| lint.name.to_string()));
270+
lints_to_show.extend(crate::lint::RUSTDOC_LINTS.iter().map(|lint| lint.name.to_string()));
432271

433-
let (lint_opts, lint_caps) = init_lints(lints_to_show, lint_opts, |lint| {
272+
let (lint_opts, lint_caps) = crate::lint::init_lints(lints_to_show, lint_opts, |lint| {
434273
// FIXME: why is this necessary?
435-
if lint.name == BROKEN_INTRA_DOC_LINKS.name
436-
|| lint.name == INVALID_CODEBLOCK_ATTRIBUTES.name
274+
if lint.name == crate::lint::BROKEN_INTRA_DOC_LINKS.name
275+
|| lint.name == crate::lint::INVALID_CODEBLOCK_ATTRIBUTES.name
437276
{
438277
None
439278
} else {
@@ -474,7 +313,7 @@ crate fn create_config(
474313
diagnostic_output: DiagnosticOutput::Default,
475314
stderr: None,
476315
lint_caps,
477-
register_lints: Some(box register_lints),
316+
register_lints: Some(box crate::lint::register_lints),
478317
override_queries: Some(|_sess, providers, _external_providers| {
479318
// Most lints will require typechecking, so just don't run them.
480319
providers.lint_mod = |_, _| {};

src/librustdoc/doctest.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ use std::str;
2626

2727
use crate::clean::Attributes;
2828
use crate::config::Options;
29-
use crate::core::init_lints;
3029
use crate::html::markdown::{self, ErrorCodes, Ignore, LangString};
30+
use crate::lint::init_lints;
3131
use crate::passes::span_of_attrs;
3232

3333
#[derive(Clone, Default)]
@@ -44,10 +44,9 @@ crate struct TestOptions {
4444
crate fn run(options: Options) -> Result<(), ErrorReported> {
4545
let input = config::Input::File(options.input.clone());
4646

47-
let invalid_codeblock_attributes_name = crate::core::INVALID_CODEBLOCK_ATTRIBUTES.name;
47+
let invalid_codeblock_attributes_name = crate::lint::INVALID_CODEBLOCK_ATTRIBUTES.name;
4848

49-
// In addition to those specific lints, we also need to allow those given through
50-
// command line, otherwise they'll get ignored and we don't want that.
49+
// See core::create_config for what's going on here.
5150
let allowed_lints = vec![
5251
invalid_codeblock_attributes_name.to_owned(),
5352
lint::builtin::UNKNOWN_LINTS.name.to_owned(),
@@ -96,7 +95,7 @@ crate fn run(options: Options) -> Result<(), ErrorReported> {
9695
diagnostic_output: DiagnosticOutput::Default,
9796
stderr: None,
9897
lint_caps,
99-
register_lints: Some(box crate::core::register_lints),
98+
register_lints: Some(box crate::lint::register_lints),
10099
override_queries: None,
101100
make_codegen_backend: None,
102101
registry: rustc_driver::diagnostics_registry(),

src/librustdoc/html/markdown.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ impl<'tcx> ExtraInfo<'tcx> {
720720
(None, None) => return,
721721
};
722722
self.tcx.struct_span_lint_hir(
723-
crate::core::INVALID_CODEBLOCK_ATTRIBUTES,
723+
crate::lint::INVALID_CODEBLOCK_ATTRIBUTES,
724724
hir_id,
725725
self.sp,
726726
|lint| {

src/librustdoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ mod formats;
8686
// used by the error-index generator, so it needs to be public
8787
pub mod html;
8888
mod json;
89+
crate mod lint;
8990
mod markdown;
9091
mod passes;
9192
mod theme;

0 commit comments

Comments
 (0)