Skip to content

incorrect fix for collapsable_if and collapsable_else_if when inner if is wrapped in parens #15303

@folkertdev

Description

@folkertdev

Summary

The collapsable_if and collapsable_else_if lints don't consider that the inner if may be wrapped in parentheses.

Reproducer

I tried this code:

unsafe fn foo() {
    if true {
        (if true {
            ()
        })
    }
}

unsafe fn bar() {
    if true {
        1
    } else {
        (if true { 2 } else { 3 })
    };
}

I expected to see this happen:

With cargo clippy --fix, the resulting code compiles

Instead, this happened:

The collapsable_if and collapsable_else_if fixes don't consider that the inner if may be wrapped in parentheses.

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error: mismatched closing delimiter: `)`
 --> src/main.rs:1:17
  |
1 | unsafe fn foo() {
  |                 ^ unclosed delimiter
...
5 |         })
  |          ^ mismatched closing delimiter

error: unexpected closing delimiter: `}`
 --> src/main.rs:6:1
  |
5 |         })
  |          - missing open `(` for this delimiter
6 | }
  | ^ unexpected closing delimiter

error: aborting due to 2 previous errors

Version

rustc 1.90.0-nightly (e43d139a8 2025-07-09)
binary: rustc
commit-hash: e43d139a82620a268d3828a73e12a8679339e8f8
commit-date: 2025-07-09
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.7

Additional Labels

@rustbot label +I-suggestion-causes-error


I tried looking for some sort of peel_parens function, but could not find any such logic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions