Skip to content

Commit 3b13398

Browse files
committed
Move lint machinery into a separate file
1 parent 8f4d435 commit 3b13398

File tree

9 files changed

+183
-180
lines changed

9 files changed

+183
-180
lines changed

src/librustdoc/core.rs

Lines changed: 7 additions & 168 deletions
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

@@ -227,164 +224,6 @@ crate fn new_handler(
227224
)
228225
}
229226

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

416-
// In addition to those specific lints, we also need to allow those given through
417-
// command line, otherwise they'll get ignored and we don't want that.
255+
// By default, rustdoc ignores all lints.
256+
// Specifically unblock lints relevant to documentation or the lint machinery itself.
418257
let mut lints_to_show = vec![
419258
// it's unclear whether these should be part of rustdoc directly
420259
rustc_lint::builtin::MISSING_DOCS.name.to_string(),
@@ -423,12 +262,12 @@ crate fn create_config(
423262
rustc_lint::builtin::RENAMED_AND_REMOVED_LINTS.name.to_string(),
424263
rustc_lint::builtin::UNKNOWN_LINTS.name.to_string(),
425264
];
426-
lints_to_show.extend(RUSTDOC_LINTS.iter().map(|lint| lint.name.to_string()));
265+
lints_to_show.extend(crate::lint::RUSTDOC_LINTS.iter().map(|lint| lint.name.to_string()));
427266

428-
let (lint_opts, lint_caps) = init_lints(lints_to_show, lint_opts, |lint| {
267+
let (lint_opts, lint_caps) = crate::lint::init_lints(lints_to_show, lint_opts, |lint| {
429268
// FIXME: why is this necessary?
430-
if lint.name == BROKEN_INTRA_DOC_LINKS.name
431-
|| lint.name == INVALID_CODEBLOCK_ATTRIBUTES.name
269+
if lint.name == crate::lint::BROKEN_INTRA_DOC_LINKS.name
270+
|| lint.name == crate::lint::INVALID_CODEBLOCK_ATTRIBUTES.name
432271
{
433272
None
434273
} else {
@@ -469,7 +308,7 @@ crate fn create_config(
469308
diagnostic_output: DiagnosticOutput::Default,
470309
stderr: None,
471310
lint_caps,
472-
register_lints: Some(box register_lints),
311+
register_lints: Some(box crate::lint::register_lints),
473312
override_queries: Some(|_sess, providers, _external_providers| {
474313
// Most lints will require typechecking, so just don't run them.
475314
providers.lint_mod = |_, _| {};

src/librustdoc/doctest.rs

Lines changed: 4 additions & 5 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ impl<'tcx> ExtraInfo<'tcx> {
719719
(None, None) => return,
720720
};
721721
self.tcx.struct_span_lint_hir(
722-
crate::core::INVALID_CODEBLOCK_ATTRIBUTES,
722+
crate::lint::INVALID_CODEBLOCK_ATTRIBUTES,
723723
hir_id,
724724
self.sp,
725725
|lint| {

src/librustdoc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ mod formats;
8787
// used by the error-index generator, so it needs to be public
8888
pub mod html;
8989
mod json;
90+
crate mod lint;
9091
mod markdown;
9192
mod passes;
9293
mod theme;

0 commit comments

Comments
 (0)