Conditional returns the boolean literals it already tested

ID

scala.correctness.simplify_boolean_expression

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Redundant Code

Language

Scala

Description

Reports an if expression, or a match on a Boolean, whose only job is to return true in one branch and false in the other. The condition itself already is the value.

Rationale

The conditional adds no logic: it re-derives a boolean that the condition has already computed. The extra branching hides the intent behind three lines of control flow, gives coverage tooling two paths where there is one expression, and invites a later edit to change only one branch and silently invert the result.

The following code illustrates the pattern detected by this rule:

class SimplifyBooleanExpression {
  def isPositive(n: Int): Boolean = {
    // FLAGGED: Conditional returns the boolean literals it already tested
    if (n > 0) true else false
  }
}

Remediation

Replace the whole conditional with the condition, or with its negation when the branches are swapped, e.g. if (n > 0) true else false becomes n > 0 and if (n > 0) false else true becomes !(n > 0).

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved