rust-lang / rust-lang/rust-clippy

`missing_panics_doc` with `check-private-items` should not trigger on tests

Open
#12,265 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

When the missing_panics_doc lint is set to "warn" or "deny" and clippy.toml contains "check-private-items = true", then the lint will warn/deny about assert!(), unwrap(), etc. in #[test] functions that lack "Panics" docs, even though there is no need for test functions to have "Panics" docs.

Lint Name

missing_panics_doc

Reproducer

I tried this code:

pub fn add(left: usize, right: usize) -> usize {
    left + right
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let result = add(2, 2);
        assert_eq!(result, 4);
    }
}

with this [lints] block in Cargo.toml:

[lints.clippy]
missing_panics_doc = "deny"

and this clippy.toml:

check-private-items = true

I saw this happen:

    Checking missing-panic v0.1.0 (/Users/jwodder/work/dev/tmp/missing-panic)
error: docs for function which may panic missing `# Panics` section
  --> src/lib.rs:10:5
   |
10 |     fn it_works() {
   |     ^^^^^^^^^^^^^
   |
note: first possible panic found here
  --> src/lib.rs:12:9
   |
12 |         assert_eq!(result, 4);
   |         ^^^^^^^^^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_panics_doc
   = note: requested on the command line with `-D clippy::missing-panics-doc`

error: could not compile `missing-panic` (lib test) due to 1 previous error

I expected to see this happen: [no lints]

Version
rustc 1.76.0 (07dca489a 2024-02-04)
binary: rustc
commit-hash: 07dca489ac2d933c78d3c5158e3f43beefeb02ce
commit-date: 2024-02-04
host: x86_64-apple-darwin
release: 1.76.0
LLVM version: 17.0.6
Additional Labels

No response

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

Find the implementation and existing tests for the missing_panics_doc lint, then reproduce the report with check-private-items enabled and the provided #[test] example. Add a regression test covering test functions and verify that the lint no longer reports their assertions or unwraps while ordinary functions retain the expected diagnostic.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
testing, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.