Branches in the conditional statements should not have the same implementation

  • IDENTICAL_BRANCHES
  • 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

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

// 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
}

Version

This rule was introduced in DeepScan 1.0.0-alpha.

See