Skip to content

unnecessary_unwrap does not consider non-nested control flow #13805

Open
@kpreid

Description

@kpreid

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn't

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions