Skip to content

unnecessary_unwrap doesn't lint following a binding name #14725

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
amab8901 opened this issue May 2, 2025 · 3 comments · Fixed by #14758
Closed

unnecessary_unwrap doesn't lint following a binding name #14725

amab8901 opened this issue May 2, 2025 · 3 comments · Fixed by #14758
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@amab8901
Copy link

amab8901 commented May 2, 2025

Summary

warn if there's a missed opportunity to make the code more clean and avoid an unnecessary unwrap. Even if you're using unwraps as a temporary placeholder before adding a full option handling later, you can't use this as an excuse to write your code the way that the propsed lint warns against because the concision level is superior with the approach I expected

Lint Name

unnecessary_unwrap

Reproducer

I tried this code:

   if option.is_some() {
                let content = option.unwrap();

                do_something(content);
            }

I expected to see this happen:

give warning that suggests rewriting to this;

   if let Some(content) = option {
                do_something(content);
            }

Instead, this happened:

silence

Version

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-unknown-linux-gnu
release: 1.86.0
LLVM version: 19.1.7
@amab8901 amab8901 added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels May 2, 2025
@Jarcho
Copy link
Contributor

Jarcho commented May 2, 2025

This already exists as unnecessary_unwrap.

@Jarcho Jarcho closed this as completed May 2, 2025
@Alexendoo
Copy link
Member

Seems like doesn't fire if it's part of or comes after a binding name

pub fn f(o: Option<String>) {
    if o.is_some() {
        let x = o.unwrap();
    }
}
pub fn f(o: Option<String>) {
    if o.is_some() {
        let y = 1;
        o.unwrap();
    }
}

@Alexendoo Alexendoo reopened this May 2, 2025
@Alexendoo Alexendoo changed the title is_some_unwrap unnecessary_unwrap doesn't lint following a binding name May 2, 2025
@relaxcn
Copy link
Contributor

relaxcn commented May 8, 2025

@rustbot claim

github-merge-queue bot pushed a commit that referenced this issue May 8, 2025
changelog: Fix [`unnecessary_unwrap`] false negative when any assignment
occurs in `if` branch (regardless of any variable).

Fixes: #14725
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 I-false-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants