Skip to content

Allowing clippy::unwrap_used in const contexts #14198

@Deewiant

Description

@Deewiant

Description

clippy::unwrap_used and clippy::expect_used lint in const contexts, but that doesn't make much sense, because if the operations fail and panic, rustc will error out regardless. The fact that we are in a const context forms a guarantee in itself that the unwrap or expect call is fine.

See also: #9307


For example:

#![warn(clippy::unwrap_used)]
const X: Option<i32> = Some(42);
pub fn foo() -> i32 {
    const { X.unwrap() }
}

Reports:

warning: used `unwrap()` on an `Option` value
 --> asdf.rs:4:13
  |
4 |     const { X.unwrap() }
  |             ^^^^^^^^^^
  |
  = note: if this value is `None`, it will panic

But this is worthless, because if we change X to None, we get an error from rustc anyway:

error[E0080]: evaluation of `foo::{constant#0}` failed
 --> asdf.rs:4:13
  |
4 |     const { X.unwrap() }
  |             ^^^^^^^^^^ the evaluated program panicked at 'called `Option::unwrap()` on a `None` value', asdf.rs:4:15

Version

rustc 1.84.0 (9fc6b4312 2025-01-07)
binary: rustc
commit-hash: 9fc6b43126469e3858e2fe86cafb4f0fd5068869
commit-date: 2025-01-07
host: x86_64-unknown-linux-gnu
release: 1.84.0
LLVM version: 19.1.5

Additional Labels

@rustbot label +C-enhancement

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