|
| 1 | +# Lints |
| 2 | + |
| 3 | +`rustdoc` provides lints to help you writing and testing your documentation. You |
| 4 | +can use them like any other lints by doing this: |
| 5 | + |
| 6 | +```rust,ignore |
| 7 | +#![allow(missing_docs)] // allowing the lint, no message |
| 8 | +#![warn(missing_docs)] // warn if there is missing docs |
| 9 | +#![deny(missing_docs)] // rustdoc will fail if there is missing docs |
| 10 | +``` |
| 11 | + |
| 12 | +Here is the list of the lints provided by `rustdoc`: |
| 13 | + |
| 14 | +## intra_doc_link_resolution_failure |
| 15 | + |
| 16 | +This lint **warns by default** and is **nightly-only**. This lint detects when |
| 17 | +an intra-doc link fails to get resolved. For example: |
| 18 | + |
| 19 | +```rust |
| 20 | +/// I want to link to [`Inexistent`] but it doesn't exist! |
| 21 | +pub fn foo() {} |
| 22 | +``` |
| 23 | + |
| 24 | +You'll get a warning saying: |
| 25 | + |
| 26 | +```text |
| 27 | +error: `[`Inexistent`]` cannot be resolved, ignoring it... |
| 28 | +``` |
| 29 | + |
| 30 | +## missing_docs |
| 31 | + |
| 32 | +This lint is **allowed by default**. It detects items missing documentation. |
| 33 | +For example: |
| 34 | + |
| 35 | +```rust |
| 36 | +#![warn(missing_docs)] |
| 37 | + |
| 38 | +pub fn undocumented() {} |
| 39 | +# fn main() {} |
| 40 | +``` |
| 41 | + |
| 42 | +The `undocumented` function will then have the following warning: |
| 43 | + |
| 44 | +```text |
| 45 | +warning: missing documentation for a function |
| 46 | + --> your-crate/lib.rs:3:1 |
| 47 | + | |
| 48 | + 3 | pub fn undocumented() {} |
| 49 | + | ^^^^^^^^^^^^^^^^^^^^^ |
| 50 | +``` |
| 51 | + |
| 52 | +## missing_doc_code_examples |
| 53 | + |
| 54 | +This lint is **allowed by default**. It detects when a documentation block |
| 55 | +is missing a code example. For example: |
| 56 | + |
| 57 | +```rust |
| 58 | +#![warn(missing_doc_code_examples)] |
| 59 | + |
| 60 | +/// There is no code example! |
| 61 | +pub fn no_code_example() {} |
| 62 | +# fn main() {} |
| 63 | +``` |
| 64 | + |
| 65 | +The `no_code_example` function will then have the following warning: |
| 66 | + |
| 67 | +```text |
| 68 | +warning: Missing code example in this documentation |
| 69 | + --> your-crate/lib.rs:3:1 |
| 70 | + | |
| 71 | +LL | /// There is no code example! |
| 72 | + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 73 | +``` |
| 74 | + |
| 75 | +To fix the lint, you need to add a code example into the documentation block: |
| 76 | + |
| 77 | +```rust |
| 78 | +/// There is no code example! |
| 79 | +/// |
| 80 | +/// ``` |
| 81 | +/// println!("calling no_code_example..."); |
| 82 | +/// no_code_example(); |
| 83 | +/// println!("we called no_code_example!"); |
| 84 | +/// ``` |
| 85 | +pub fn no_code_example() {} |
| 86 | +``` |
| 87 | + |
| 88 | +## private_doc_tests |
| 89 | + |
| 90 | +This lint is **allowed by default**. It detects documentation tests when they |
| 91 | +are on a private item. For example: |
| 92 | + |
| 93 | +```rust |
| 94 | +#![warn(private_doc_tests)] |
| 95 | + |
| 96 | +mod foo { |
| 97 | + /// private doc test |
| 98 | + /// |
| 99 | + /// ``` |
| 100 | + /// assert!(false); |
| 101 | + /// ``` |
| 102 | + fn bar() {} |
| 103 | +} |
| 104 | +# fn main() {} |
| 105 | +``` |
| 106 | + |
| 107 | +Which will give: |
| 108 | + |
| 109 | +```text |
| 110 | +warning: Documentation test in private item |
| 111 | + --> your-crate/lib.rs:4:1 |
| 112 | + | |
| 113 | + 4 | / /// private doc test |
| 114 | + 5 | | /// |
| 115 | + 6 | | /// ``` |
| 116 | + 7 | | /// assert!(false); |
| 117 | + 8 | | /// ``` |
| 118 | + | |___________^ |
| 119 | +``` |
0 commit comments