Email me

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-cond

The [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
Correctnessgroup
denydefault level
nofixed by cargo clippy --fix
pre 1.29.0added in Rust

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

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?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ