Clippy lints / Nursery
Clippy lint · Nursery · checked with Clippy 0.1.99
clippy::option_if_let_else: Lints usage of if let Some(v) =
What it meansLints usage of if let Some(v) = ... { y } else { x } and match .. { Some(v) => y, None/_ => x } which are more idiomatically done with Option::map_or (if the else bit is a pure expression) or Option::map_or_else (if the else bit is an impure expression). It is in the nursery group and off by default.
Does it matter?
Using the dedicated functions of the Option type is clearer and more concise than an if let expression.
New lints still being finished. Off by default and may have false positives.
How to fix it
Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:
Flagged
let _ = if let Some(foo) = optional {
foo
} else {
5
};
let _ = match optional {
Some(val) => val + 1,
None => 5
};
let _ = if let Some(foo) = optional {
foo
} else {
let y = do_complicated_function();
y*y
};
Write this instead
let _ = optional.map_or(5, |foo| foo);
let _ = optional.map_or(5, |val| val + 1);
let _ = optional.map_or_else(||{
let y = do_complicated_function();
y*y
}, |foo| foo);
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
option_if_let_else = "warn"In code
#![warn(clippy::option_if_let_else)]One run
cargo clippy -- -W clippy::option-if-let-elseThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Lints usage of if let Some(v) = ... { y } else { x } and
match .. { Some(v) => y, None/_ => x } which are more
idiomatically done with Option::map_or (if the else bit is a pure
expression) or Option::map_or_else (if the else bit is an impure
expression).
Why is this bad?
Using the dedicated functions of the Option type is clearer and
more concise than an if let expression.
Notes
This lint uses a deliberately conservative metric for checking if the
inside of either body contains loop control expressions break or
continue (which cannot be used within closures). If these are found,
this lint will not be raised.
Example
let _ = if let Some(foo) = optional {
foo
} else {
5
};
let _ = match optional {
Some(val) => val + 1,
None => 5
};
let _ = if let Some(foo) = optional {
foo
} else {
let y = do_complicated_function();
y*y
};
should be
let _ = optional.map_or(5, |foo| foo);
let _ = optional.map_or(5, |val| val + 1);
let _ = optional.map_or_else(||{
let y = do_complicated_function();
y*y
}, |foo| foo);
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- redundant_cloneChecks for a redundant clone() (and its relatives) which clones an owned value that is going to be dropped without further use
- needless_pass_by_ref_mutthis parameter is a mutable reference but is not used mutably
- needless_type_castChecks for bindings (constants, statics, or let bindings) that are defined with one numeric type but are consistently cast to a different type in all usages
- non_send_fields_in_send_tyThis lint warns about a Send implementation for a type that contains fields that are not safe to be sent across threads. It tries to detect fields that can cause a soundness issue when sent to another thread (e.g., Rc) while allowing !Send fields that are expected to exist in a Send type, such as raw pointers
- or_fun_callChecks for calls to .or(foo(..)), .unwrap_or(foo(..)), .or_insert(foo(..)) etc., and suggests to use .or_else(|| foo(..)), .unwrap_or_else(|| foo(..)), .unwrap_or_default() or .or_default() etc
- path_buf_push_overwritecalling `push` with '/' or '\' (file system root) will overwrite the previous path definition
- read_zero_byte_vecreading zero byte data to `Vec`
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::redundant_cloneChecks for a redundant clone() (and its relatives) which clones an owned value that is going to be dropped without further useRead it next →Did this answer your question?