rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

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

Open jwodder opened this issue 1 year ago • 1 comments

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

jwodder avatar Feb 10 '24 16:02 jwodder

Seems not an issue on version 0.1.80

alex-semenyuk avatar Aug 16 '24 16:08 alex-semenyuk

This is still happening with Rust 1.83.0. Note that you have to run cargo clippy --all-targets, not just cargo clippy, in order for clippy to lint the test function.

jwodder avatar Dec 20 '24 20:12 jwodder