rust-lang / rust-lang/rust-clippy

clippy::option_if_let_else suggests hard code

Open
#8,829 11 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

I guess clippy::option_if_let_else should not be suggested here, because the branch yields an Output variant, and not a string slice as suggested.

Lint Name

clippy::option_if_let_else

Reproducer

I tried this code in rust playground:

#![deny(clippy::option_if_let_else)]

fn main() {
    println!("{:?}", run("ok:yay"));
    println!("{:?}", run("ko:there was an error"));
    println!("{:?}", run("crash:oh no, it crashed"));
}


#[derive(Debug)]
pub enum Output {
    Success(String, String),
    JobError(String, String),
    ProcessError(String),
}
fn run(job: &str) -> Output {
    if let Some(stripped) = job.strip_prefix("ok:") {
        Output::Success(stripped.into(), "".into())
    } else if let Some(stripped) = job.strip_prefix("ko:") {
        Output::JobError(stripped.into(), "".into())
    } else if let Some(stripped) = job.strip_prefix("crash:") {
        Output::ProcessError(stripped.into())
    } else {
        unreachable!(
            "Job should begin with ok:, ko, or crash: (actual: '{}')",
            job
        )
    }
}

I saw this happen:

error: use Option::map_or_else instead of an if let/else
   --> src/ci/job/schedule.rs:176:13
    |
176 | /             if let Some(stripped) = job.strip_prefix("ok:") {
177 | |                 Output::Success(stripped.into(), "".into())
178 | |             } else if let Some(stripped) = job.strip_prefix("ko:") {
179 | |                 Output::JobError(stripped.into(), "".into())
...   |
186 | |                 )
187 | |             }
    | |_____________^
    |
note: the lint level is defined here
   --> src/main.rs:3:9
    |
3   | #![deny(clippy::nursery)]
    |         ^^^^^^^^^^^^^^^
    = note: `#[deny(clippy::option_if_let_else)]` implied by `#[deny(clippy::nursery)]`
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#option_if_let_else
help: try
    |
176 ~             job.strip_prefix("ok:").map_or_else(|| if let Some(stripped) = job.strip_prefix("ko:") {
177 +                 Output::JobError(stripped.into(), "".into())
178 +             } else if let Some(stripped) = job.strip_prefix("crash:") {
179 +                 Output::ProcessError(stripped.into())
180 +             } else {
181 +                 unreachable!(
  ...

I expected to see this happen:
???

Version
cargo clippy --version
clippy 0.1.62 (88860d5 2022-05-09)
$ rustc -Vv
rustc 1.62.0-nightly (88860d547 2022-05-09)
binary: rustc
commit-hash: 88860d5474a32f507dde8fba8df35fd2064f11b9
commit-date: 2022-05-09
host: x86_64-unknown-linux-gnu
release: 1.62.0-nightly
LLVM version: 14.0.1
Additional Labels

No response

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

Locate the implementation and tests for clippy::option_if_let_else, then reproduce the reported suggestion with the Rust Playground example. Check why the lint suggests map_or_else for the chained branches returning Output variants, and add regression coverage showing that this case is not incorrectly suggested.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.