Both branches of a conditional have the same single-expression body
ID |
scala.correctness.duplicate_branches |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Duplicate Logic |
Language |
Scala |
Description
Reports an if expression whose branches hold the same single expression, so the condition makes no difference to the result. Only single-expression branches are reported.
Rationale
The condition is evaluated and then discarded: whichever way it goes, the same code runs. Either the test is pointless and adds a branch that tests and coverage must carry for nothing, or - far more often - one branch was copied and never edited, so the case it was written for is silently handled with the wrong behaviour.
The following code illustrates the pattern detected by this rule:
class DuplicateBranches {
def pick(flag: Boolean): String = {
// FLAGGED: Both branches of a conditional have the same single-expression body
if (flag) "value" else "value"
}
}