oxc-project/oxc · error · OxcDiagnostic

Unexpected constant comparison

Error message

Unexpected constant comparison

What it means

The 'impossible' arm of `oxc/const-comparisons`: an `&&` chain compares one variable in opposite directions against two constants that cannot be satisfied together, e.g. `status_code <= 400 && status_code > 500`. Labels read 'Requires that ...' per comparison and the note derives that the expression is false for every value of the variable — a contradiction, hence 'Unexpected constant comparison'.

Source

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

    OxcDiagnostic::warn("Left-hand side of `&&` operator has no effect.")
        .with_help(help)
        .with_labels([
            right_span.label("If this evaluates to `true`"),
            left_span.label("This will always evaluate to true."),
        ])
}

fn redundant_right_hand_side(right_span: Span, left_span: Span, help: String) -> OxcDiagnostic {
    OxcDiagnostic::warn("Right-hand side of `&&` operator has no effect.")
        .with_help(help)
        .with_labels([
            left_span.label("If this evaluates to `true`"),
            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 }
}

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Rewrite the range with ordered bounds: `status_code >= 400 && status_code < 500`
  2. Check whether one side should test a different variable
  3. Delete the contradictory comparison if the whole condition is dead code

Example fix

// before
if (status_code <= 400 && status_code > 500) { /* unreachable */ }

// after
if (status_code >= 400 && status_code < 500) { /* 4xx only */ }
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: `status_code <= 400 && status_code > 500`; `x > 10 && x < 5`; any `<`/`<=` bound whose constant is below the paired `>`/`>=` bound for the same variable inside an `&&`.

Common situations: Editing range bounds (e.g. flipping a 4xx check to `<= 400` and `> 500`); mixing up inclusive/exclusive endpoints; converting `||` chains to `&&` without inverting the comparisons.

Related errors


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