Sub topic 13
Enforcing documentation with lints
This chapter assumes you can write /// and //! comments, use intra-doc links, and run cargo doc and cargo test --doc. Here you will make the compiler and rustdoc report missing or broken documentation, so gaps are caught by tooling and not by reviewers.
The missing_docs lint
The compiler lint missing_docs reports public items that have no doc comment. It is set to allow by default, so you must turn it on. Put the attribute at the top of lib.rs:
//! Small utilities for working with numbers.
#![warn(missing_docs)]
/// Adds two numbers.
pub fn add(a: i32, b: i32) -> i32 {
a + b
}
pub fn sub(a: i32, b: i32) -> i32 {
a - b
}cargo build now prints warning: missing documentation for a function for sub. The lint only checks items reachable from outside the crate. Private functions and private modules are skipped. It also checks the crate itself, so a library without a //! comment gets a warning too.
Use #![deny(missing_docs)] if you want the build to fail instead of warn. Many teams use warn locally and turn warnings into errors in CI, which keeps the edit-compile loop quiet while you work.
Rustdoc lints
Rustdoc has its own lints in the rustdoc:: namespace. You set them with the same attributes.
| Lint | Default | Reports |
|---|---|---|
| `rustdoc::broken_intra_doc_links` | warn | A link such as `[Foo]` that resolves to no item |
| `rustdoc::private_intra_doc_links` | warn | A public item linking to a private one |
| `rustdoc::bare_urls` | warn | A URL not wrapped in `<...>` or a Markdown link |
| `rustdoc::invalid_codeblock_attributes` | warn | A misspelled attribute, such as `should_panics` |
| `rustdoc::missing_crate_level_docs` | allow | A crate root with no `//!` documentation |
These lints run during cargo doc, not cargo build. A broken link therefore shows up only when you build the docs. To make the stricter ones mandatory, add them to the crate root:
//! Small utilities for working with numbers.
#![deny(missing_docs)]
#![deny(rustdoc::broken_intra_doc_links)]
#![warn(rustdoc::missing_crate_level_docs)]Clippy checks for conventional sections
Clippy can check that the sections from earlier chapters are present:
clippy::missing_safety_docis on by default. It requires a# Safetysection on every publicunsafe fn.clippy::missing_errors_docrequires an# Errorssection on public functions that returnResult.clippy::missing_panics_docrequires a# Panicssection on public functions that can panic.
The last two belong to the pedantic group, so they are off until you enable them:
#![warn(clippy::missing_errors_doc, clippy::missing_panics_doc)]Run them with cargo clippy.
Turning warnings into errors
Attributes in source fix the policy for everyone. You can also tighten it for one run without editing code:
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps-D warnings makes every rustdoc warning an error, so a single broken link fails the command. This is the usual form for a CI job, which the next chapter covers.
Common mistakes
- Expecting
missing_docsto fire on private items. It does not. - Running only
cargo buildand never seeing rustdoc warnings. Runcargo docas well. - Using
#![deny(warnings)]in a published library. A newer compiler can add lints and break your build for users. Prefer-D warningsin CI.
Exercise
You add #![warn(missing_docs)] to lib.rs, then add a documented pub fn and an undocumented private fn helper(). A doc comment on the public function contains a link [Missing] to an item that does not exist. Which command reports each problem: the missing documentation, and the broken link?
Show answer
Neither problem comes from the private helper, because missing_docs ignores private items. Any undocumented public item is reported by cargo build. The broken link is reported by cargo doc, through rustdoc::broken_intra_doc_links, which warns by default. Run RUSTDOCFLAGS="-D warnings" cargo doc --no-deps to make that warning fail the command.