Skip to content
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

unnecessary_unwrap does not consider non-nested control flow #13805

Open
kpreid opened this issue Dec 10, 2024 · 0 comments
Open

unnecessary_unwrap does not consider non-nested control flow #13805

kpreid opened this issue Dec 10, 2024 · 0 comments
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

@kpreid
Copy link
Contributor

kpreid commented Dec 10, 2024

Summary

unnecessary_unwrap lints when the unwrap() is inside an if option.is_some() or if option.is_none(), but does not lint when the condition is an early return and the unwrap() is in following rather than nested code.

Lint Name

unnecessary_unwrap

Reproducer

I tried this code:

pub fn linted1(input: Option<i32>) {
    if input.is_some() {
        std::hint::black_box(input.unwrap());
    }
}
pub fn linted2(input: Option<i32>) {
    if input.is_none() {
    } else {
        std::hint::black_box(input.unwrap());
    }
}

pub fn silent1(input: Option<i32>) {
    if input.is_some() {
    } else {
        return;
    }
    std::hint::black_box(input.unwrap());
}
pub fn silent2(input: Option<i32>) {
    if input.is_none() {
        return;
    }
    std::hint::black_box(input.unwrap());
}

I expected to see this happen: Each of these functions should provoke a warning

Instead, this happened: No warnings on silent1 and silent2

Version

Clippy 0.1.85 (2024-12-09 a224f3807e)
via Rust Playground
@kpreid kpreid 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 Dec 10, 2024
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

No branches or pull requests

1 participant