oxc-project/oxc · error

Unexpected constant binary expression

Error message

Unexpected constant binary expression

What it means

Diagnostic from `no-constant-binary-expression`, emitted by `constant_binary_operand` (crates/oxc_linter/src/rules/eslint/no_constant_binary_expression.rs:89). It fires on `==`/`!=`/`===`/`!==` comparisons where one operand is `null`/`undefined`/a static boolean and the other operand has a statically knowable comparison result — e.g. `({}) == true`, `typeof n === true`, `x === true` when x's type family can never be boolean. The comparison outcome is constant, so the check conveys no information (help: 'This compares constantly with the left/right-hand side of the ===').

Source

Thrown at crates/oxc_linter/src/rules/eslint/no_constant_binary_expression.rs:90

    /// ```
    NoConstantBinaryExpression,
    eslint,
    correctness,
    config = NoConstantBinaryExpressionConfig,
    version = "0.0.3",
    short_description = "Disallow expressions where the operation doesn't affect the value.",
);

fn constant_short_circuit(lhs_name: &str, expr_name: &str, span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!(
        "Unexpected constant {lhs_name} on the left-hand side of a {expr_name:?} expression"
    ))
    .with_help("This expression always evaluates to the constant on the left-hand side")
    .with_label(span)
}

fn constant_binary_operand(left_or_right: &str, operator: &str, span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Unexpected constant binary expression")
        .with_help(format!(
            "This compares constantly with the {left_or_right}-hand side of the {operator}"
        ))
        .with_label(span)
}

fn constant_always_new(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Unexpected comparison to newly constructed object")
        .with_help("These two values can never be equal")
        .with_label(span)
}

fn constant_both_always_new(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Unexpected comparison of two newly constructed objects")
        .with_help("These two values can never be equal")
        .with_label(span)
}

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Use truthiness directly instead of comparing: `if (obj)` not `if (obj === true)`.
  2. For null checks keep `x === null` only when x can actually be null/undefined; remove the check when the operand (e.g. an object literal, `typeof` result, arithmetic result) provably cannot be.
  3. For type checks compare `typeof x` with a string: `typeof x === 'boolean'`.
  4. If the constant comparison is intentional documentation, rewrite as the literal it collapses to (`true`/`false`).

Example fix

// before
if (typeof n === true) { /* never runs */ }

// after
if (typeof n === 'boolean') { }
Defensive patterns

Strategy: validation

Validate before calling

// Gate: comparisons of non-boolean expressions against boolean literals
const boolLitCompare = /[^=!<>]={1,2}\s*(true|false)|\b(true|false)\s*={1,2}/.test(src) &&
  !/typeof\s+\w+\s*===?\s*['"]/.test(src);

Type guard

// Express the real intent instead of `=== true`
function isTruthy(v) { return Boolean(v); }
function isType(v, name) { return typeof v === name; }

Prevention

When it happens

Trigger: BinaryExpression with Equality/Inequality where `find_binary_expression_constant_operand` matches: comparing an object/array/function/class/regex/template literal or numeric-like binary expression against `true`/`false`/`null`/`undefined` — e.g. `'hello' === true`, `({}) == null`, `void a === undefined`, `1 === true`, `(a + b) === true`.

Common situations: Truthiness checks mistakenly written as `=== true`; comparing a `typeof` result (always a string) to a boolean; legacy `x == null` checks on expressions that can never be nullish; porting Python-style truth comparisons to JS.

Related errors


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