Clippy lints / Nursery
Clippy lint · Nursery · checked with Clippy 0.1.99
clippy::useless_let_if_seq: Checks for variable declarations immediately followed by a conditional affectation
What it meansChecks for variable declarations immediately followed by a conditional affectation. It is in the nursery group and off by default.
Does it matter?
This is not idiomatic Rust.
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 foo;
if bar() {
foo = 42;
} else {
foo = 0;
}
let mut baz = None;
if bar() {
baz = Some(42);
}
Write this instead
let foo = if bar() {
42
} else {
0
};
let baz = if bar() {
Some(42)
} else {
None
};
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]
useless_let_if_seq = "warn"In code
#![warn(clippy::useless_let_if_seq)]One run
cargo clippy -- -W clippy::useless-let-if-seqThe [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 variable declarations immediately followed by a conditional affectation.
Why is this bad?
This is not idiomatic Rust.
Example
let foo;
if bar() {
foo = 42;
} else {
foo = 0;
}
let mut baz = None;
if bar() {
baz = Some(42);
}
should be written
let foo = if bar() {
42
} else {
0
};
let baz = if bar() {
Some(42)
} else {
None
};
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
- unused_peekable`peek` never called on `Peekable` iterator
- unused_roundingused the `ceil` method with a whole number float
- use_selfunnecessary structure name repetition
- volatile_compositestype `main::MyDevice` is not volatile-compatible
- while_floatwhile condition comparing floats
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?