Skip to content

Allowing clippy::unwrap_used in const contexts #14198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Deewiant opened this issue Feb 11, 2025 · 0 comments · Fixed by #14200
Closed

Allowing clippy::unwrap_used in const contexts #14198

Deewiant opened this issue Feb 11, 2025 · 0 comments · Fixed by #14200
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages

Comments

@Deewiant
Copy link

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

@rustbot rustbot added the C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages label Feb 11, 2025
github-merge-queue bot pushed a commit that referenced this issue Feb 12, 2025
By default, do not lint `.unwrap()` and `.expect(…)` in always const
contexts, as a failure would be detected at compile time anyway.

New options `allow_expect_in_consts` and `allow_unwrap_in_consts`,
defaulting to `true`, can be turned unset to still lint in always const
contexts.

Close #14198

changelog: [`unwrap_used`, `expect_used`]: add new option to lint in
always constant contexts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants