Skip to content

unnecessary_unwrap: should not trigger if the is_some is just part of the conditional #4530

Open

Description

@nrc

unnecessary_unwrap recommends that foo.is_some() in a conditional followed by foo.unwrap() should be replaced by an if let. That is usually good advice, however, if the is_some is just part of the conditional, then the only way to use if let is with nested ifs.

E.g.,

if some_condition && foo.is_some() {
    let foo = foo.unwrap();
    ...
}

can only be rewritten as

if some_condition {
    if let Some(foo) = foo {
        ...
    }
}

which is not much of an improvement (and arguably no improvement at all). (In this case, using match might be better, but in some more complex examples it is not possible).

I would much prefer is unnecessary_unwrap only triggered when it was possible to rewrite exactly using a single if let or match.

Activity

  1. added
    L-suggestionLint: Improving, adding or fixing lint suggestions
    C-enhancementCategory: Enhancement of lints, like adding more cases or adding help messages
    on Sep 10, 2019
  2. oli-obk commented on Sep 10, 2019

    @oli-obk
    Contributor

    Since chaining if lets in a condition already works on nightly, we can just wait until that hits stable and suggest the chained condition instead.

    But until then this seems like an OK fix

  3. matthiaskrgr commented on Mar 21, 2020

    @matthiaskrgr
    Member

    triage: it seems the let_chains feature has not been stabilized yet.

  4. added a commit that references this issue on Apr 26, 2021
  5. yerke commented on Jul 22, 2022

    @yerke
    Contributor

    let_chains is riding the train to stable and is expected to be in Rust 1.64 🚀 : rust-lang/rust#94927

  6. DanielT commented on Aug 22, 2023

    @DanielT

    I just ran into this issue.
    let-chainsgot reverted from Rust 1.64 and as far as I can tell there is no timeline for stabilization

    Since let chains have not landed in 4 years, could this be fixed in clippy after all?

  7. added a commit that references this issue on Jan 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesL-suggestionLint: Improving, adding or fixing lint suggestions

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions