rust-lang / rust-lang/rust-clippy

Disagreement with missing_panics_doc changes regarding `expect`

Open
#11,436 12 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

In #10953, missing_panics_doc was updated to include reporting on Option::expect and Result::expect. This has caused a large number of warnings across my projects, and I wanted to start a discussion over the changes.

To me, these two approaches to asserting that an option shouldn't be None are identical from a programmer's intent:

pub fn example(opt: Option<()>) {
    match opt {
        Some(_) => {}
        None => unreachable!("invalid"),
    }

    opt.expect("invalid")
}

Despite the first match statement possibly panicking, no warning is given. Yet, with the new behavior, opt.expect() now warns about possible panics. To me, my intention as a programmer between these two approaches is identical.

The net result of this change is that I'm going to be forced into an ugly pattern in my code to avoid this false positive. Could we split the warnings for panicking on expect into its own lint so that we can keep the previous behavior?

Lint Name

missing_panics_doc

Reproducer

I tried this code:

pub fn example(opt: Option<()>) {
    opt.expect("invalid")
}

I saw this happen:

warning: docs for function which may panic missing `# Panics` section
   --> src/shapes.rs:442:5
    |
442 |     pub fn test(i: Option<()>) {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
note: first possible panic found here
   --> src/shapes.rs:448:9
    |
448 |         i.expect("invalid");
    |         ^^^^^^^^^^^^^^^^^^^
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_panics_doc

I expected to see this happen: No warnings

Version
rustc 1.72.0 (5680fa18f 2023-08-23)
binary: rustc
commit-hash: 5680fa18feaa87f3ff04063800aec256c3d4b4be
commit-date: 2023-08-23
host: x86_64-unknown-linux-gnu
release: 1.72.0
LLVM version: 16.0.5
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 by reading the missing_panics_doc lint and its handling of Option::expect and Result::expect, then compare it with the provided match and unreachable! reproducer. Review the discussion around issue #10953; done requires resolving whether expect warnings should remain part of this lint or be separated, with corresponding behavior and tests.

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
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.