Skip to content

unnecessary_unwrap doesn't work with .as_ref().unwrap() and .as_mut().unwrap() #11371

@jakubdabek

Description

@jakubdabek

Summary

The lint should recognize Option::as_ref and Option::as_mut methods and suggest code with if let, matching on &foo/&mut foo, or alternatively with ref/ref mut in the pattern.

Lint Name

unnecessary_unwrap

Reproducer

I tried this code:

let a = Some(1);
if a.is_some() {
    let x = a.as_ref().unwrap();
    println!("{x}");
}

I expected to see this happen:

The lint would trigger and suggest the following code:

let a = Some(1);
if let Some(x) = &a {
    println!("{x}");
}

Instead, the lint didn't trigger.

Version

0.1.73 (2023-08-20 5c6a7e7) on 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