Open
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