Branches in the conditional statements should not have the same implementation

  • Code Quality
  • Medium
  • cwe

This rule applies when branches in the conditional statements or expressions have the same implementation.

This might imply the condition is unnecessary, but at worst this is a programmer's mistake. For example, a programmer copies code from a branch into another and he often forgets to modify it.

Therefore, a programmer needs to check the same code is really needed for both.

Note: Not applied for case clauses in switch statement because it is likely to be a programmer's intent.

Noncompliant Code Example

View with compliant examples side by side
// Example 1
if (x >= 0) { // IDENTICAL_BRANCHES alarm
    y = x;
} else {
    y = x;

// Example 2
y = x >= 0 ? x : x; // IDENTICAL_BRANCHES alarm

Compliant Code Example

View with noncompliant examples side by side
// In case when two branches have actually the same implementation
y = x;

// In case when it's like a copy-paste mistake
if (x >= 0) {
    y = x;
} else {
    // Do another thing


This rule was introduced in DeepScan 1.0.0-alpha.