-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Closed
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedIssue: The suggestions provided by this Lint cause an ICE/error when applied
Description
Summary
The unneccessary_unwrap
lint should not be triggered when the preceding is_err(), is_ok(), or is_some() call is in a combined expression, at least not if the code is at Rust edition 2021 or older.
Lint Name
unnecessary_unwrap
Reproducer
I tried this code:
if res.is_err() && !std::thread::panicking() {
panic!("Failed to remove flock: {}", res.unwrap_err());
}
I saw this happen:
error: called `unwrap_err` on `res` after checking its variant with `is_err`
--> src/fcntl.rs:1045:50
|
1044 | if res.is_err() && !std::thread::panicking() {
| ------------ the check is happening here
1045 | panic!("Failed to remove flock: {}", res.unwrap_err());
| ^^^^^^^^^^^^^^^^
|
= help: try using `if let` or `match`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_unwrap
= note: `-D clippy::unnecessary-unwrap` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_unwrap)]`
I expected to see this happen:
No warning should be emitted. Because if I follow Clippy's advice and replace the is_err()
call with if let
, then the compile fails with this error:
error: let chains are only allowed in Rust 2024 or later
--> src/fcntl.rs:1044:12
|
1044 | if let Err(e) = res && !std::thread::panicking() {
| ^^^^^^^^^^^^^^^^
Version
rustc 1.92.0-nightly (9f32ccf35 2025-09-21)
binary: rustc
commit-hash: 9f32ccf35fb877270bc44a86a126440f04d676d0
commit-date: 2025-09-21
host: x86_64-unknown-freebsd
release: 1.92.0-nightly
LLVM version: 21.1.1
Additional Labels
@rustbot label +I-suggestion-causes-error
Metadata
Metadata
Assignees
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedIssue: The suggestions provided by this Lint cause an ICE/error when applied