3 `rustdoc` provides lints to help you writing and testing your documentation. You
4 can use them like any other lints by doing this:
7 #![allow(rustdoc::broken_intra_doc_links)] // allows the lint, no diagnostics will be reported
8 #![warn(rustdoc::broken_intra_doc_links)] // warn if there are broken intra-doc links
9 #![deny(rustdoc::broken_intra_doc_links)] // error if there are broken intra-doc links
12 Note that, except for `missing_docs`, these lints are only available when running `rustdoc`, not `rustc`.
14 Here is the list of the lints provided by `rustdoc`:
16 ## broken_intra_doc_links
18 This lint **warns by default**. This lint detects when an [intra-doc link] fails to be resolved. For example:
20 [intra-doc link]: linking-to-items-by-name.md
23 /// I want to link to [`Nonexistent`] but it doesn't exist!
27 You'll get a warning saying:
30 warning: unresolved link to `Nonexistent`
33 1 | /// I want to link to [`Nonexistent`] but it doesn't exist!
34 | ^^^^^^^^^^^^^ no item named `Nonexistent` in `test`
37 It will also warn when there is an ambiguity and suggest how to disambiguate:
49 warning: `Foo` is both an enum and a function
53 | ^^^^^ ambiguous link
55 = note: `#[warn(rustdoc::broken_intra_doc_links)]` on by default
56 help: to link to the enum, prefix with the item type
60 help: to link to the function, add parentheses
67 ## private_intra_doc_links
69 This lint **warns by default**. This lint detects when [intra-doc links] from public to private items.
78 This gives a warning that the link will be broken when it appears in your documentation:
81 warning: public documentation for `public` links to private item `private`
85 | ^^^^^^^ this item is private
87 = note: `#[warn(rustdoc::private_intra_doc_links)]` on by default
88 = note: this link will resolve properly if you pass `--document-private-items`
91 Note that this has different behavior depending on whether you pass `--document-private-items` or not!
92 If you document private items, then it will still generate a link, despite the warning:
95 warning: public documentation for `public` links to private item `private`
99 | ^^^^^^^ this item is private
101 = note: `#[warn(rustdoc::private_intra_doc_links)]` on by default
102 = note: this link resolves only because you passed `--document-private-items`, but will break without
105 [intra-doc links]: linking-to-items-by-name.html
109 This lint is **allowed by default**. It detects items missing documentation.
113 #![warn(missing_docs)]
115 pub fn undocumented() {}
119 The `undocumented` function will then have the following warning:
122 warning: missing documentation for a function
123 --> your-crate/lib.rs:3:1
125 3 | pub fn undocumented() {}
126 | ^^^^^^^^^^^^^^^^^^^^^
129 Note that unlike other rustdoc lints, this lint is also available from `rustc` directly.
131 ## missing_crate_level_docs
133 This lint is **allowed by default**. It detects if there is no documentation
134 at the crate root. For example:
137 #![warn(rustdoc::missing_crate_level_docs)]
140 This will generate the following warning:
143 warning: no documentation found for this crate's top-level module
145 = help: The following guide may be of use:
146 https://doc.rust-lang.org/nightly/rustdoc/how-to-write-documentation.html
149 This is currently "allow" by default, but it is intended to make this a
150 warning in the future. This is intended as a means to introduce new users on
151 *how* to document their crate by pointing them to some instructions on how to
152 get started, without providing overwhelming warnings like `missing_docs`
155 ## missing_doc_code_examples
157 This lint is **allowed by default** and is **nightly-only**. It detects when a documentation block
158 is missing a code example. For example:
161 #![warn(rustdoc::missing_doc_code_examples)]
163 /// There is no code example!
164 pub fn no_code_example() {}
168 The `no_code_example` function will then have the following warning:
171 warning: Missing code example in this documentation
172 --> your-crate/lib.rs:3:1
174 LL | /// There is no code example!
175 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
178 To fix the lint, you need to add a code example into the documentation block:
181 /// There is no code example!
184 /// println!("calling no_code_example...");
185 /// no_code_example();
186 /// println!("we called no_code_example!");
188 pub fn no_code_example() {}
193 This lint is **allowed by default**. It detects documentation tests when they
194 are on a private item. For example:
197 #![warn(rustdoc::private_doc_tests)]
213 warning: Documentation test in private item
214 --> your-crate/lib.rs:4:1
216 4 | / /// private doc test
219 7 | | /// assert!(false);
224 ## invalid_codeblock_attributes
226 This lint **warns by default**. It detects code block attributes in
227 documentation examples that have potentially mis-typed values. For example:
233 /// assert_eq!(1, 2);
241 warning: unknown attribute `should-panic`. Did you mean `should_panic`?
246 3 | | /// ```should-panic
247 4 | | /// assert_eq!(1, 2);
251 = note: `#[warn(rustdoc::invalid_codeblock_attributes)]` on by default
252 = help: the code block will either not be tested if not marked as a rust one or won't fail if it doesn't panic when running
255 In the example above, the correct form is `should_panic`. This helps detect
256 typo mistakes for some common attributes.
260 This lint is **allowed by default** and is **nightly-only**. It detects unclosed
261 or invalid HTML tags. For example:
264 #![warn(rustdoc::invalid_html_tags)]
274 warning: unopened HTML tag `script`
281 note: the lint level is defined here
284 1 | #![warn(rustdoc::invalid_html_tags)]
285 | ^^^^^^^^^^^^^^^^^^^^^^^^^^
287 warning: unclosed HTML tag `h1`
294 warning: 2 warnings emitted
299 This lint is **warn-by-default**. It detects URLs which are not links.
303 /// http://example.org
304 /// [http://example.net]
311 warning: this URL is not a hyperlink
314 1 | /// http://example.org
315 | ^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<http://example.org>`
317 = note: `#[warn(rustdoc::bare_urls)]` on by default
319 warning: this URL is not a hyperlink
322 3 | /// [http://example.net]
323 | ^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<http://example.net>`
325 warning: 2 warnings emitted