if condition is the literal true or false
ID |
scala.correctness.constant_condition |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Dead Code |
Language |
Scala |
Description
Reports an if whose condition is written as the literal true or false, so the same branch is taken on every run.
Rationale
One branch of the if can never execute and the other is effectively unconditional, so the construct no longer expresses a decision while still reading like one. This is normally a leftover from debugging or a feature toggle that was hard-wired and never restored, and it leaves dead code that the compiler will not warn about and that tests can never cover.
The following code illustrates the pattern detected by this rule:
class ConstantCondition {
def alwaysRuns(): Unit = {
// FLAGGED: if condition is the literal true or false
if (true) {
println("always")
}
}
}