rust-lang / rust-lang/rust-clippy

missing_example_doc to require examples for public items

Open
#14,485 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Clippy has nice lints to remind us to document errors and panics.

As I'm sure you're aware, usage examples are helpful to know how to invoke functions or construct types.

Would it be possible to re-use code from the missing_errors_doc / missing_panics_doc lints to detect missing # Example or # Examples sections in the doc comments for public functions?

This would help library developers highlight areas where we need to add usage examples.

Multiplied across many crates, could this significantly improve Rustdoc?

Advantage
  • Enables Rust developers to enforce usage examples across their codebases
  • Usage examples help other developers know how to use the code
Drawbacks

This could lead to more doctests being run, and therefore it might be nice to include examples of how to write doctest examples with Result in the doctest in the lint documentation, and how to skip doctests where necessary

The lint could be annoying, I'd for sure suggest it ought to be an opt-in only restriction

Example
pub fn add_2_and_2() -> Result<u8, String> {
    2u8 + 2u8
}

Could be written as:

/// # Example
/// ```
/// # use my_crate::add_2_and_2;
/// let four = add_2_and_2()?;
/// assert_eq!(four, 4u8);
/// # Ok::<(), String>(())
/// ```
pub fn add_2_and_2() -> Result<u8, String> {
    2u8 + 2u8
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read the existing missing_errors_doc and missing_panics_doc lints first, since the issue proposes reusing their code. Then define how an opt-in lint should detect # Example or # Examples in public function documentation, and update the lint documentation with doctest guidance and completion criteria.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.