rust-lang / rust-lang/rust-clippy

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

Open
#11,684 3 comments 0 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

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

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

Start with the missing_panics_doc lint implementation and reproduce the supplied Rust examples for NonZeroUsize and NonNull. Done means known-nonzero constructors no longer trigger the missing # Panics diagnostic, while genuinely fallible uses still do.

Written by the indexing model from the issue text.

Assessment

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