Boolean Compare With Literal

ID

kotlin.boolean_compare_with_literal

Severity

low

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Kotlin

Tags

boolean, readability

Description

Reports == or != expressions where one operand is a true or false literal.

Comparing a boolean expression to a boolean literal is always redundant. x == true is identical to x, and x == false is identical to !x. The literal adds no information and reduces readability.

Rationale

val active: Boolean = isUserActive()

// Bad — literal comparison is redundant
if (active == true) { sendAlert() }   // FLAW
if (active != false) { sendAlert() }  // FLAW

// Good — use the boolean expression directly
if (active) { sendAlert() }
if (active) { sendAlert() }

Remediation

Remove the comparison with the boolean literal:

  • Replace x == true with x.

  • Replace x == false with !x.

  • Replace x != true with !x.

  • Replace x != false with x.