Why is this an issue?

Having shared code in all branches of an if-statement leads to redundancy, making the code harder to maintain and increasing the risk of errors. It also reduces readability by cluttering the distinct logic of each branch. Extracting shared code into a single location improves maintainability.

How to fix it

To fix this issue, move the shared code outside of the if-statement branches so that it is executed regardless of the condition.

Code examples

Noncompliant code example

let result = if condition {
    println!("Hello World");
    42
} else {
    println!("Hello World");
    24
};

Compliant solution

println!("Hello World");
let result = if condition {
    42
} else {
    24
};

Resources

Documentation