oxc-project/oxc · error · OxcDiagnostic

This comparison will always evaluate to {evaluates_to}

Error message

This comparison will always evaluate to {evaluates_to}

What it means

Fired by `oxc/const-comparisons` when a relational expression (`<`, `<=`, `>`, `>=`) compares an expression with itself: `a < a` is always false and `a >= a` is always true. The message interpolates the constant result; the help explains 'Because `a` will never be less than itself'. When such a self-comparison appears unparenthesized as the right side of `||` or `??`, an extra note explains the precedence trap (comparisons bind tighter than logical operators) and shows the parenthesized form.

Source

Thrown at crates/oxc_linter/src/rules/oxc/const_comparisons.rs:47

            right_span.label("This will always evaluate to true."),
        ])
}

fn impossible(span: Span, span1: Span, x2: &str, x3: &str, x4: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn("Unexpected constant comparison").with_help(x4.to_string()).with_labels([
        span.label(format!("Requires that {x2}")),
        span1.label(format!("Requires that {x3}")),
    ])
}

fn constant_comparison_diagnostic(
    span: Span,
    evaluates_to: bool,
    help: String,
    precedence_note: Option<String>,
) -> OxcDiagnostic {
    let diagnostic =
        OxcDiagnostic::warn(format!("This comparison will always evaluate to {evaluates_to}"))
            .with_help(help)
            .with_label(span);

    if let Some(note) = precedence_note { diagnostic.with_note(note) } else { diagnostic }
}

fn identical_expressions_logical_operator(left_span: Span, right_span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Both sides of the logical operator are the same")
                    .with_help("This logical expression will always evaluate to the same value as the expression itself.")
                    .with_labels([
                        left_span.label("If this expression evaluates to true"),
                        right_span
                            .label("This expression will always evaluate to true"),
                    ])
}

fn equivalent_expressions_logical_operator(left_span: Span, right_span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Both sides of the logical operator are equivalent")

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Compare against the intended other operand: `a < b`
  2. For the precedence case, add parentheses so the logical result is compared: `(a || b) >= c`
  3. Remove the tautological comparison if it was purely accidental

Example fix

// before
if (a < a) { /* never true */ }

// after
if (a < b) { /* ... */ }
Defensive patterns

Strategy: validation

Validate before calling

// .oxlintrc.json — correctness rule (on in the default category set)
{
  "rules": { "oxc/const-comparisons": "error" }
}
// CLI: npx oxlint src/

Prevention

When it happens

Trigger: `if (a < a) {}`; `while (x.id >= x.id) {}`; precedence case `a || b >= b`, which parses as `a || (b >= b)` when `(a || b) >= c` was meant.

Common situations: Copy-paste where the second operand was never changed; mixing `||`/`??` with comparisons without parentheses; auto-generated comparison code from templates.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/98519d4333d071b2. Report an issue: GitHub.