Skip to content

branches-sharing-code suggestion garbage when we actually try to assign the condition result #15004

Open
@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::branches-sharing-code

this code:

#![allow(clippy::if_same_then_else)]

fn main() {
    let a = 12u32;
    let b = 13u32;
    let c = 8u32;

    let result = if b > a { 0 } else { 0 };
}

caused the following diagnostics:

    Checking _a v0.1.0 (/tmp/icemaker_global_tempdir.88vBNDQKmAnP/icemaker_clippyfix_tempdir.mjQOJRXEejmO/_a)
warning: all if blocks contain the same code at the end
 --> src/main.rs:8:40
  |
8 |     let result = if b > a { 0 } else { 0 };
  |                                        ^^^
  |
  = note: the end suggestion probably needs some adjustments to use the expression result correctly
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#branches_sharing_code
  = note: requested on the command line with `--force-warn clippy::branches-sharing-code`
help: consider moving these statements after the if
  |
8 ~     let result = if b > a { 0 } else { }
9 ~     0;
  |

warning: `_a` (bin "_a") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.12s

However after applying these diagnostics, the resulting code:

#![allow(clippy::if_same_then_else)]

fn main() {
    let a = 12u32;
    let b = 13u32;
    let c = 8u32;

    let result = if b > a { 0 } else { };
    0;
}

no longer compiled:

    Checking _a v0.1.0 (/tmp/icemaker_global_tempdir.88vBNDQKmAnP/icemaker_clippyfix_tempdir.mjQOJRXEejmO/_a)
error[E0308]: `if` and `else` have incompatible types
 --> src/main.rs:8:38
  |
8 |     let result = if b > a { 0 } else { };
  |                             -        ^^^ expected integer, found `()`
  |                             |
  |                             expected because of this

For more information about this error, try `rustc --explain E0308`.
error: could not compile `_a` (bin "_a" test) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_a` (bin "_a") due to 1 previous error

Version:

rustc 1.89.0-nightly (9f0e5d963 2025-06-06)
binary: rustc
commit-hash: 9f0e5d963d05ebcf7ae1ca1f4bda3668c702acda
commit-date: 2025-06-06
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

Metadata

Metadata

Assignees

No one assigned

    Labels

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