rust-lang / rust-lang/rust-clippy

FN unnecessary_unwrap does not traverse struct fields

Open
#6,540 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-negative
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Lint name : unnecessary_unwrap

I tried this code:

pub struct S {
    inner: Option<i32>,
}

impl S {
    pub fn fun(&self) -> i32 {
        if self.inner.is_some() {
            return self.inner.unwrap();
        }
        4
    }
}

I expect this to trigger clippy::unnecessary_unwrap and suggest something like

if let Some(val) = self.inner {
    return val;
}
4

but the lint did not trigger 😦

Meta

clippy 0.0.212 (fde6927 2021-01-02)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the unnecessary_unwrap lint and reproduce the Rust example from the issue, checking why access through the struct field is not traversed. Done means the lint recognizes this pattern and suggests the shown if let Some form without regressing existing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.