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

[missing_panics_doc]: trigger on <NonZeroTy>::new(known_to_be_non_zero).unwrap()

Open KisaragiEffective opened this issue 2 years ago • 3 comments
trafficstars

Summary

missing_panics_doc should not trigger on NonZero-type-family constructor whose value is known to be non-zero:

  • for NonZeroNum, integer literal whose value is not zero
  • for Nonnull, references (which is automatically coerced into raw-mut pointer)

Lint Name

missing_panics_doc

Reproducer

I tried this code:

#![deny(clippy::missing_panics_doc)]

use core::num::NonZeroUsize;

struct SourcePos {
    line: NonZeroUsize,
    column: NonZeroUsize,
}

pub fn init() -> SourcePos {
    SourcePos {
        line: NonZeroUsize::new(1).unwrap(),
        column: NonZeroUsize::new(1).unwrap(),
    }
}

use core::ptr::NonNull;

pub fn make_non_null<T>(re: &mut T) -> NonNull<T> {
    NonNull::new(re).unwrap()
}

I saw this happen:

error: docs for function which may panic missing `# Panics` section
  --> src/lib.rs:10:1
   |
10 | pub fn init() -> SourcePos {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: first possible panic found here
  --> src/lib.rs:12:15
   |
12 |         line: NonZeroUsize::new(1).unwrap(),
   |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_panics_doc
note: the lint level is defined here
  --> src/lib.rs:1:9
   |
1  | #![deny(clippy::missing_panics_doc)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^

error: docs for function which may panic missing `# Panics` section
  --> src/lib.rs:19:1
   |
19 | pub fn make_non_null<T>(re: &mut T) -> NonNull<T> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: first possible panic found here
  --> src/lib.rs:20:5
   |
20 |     NonNull::new(re).unwrap()
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_panics_doc

I expected to see nothing happen.

Version

rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: x86_64-unknown-linux-gnu
release: 1.73.0
LLVM version: 17.0.2

Additional Labels

No response

KisaragiEffective avatar Oct 19 '23 15:10 KisaragiEffective