E08084: comparing a Boolean with true/false is redundant

Category: E08xxx Pre-IR checks (flow analysis) · Reported in phase: PRE_IR_CHECKS · Classification: REDUNDANT_BOOLEAN_COMPARISON · All error codes

What went wrong

Comparing a Boolean expression with true or false is redundant. Use the expression directly.

Worked examples

Compilable examples demonstrating this error and the correct fix:

  • ek9 -q 639 (Should I compare Boolean values with == true or == false?)
  • ek9 -q 558 (What are tautological conditions and how do I fix them?)

How to fix it

  1. For '== true' or '<> false': use the expression directly
  2. For '== false' or '<> true': use 'not expression'
  3. Comparing two Boolean variables is valid

Common causes

  • Habit from languages where conditions are not inherently Boolean
  • Overly explicit coding style
  • Misunderstanding of Boolean logic

See also

  • E07390: Boolean literal in condition
  • E08085: logical tautology

Examples

Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.

Fails with E08084: comparing a Boolean with true/false is redundant

#!ek9
defines module error.e08084.bad

  defines function

    checkEqualTrue()
      -> ready as Boolean
      <- result as Boolean?

      result: ready == true

    checkNotEqualTrue()
      -> ready as Boolean
      <- result as Boolean?

      result: ready <> true

    checkEqualFalse()
      -> ready as Boolean
      <- result as Boolean?

      result: ready == false

    checkNotEqualFalse()
      -> ready as Boolean
      <- result as Boolean?

      result: ready <> false

    checkReversedTrueEqual()
      -> ready as Boolean
      <- result as Boolean?

      result: true == ready

Fixed: Use a Boolean as it is, or with not: comparing it to true or false adds nothing

#!ek9
defines module error.e08084.good.fix

  defines function

    checkEqualTrue()
      -> ready as Boolean
      <- result as Boolean?

      result: ready

    checkNotEqualTrue()
      -> ready as Boolean
      <- result as Boolean?

      result: not ready

    checkEqualFalse()
      -> ready as Boolean
      <- result as Boolean?

      result: not ready

    checkNotEqualFalse()
      -> ready as Boolean
      <- result as Boolean?

      result: ready

    checkReversedTrueEqual()
      -> ready as Boolean
      <- result as Boolean?

      result: ready

Fixed: Use the Boolean directly, or with not

#!ek9
defines module error.e08084.good

  defines function

    compareTwoBooleans()
      ->
        first as Boolean
        second as Boolean
      <- result as Boolean?

      result: first == second

    useBooleanDirectly()
      -> ready as Boolean
      <- result as Boolean?

      result: ready

    useNegatedBoolean()
      -> ready as Boolean
      <- result as Boolean?

      result: not ready

Questions and answers

These Q&A entries show E08084 and its fix:

Found a problem?

If EK9 reported E08084 for a program you think is valid, or did not report it where it should have, use the compiler form with the smallest program that shows it. If this page is wrong or unclear, use the documentation form. Both are at github.com/ek9lang/ek9-issues.