Skip to content

unnecessary_unwrap emitted twice in closure #14763

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
samueltardieu opened this issue May 8, 2025 · 1 comment · Fixed by #14770
Closed

unnecessary_unwrap emitted twice in closure #14763

samueltardieu opened this issue May 8, 2025 · 1 comment · Fixed by #14770
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@samueltardieu
Copy link
Contributor

samueltardieu commented May 8, 2025

Summary

The unnecessary_unwrap can be emitted twice in a closure.

Reproducer

I tried this code:

fn f(x: Option<String>) {
    _ = || {
        if x.is_some() {
            _ = x.unwrap();
        }
    };
}

fn main() {
    f(Some(String::new()));
}

and ran clippy-driver t.rs -Z deduplicate-diagnostics=no

I expected to see this happen: one unnecessary_unwrap lint

Instead, this happened:

warning: called `unwrap` on `x` after checking its variant with `is_some`
 --> t.rs:4:17
  |
3 |         if x.is_some() {
  |         -------------- help: try: `if let Some(<item>) = x`
4 |             _ = x.unwrap();
  |                 ^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_unwrap
  = note: `#[warn(clippy::unnecessary_unwrap)]` on by default

warning: called `unwrap` on `x` after checking its variant with `is_some`
 --> t.rs:4:17
  |
3 |         if x.is_some() {
  |         -------------- help: try: `if let Some(<item>) = x`
4 |             _ = x.unwrap();
  |                 ^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_unwrap
  = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`

warning: 2 warnings emitted

Version

rustc 1.88.0-nightly (e9f8103f9 2025-05-07)
binary: rustc
commit-hash: e9f8103f93f8ce2fa2c15c0c6796ec821f8ae15d
commit-date: 2025-05-07
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.4

Additional Labels

No response

@samueltardieu samueltardieu added the C-bug Category: Clippy is not doing the correct thing label May 8, 2025
@relaxcn
Copy link
Contributor

relaxcn commented May 9, 2025

@rustbot claim

github-merge-queue bot pushed a commit that referenced this issue May 10, 2025
The problem is that `check_fn` is triggered by both function and
closure, and `visit_expr` can visit expressions in another closures
within a function or closure.

So just skip walking in a inner closure.

changelog: Fix [`unnecessary_unwrap`] emitted twice in closure which
inside in a function or another closure.

Fixes: #14763
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants