rust-lang / rust-lang/rust-clippy

New lint suggestion: doctest doesn't use documented item

Open
#9,228 0 comments 3 reactions 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

It happened to me a few times that I was copy-pasting a method to make some light changes. I then renamed the method in the pasted code, but forget to change the name in the doctest. I then had a duplicated doctest on an unrelated method.

Similarly, i think the lint should fire if a doctest on a struct S doesn't use S, and on a module m if it doesn't use m.

Lint Name

not sure maybe suspicious_doctest or doctest_doesnt_test

Category

suspicious

Advantage
  • It is confusing when a doctest on S::dec doesn't use S::dec. When the test fails you would look inside dec for the bug, which costs time.
  • When there is a doctest on S::dec, you assume S::dec is tested. (This could be solved with code coverage).
  • When a doctest on S::dec doesn't use S::dec, you should be considering if the test really belongs there. Maybe the module docs would be a better place?
Drawbacks

There could be valid usecases, but at the moment I can't think of any.

I searched through the docs of Vec and Command and couldn't find anything that would violate this proposed lint.

Example
struct S(i8);
impl S {
    /// Increases the contained value
    /// ```
    /// let mut s = S(0);
    /// s.inc();
    /// assert_eq!(s.0, 1);
    /// ```
    fn inc(&mut self) { self.0 += 1; }

    /// Decreases the contained value
    /// ```                                                // Lint should fire here because the test doesn't reference `dec`
    /// let mut s = S(0);
    /// s.inc();                                     
    /// assert_eq!(s.0, 1);
    /// ```
    fn dec(&mut self) { self.0 -= 1; }
}

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

No files, tests, or entry points are named; start by reading the proposed Rust example and defining how doctests are associated with methods, structs, and modules. Done means the lint reliably reports doctests that do not use their documented item, with appropriate handling for valid use cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
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.