RS.CLIPPY.BRANCHES_SHARING_CODE

`if` statement with shared code in all blocks

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: branches_sharing_code. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks if the if and else block contain shared code that can be moved out of the blocks.

Why is this bad?

Duplicate code is less maintainable.

Example

let foo = if ... {
    println!("Hello World");
    13
} else {
    println!("Hello World");
    42
};

Use instead:

println!("Hello World");
let foo = if ... {
    13
} else {
    42
};