-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Open
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn'tIssue: The lint should have been triggered on code, but wasn't
Description
Summary
If an Option variable tested with if + is_none before unwrap is local, the lint unnecessary_unwrap
is reported. But if the variable is nested in a struct, nothing is reported.
Lint Name
unnecessary_unwrap
Reproducer
I tried this code:
struct Soption {
option: Option<bool>,
}
let sopt = Soption { option: Some(true) };
let _res = if sopt.option.is_none() {
false // default
} else {
sopt.option.unwrap()
};
I expected to see this happen:
warning: called `unwrap` on `option` after checking its variant with `is_none`
--> src/clippy_unwrap_or.rs:7:9
|
4 | let _res = if sopt.option.is_none() {
| ------------------- help: try: `if let Some(<item>) = option`
...
7 | sopt.option.unwrap()
| ^^^^^^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_unwrap
Instead, this happened:
None
while if the option var is local, a report is provided :
let opt = Some(true);
let _res = if opt.is_none() {
false // default
} else {
opt.unwrap()
};
warning: used `unwrap()` on `Some` value
--> src/clippy_unwrap_or.rs:7:9
|
7 | option.unwrap()
| ^^^^^^^^^^^^^^^
|
help: remove the `Some` and `unwrap()`
--> src/clippy_unwrap_or.rs:2:18
|
2 | let option = Some(true);
| ^^^^^^^^^^
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_literal_unwrap
= note: `#[warn(clippy::unnecessary_literal_unwrap)]` on by default
Version
rustc 1.88.0 (6b00bc388 2025-06-23)
binary: rustc
commit-hash: 6b00bc3880198600130e1cf62b8f8a93494488cc
commit-date: 2025-06-23
host: x86_64-unknown-linux-gnu
release: 1.88.0
LLVM version: 20.1.5
Metadata
Metadata
Assignees
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn'tIssue: The lint should have been triggered on code, but wasn't