Skip to content

collapsible_if ignore gated out lines between two if #15502

@mockersf

Description

@mockersf

Summary

When something is configured ut between two if, it's ignored by the collapsible_if and it suggest an invalid fix

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=f1a5ce54c09ca095d89a9bd927cd826e

Lint Name

collapsible_if

Reproducer

I tried this code:

fn main() {
    #[allow(unused_mut)]
    let mut inner = false;
    if true {
        #[cfg(false)]
        { inner = true; }
        if inner {
            println!("hello");
        }
    }
}

I saw this happen:

warning: this `if` statement can be collapsed
  --> src/main.rs:4:5
   |
4  | /     if true {
5  | |         #[cfg(false)]
6  | |         { inner = true; }
7  | |         if inner {
...  |
10 | |     }
   | |_____^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_if
   = note: `#[warn(clippy::collapsible_if)]` on by default
help: collapse nested if block
   |
4  ~     if true
5  |         #[cfg(false)]
6  |         { inner = true; }
7  ~         && inner {
8  |             println!("hello");
9  ~         }
   |

I expected to see this happen: no lint

Version

rustc 1.89.0 (29483883e 2025-08-04)
binary: rustc
commit-hash: 29483883eed69d5fb4db01964cdf2af4d86e9cb2
commit-date: 2025-08-04
host: aarch64-apple-darwin
release: 1.89.0
LLVM version: 20.1.7

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveS-fixedStatus: Issues that got fixed, waiting to be closed as completed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions