]>
Commit | Line | Data |
---|---|---|
6a06907d XL |
1 | use rustc_data_structures::fx::FxHashMap; |
2 | use rustc_lint::LintStore; | |
3 | use rustc_lint_defs::{declare_tool_lint, Lint, LintId}; | |
4 | use rustc_session::{lint, Session}; | |
5 | ||
6 | use std::lazy::SyncLazy as Lazy; | |
7 | ||
8 | /// This function is used to setup the lint initialization. By default, in rustdoc, everything | |
9 | /// is "allowed". Depending if we run in test mode or not, we want some of them to be at their | |
10 | /// default level. For example, the "INVALID_CODEBLOCK_ATTRIBUTES" lint is activated in both | |
11 | /// modes. | |
12 | /// | |
13 | /// A little detail easy to forget is that there is a way to set the lint level for all lints | |
14 | /// through the "WARNINGS" lint. To prevent this to happen, we set it back to its "normal" level | |
15 | /// inside this function. | |
16 | /// | |
17 | /// It returns a tuple containing: | |
18 | /// * Vector of tuples of lints' name and their associated "max" level | |
19 | /// * HashMap of lint id with their associated "max" level | |
20 | pub(crate) fn init_lints<F>( | |
21 | mut allowed_lints: Vec<String>, | |
22 | lint_opts: Vec<(String, lint::Level)>, | |
23 | filter_call: F, | |
24 | ) -> (Vec<(String, lint::Level)>, FxHashMap<lint::LintId, lint::Level>) | |
25 | where | |
26 | F: Fn(&lint::Lint) -> Option<(String, lint::Level)>, | |
27 | { | |
28 | let warnings_lint_name = lint::builtin::WARNINGS.name; | |
29 | ||
30 | allowed_lints.push(warnings_lint_name.to_owned()); | |
31 | allowed_lints.extend(lint_opts.iter().map(|(lint, _)| lint).cloned()); | |
32 | ||
33 | let lints = || { | |
34 | lint::builtin::HardwiredLints::get_lints() | |
35 | .into_iter() | |
36 | .chain(rustc_lint::SoftLints::get_lints().into_iter()) | |
37 | }; | |
38 | ||
39 | let lint_opts = lints() | |
40 | .filter_map(|lint| { | |
41 | // Permit feature-gated lints to avoid feature errors when trying to | |
42 | // allow all lints. | |
43 | if lint.feature_gate.is_some() || allowed_lints.iter().any(|l| lint.name == l) { | |
44 | None | |
45 | } else { | |
46 | filter_call(lint) | |
47 | } | |
48 | }) | |
49 | .chain(lint_opts.into_iter()) | |
50 | .collect::<Vec<_>>(); | |
51 | ||
52 | let lint_caps = lints() | |
53 | .filter_map(|lint| { | |
54 | // We don't want to allow *all* lints so let's ignore | |
55 | // those ones. | |
56 | if allowed_lints.iter().any(|l| lint.name == l) { | |
57 | None | |
58 | } else { | |
59 | Some((lint::LintId::of(lint), lint::Allow)) | |
60 | } | |
61 | }) | |
62 | .collect(); | |
63 | (lint_opts, lint_caps) | |
64 | } | |
65 | ||
66 | macro_rules! declare_rustdoc_lint { | |
67 | ($(#[$attr:meta])* $name: ident, $level: ident, $descr: literal $(,)?) => { | |
68 | declare_tool_lint! { | |
69 | $(#[$attr])* pub rustdoc::$name, $level, $descr | |
70 | } | |
71 | } | |
72 | } | |
73 | ||
74 | declare_rustdoc_lint! { | |
75 | /// The `broken_intra_doc_links` lint detects failures in resolving | |
76 | /// intra-doc link targets. This is a `rustdoc` only lint, see the | |
77 | /// documentation in the [rustdoc book]. | |
78 | /// | |
79 | /// [rustdoc book]: ../../../rustdoc/lints.html#broken_intra_doc_links | |
80 | BROKEN_INTRA_DOC_LINKS, | |
81 | Warn, | |
82 | "failures in resolving intra-doc link targets" | |
83 | } | |
84 | ||
85 | declare_rustdoc_lint! { | |
86 | /// This is a subset of `broken_intra_doc_links` that warns when linking from | |
87 | /// a public item to a private one. This is a `rustdoc` only lint, see the | |
88 | /// documentation in the [rustdoc book]. | |
89 | /// | |
90 | /// [rustdoc book]: ../../../rustdoc/lints.html#private_intra_doc_links | |
91 | PRIVATE_INTRA_DOC_LINKS, | |
92 | Warn, | |
93 | "linking from a public item to a private one" | |
94 | } | |
95 | ||
96 | declare_rustdoc_lint! { | |
97 | /// The `invalid_codeblock_attributes` lint detects code block attributes | |
98 | /// in documentation examples that have potentially mis-typed values. This | |
99 | /// is a `rustdoc` only lint, see the documentation in the [rustdoc book]. | |
100 | /// | |
101 | /// [rustdoc book]: ../../../rustdoc/lints.html#invalid_codeblock_attributes | |
102 | INVALID_CODEBLOCK_ATTRIBUTES, | |
103 | Warn, | |
104 | "codeblock attribute looks a lot like a known one" | |
105 | } | |
106 | ||
107 | declare_rustdoc_lint! { | |
108 | /// The `missing_crate_level_docs` lint detects if documentation is | |
109 | /// missing at the crate root. This is a `rustdoc` only lint, see the | |
110 | /// documentation in the [rustdoc book]. | |
111 | /// | |
112 | /// [rustdoc book]: ../../../rustdoc/lints.html#missing_crate_level_docs | |
113 | MISSING_CRATE_LEVEL_DOCS, | |
114 | Allow, | |
115 | "detects crates with no crate-level documentation" | |
116 | } | |
117 | ||
118 | declare_rustdoc_lint! { | |
119 | /// The `missing_doc_code_examples` lint detects publicly-exported items | |
120 | /// without code samples in their documentation. This is a `rustdoc` only | |
121 | /// lint, see the documentation in the [rustdoc book]. | |
122 | /// | |
123 | /// [rustdoc book]: ../../../rustdoc/lints.html#missing_doc_code_examples | |
124 | MISSING_DOC_CODE_EXAMPLES, | |
125 | Allow, | |
126 | "detects publicly-exported items without code samples in their documentation" | |
127 | } | |
128 | ||
129 | declare_rustdoc_lint! { | |
130 | /// The `private_doc_tests` lint detects code samples in docs of private | |
131 | /// items not documented by `rustdoc`. This is a `rustdoc` only lint, see | |
132 | /// the documentation in the [rustdoc book]. | |
133 | /// | |
134 | /// [rustdoc book]: ../../../rustdoc/lints.html#private_doc_tests | |
135 | PRIVATE_DOC_TESTS, | |
136 | Allow, | |
137 | "detects code samples in docs of private items not documented by rustdoc" | |
138 | } | |
139 | ||
140 | declare_rustdoc_lint! { | |
141 | /// The `invalid_html_tags` lint detects invalid HTML tags. This is a | |
142 | /// `rustdoc` only lint, see the documentation in the [rustdoc book]. | |
143 | /// | |
144 | /// [rustdoc book]: ../../../rustdoc/lints.html#invalid_html_tags | |
145 | INVALID_HTML_TAGS, | |
146 | Allow, | |
147 | "detects invalid HTML tags in doc comments" | |
148 | } | |
149 | ||
150 | declare_rustdoc_lint! { | |
cdc7bbd5 XL |
151 | /// The `bare_urls` lint detects when a URL is not a hyperlink. |
152 | /// This is a `rustdoc` only lint, see the documentation in the [rustdoc book]. | |
6a06907d | 153 | /// |
cdc7bbd5 XL |
154 | /// [rustdoc book]: ../../../rustdoc/lints.html#bare_urls |
155 | BARE_URLS, | |
6a06907d | 156 | Warn, |
cdc7bbd5 | 157 | "detects URLs that are not hyperlinks" |
6a06907d XL |
158 | } |
159 | ||
17df50a5 XL |
160 | declare_rustdoc_lint! { |
161 | /// The `invalid_rust_codeblocks` lint detects Rust code blocks in | |
162 | /// documentation examples that are invalid (e.g. empty, not parsable as | |
163 | /// Rust code). This is a `rustdoc` only lint, see the documentation in the | |
164 | /// [rustdoc book]. | |
165 | /// | |
166 | /// [rustdoc book]: ../../../rustdoc/lints.html#invalid_rust_codeblocks | |
167 | INVALID_RUST_CODEBLOCKS, | |
168 | Warn, | |
169 | "codeblock could not be parsed as valid Rust or is empty" | |
170 | } | |
171 | ||
6a06907d XL |
172 | crate static RUSTDOC_LINTS: Lazy<Vec<&'static Lint>> = Lazy::new(|| { |
173 | vec![ | |
174 | BROKEN_INTRA_DOC_LINKS, | |
175 | PRIVATE_INTRA_DOC_LINKS, | |
176 | MISSING_DOC_CODE_EXAMPLES, | |
177 | PRIVATE_DOC_TESTS, | |
178 | INVALID_CODEBLOCK_ATTRIBUTES, | |
17df50a5 | 179 | INVALID_RUST_CODEBLOCKS, |
6a06907d | 180 | INVALID_HTML_TAGS, |
cdc7bbd5 | 181 | BARE_URLS, |
6a06907d XL |
182 | MISSING_CRATE_LEVEL_DOCS, |
183 | ] | |
184 | }); | |
185 | ||
186 | crate fn register_lints(_sess: &Session, lint_store: &mut LintStore) { | |
187 | lint_store.register_lints(&**RUSTDOC_LINTS); | |
188 | lint_store.register_group( | |
189 | true, | |
190 | "rustdoc::all", | |
191 | Some("rustdoc"), | |
192 | RUSTDOC_LINTS.iter().map(|&lint| LintId::of(lint)).collect(), | |
193 | ); | |
194 | for lint in &*RUSTDOC_LINTS { | |
195 | let name = lint.name_lower(); | |
136023e0 | 196 | lint_store.register_renamed(&name.replace("rustdoc::", ""), &name); |
6a06907d XL |
197 | } |
198 | lint_store | |
199 | .register_renamed("intra_doc_link_resolution_failure", "rustdoc::broken_intra_doc_links"); | |
cdc7bbd5 XL |
200 | lint_store.register_renamed("non_autolinks", "rustdoc::bare_urls"); |
201 | lint_store.register_renamed("rustdoc::non_autolinks", "rustdoc::bare_urls"); | |
6a06907d | 202 | } |