rust-lang / rust-lang/rust-clippy

clippy --fix uses let chains and breaks existing code, when `#[cfg(feature...)]` is used

Open
#16,191 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-suggestion-causes-bug
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

I used cargo clippy --fix and unittests failed as a result, because the code didn't do the same as before.

Reproducer

Cargo.toml:

[package]
name = "testcase"
edition = "2024"

[features]
bundle-translations = []

src/main.rs:

fn main() {
    let f = 1;
    let g = Some(1);
    let _h = Some(2);
    let mut ret = 0;
    // [...]
    // Nothing in this if statement should execute
    if f == 0 {
        if let Some(v) = g {
            ret = v;
        }
        #[cfg(feature = "bundle-translations")]
        if let Some(v) = _h {
            ret = v;
        }
    }
    assert!(ret == 0);
}  

cargo build --all-features ; cargo run, the assert passes.

clippy --fix changes this to

    // Nothing in this if statement should execute
    if f == 0
        && let Some(v) = g {
            ret = v;
        }
        #[cfg(feature = "bundle-translations")]
        if let Some(v) = _h {
            ret = v;
        }
    assert!(ret == 0);

Which means that when building the code with the bundle-translations feature enabled, the second if() will be evaluated, unlike before, and ret will be 2 instead of 0.

Pretty sure clippy got confused by the fact that the code was not enabled when it ran (the feature is off by default), but changing behavior when the feature is set is surely a --fix bug.

Version
rustc 1.91.1 (ed61e7d7e 2025-11-07)
binary: rustc
commit-hash: ed61e7d7e242494fb7057f2657300d9e77bb4fcb
commit-date: 2025-11-07
host: x86_64-unknown-linux-gnu
release: 1.91.1
LLVM version: 21.1.2
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reported Cargo.toml and src/main.rs reproducer, then run cargo build --all-features, cargo run, and cargo clippy --fix with the feature disabled and enabled. Check that the transformed code preserves the original behavior in both feature configurations; done means --fix no longer changes control flow across #[cfg] boundaries.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.