Clippy lints / Nursery
Clippy lint · Nursery · checked with Clippy 0.1.99
clippy::equatable_if_let: Checks for pattern matchings that can be expressed using equality
What it meansChecks for pattern matchings that can be expressed using equality. It is in the nursery group and off by default.
Does it matter?
It reads better and has less cognitive load because equality won’t cause binding. It is a Yoda condition. Yoda conditions are widely criticized for increasing the cognitive load of reading the code. Equality is a simple bool expression and can be merged with && and || and reuse if blocks
New lints still being finished. Off by default and may have false positives.
How to fix it
Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.
Flagged
if let Some(2) = x {
do_thing();
}
Write this instead
if x == Some(2) {
do_thing();
}
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]
equatable_if_let = "warn"In code
#![warn(clippy::equatable_if_let)]One run
cargo clippy -- -W clippy::equatable-if-letThe [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
Checks for pattern matchings that can be expressed using equality.
Why is this bad?
- It reads better and has less cognitive load because equality won’t cause binding.
- It is a Yoda condition. Yoda conditions are widely criticized for increasing the cognitive load of reading the code.
- Equality is a simple bool expression and can be merged with
&&and||and reuse if blocks
Example
if let Some(2) = x {
do_thing();
}
Use instead:
if x == Some(2) {
do_thing();
}
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
- derive_partial_eq_without_eqChecks for types that derive PartialEq and could implement Eq
- doc_link_codecode link adjacent to code text
- empty_enumsChecks for enums with no variants, which therefore are uninhabited types (cannot be instantiated). As of this writing, the never_type is still a nightly-only experimental API. Therefore, this lint is only triggered if #![feature(never_type)] is enabled
- fallible_impl_fromconsider implementing `TryFrom` instead
- future_not_sendfuture cannot be sent between threads safely
- imprecise_flopscube-root of a number can be computed more accurately
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?