Class MagicLiteralInComparisonOrError

java.lang.Object
org.ek9lang.compiler.phase5.MagicLiteralInComparisonOrError
All Implemented Interfaces:
Consumer<EK9Parser.ExpressionContext>

final class MagicLiteralInComparisonOrError extends Object implements Consumer<EK9Parser.ExpressionContext>
Detects bare literals used as operands in comparison, relational, matching, and range expressions (E11064).

A literal at a decision point carries no semantic signal — the reader must infer what the value means. The fix is to give it a meaningful name, either as a local variable or a named constant.

Exemptions:

  • All non-numeric literals: Boolean, Character, String, RegEx, Binary, Time, Date, DateTime, Duration, Millisecond, Dimension, Resolution, Colour, Money, Version, Path — these carry semantic meaning in their literal form. String comparisons are self-documenting and covered by E11065 for repeated occurrences
  • Only Integer and Float literals are checked — numeric values at decision points need naming to convey intent (SonarQube S109 alignment)
  • Literals inside constant declarations (the fix itself)
  • Literals inside require/assert statements (test assertions)
  • Literals inside named arguments — the parameter name documents the value
  • Literals inside constructor calls — type construction is data, not logic
  • Integer 0, 1, -1 and Float 0.0, 1.0 (universally understood values)
  • Constructor Details

    • MagicLiteralInComparisonOrError

      MagicLiteralInComparisonOrError(ErrorListener errorListener, Set<org.antlr.v4.runtime.Token> reportedTokens)
  • Method Details

    • accept

      public void accept(EK9Parser.ExpressionContext ctx)
      Specified by:
      accept in interface Consumer<EK9Parser.ExpressionContext>
    • checkForRange

      void checkForRange(EK9Parser.ForRangeContext ctx)
      Check range bounds in a for-range statement (for i in 1 ... 10).

      For-range bounds are NOT checked for magic literals. Range bounds define iteration counts rather than semantic thresholds. Patterns like for i in 1 ... 10 are universally understood and do not benefit from naming. This aligns with SonarQube S109 which exempts loop bounds.