Skip to content

Disagreement with missing_panics_doc changes regarding expect #11436

Open
@ecton

Description

@ecton

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: Enhancement of lints, like adding more cases or adding help messages

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions