Clippy lints / Correctness
Clippy lint · Correctness · checked with Clippy 0.1.99
clippy::ifs_same_cond: Checks for consecutive ifs with the same condition
What it meansChecks for consecutive ifs with the same condition. It is in the correctness group and denied by default (the build fails).
Does it matter?
This is probably a copy & paste error.
Code that is almost certainly wrong. Denied by default: the build fails until you fix it or allow it.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
if a == b {
…
} else if a == b {
…
}
Write this instead
if foo() {
…
} else if foo() { // not linted
…
}
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::ifs_same_cond)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
ifs_same_cond = "allow"One run
cargo clippy -- -A clippy::ifs-same-condThe [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.
Settings in clippy.toml
ignore-interior-mutability: A list of paths to types that should be treated as if they do not contain interior mutability
Clippy's own documentation
What it does
Checks for consecutive ifs with the same condition.
Why is this bad?
This is probably a copy & paste error.
Example
if a == b {
…
} else if a == b {
…
}
Note that this lint ignores all conditions with a function call as it could have side effects:
if foo() {
…
} else if foo() { // not linted
…
}
Configuration
-
ignore-interior-mutability: A list of paths to types that should be treated as if they do not contain interior mutability(default:
["bytes::Bytes"])
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- eq_opChecks for equal operands to comparison, logical and bitwise, difference and division binary operators (==, >, etc., &&, ||, &, |, ^, - and /)
- erasing_opthis operation will always return zero. This is likely not the intended outcome
- if_let_mutexChecks for Mutex::lock calls in if let expression with lock calls in any of the else blocks
- impl_hash_borrow_with_str_and_bytesthe semantics of `Borrow<T>` around `Hash` can't be satisfied when both `Borrow<str>` and `Borrow<[u8]>` are implemented
- impossible_comparisonsChecks for double comparisons that can never succeed
- ineffective_bit_maskChecks for bit masks in comparisons which can be removed without changing the outcome
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::eq_opChecks for equal operands to comparison, logical and bitwise, difference and division binary operators (==, >, etc., &&, ||, &, |, ^, - and /)Read it next →Did this answer your question?