Skip to content

Incorrect clippy::panicking_unwrap on nightly #16188

@JeroenGar

Description

@JeroenGar

Summary

Incorrect "this call to unwrap() will always panic"

Lint Name

clippy::panicking_unwrap

Reproducer

I tried this code:

struct Foo {
    value: Option<i32>,
}

impl Foo {
    pub fn bar(&mut self) {
        let print_value = |v: i32| {
            println!("{}",v);
        };

        if self.value.is_none() {
            self.value = Some(10);
            print_value(self.value.unwrap());
        }
    }
}

pub fn main() {
    let mut a = Foo { value: None };
    a.bar();
}

I saw this happen:

error: this call to `unwrap()` will always panic
  --> src/main.rs:13:25
   |
11 |         if self.value.is_none() {
   |            -------------------- because of this check
12 |             self.value = Some(10);
13 |             print_value(self.value.unwrap());
   |                         ^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#panicking_unwrap
   = note: `#[deny(clippy::panicking_unwrap)]` on by default

I expected to see this happen:

Clippy should not complain as this code compiles and runs fine without panicking:

[test_project] cargo run                                                                                           master  ✱
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.17s
     Running `target/debug/test_project`
10

Version

rustc 1.93.0-nightly (646a3f8c1 2025-12-02)
binary: rustc
commit-hash: 646a3f8c15baefb98dc6e0c1c1ba3356db702d2a
commit-date: 2025-12-02
host: aarch64-apple-darwin
release: 1.93.0-nightly
LLVM version: 21.1.5

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions