oxc-project/oxc · error

Unexpected comparison to newly constructed object

Error message

Unexpected comparison to newly constructed object

What it means

Diagnostic from `no-constant-binary-expression`, emitted by `constant_always_new` (crates/oxc_linter/src/rules/eslint/no_constant_binary_expression.rs:97). It fires on strict equality/inequality (`===`/`!==`) where either operand is an expression that always yields a fresh reference — object literal, array literal, function/arrow/class expression, regex literal, or `new` of a builtin global (Promise, WeakSet, Boolean...). JS compares objects by reference, so `x === {}` (or `x === new Foo()` for known globals) can never be true; the result is a constant false.

Source

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

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)
}

fn constant_relational_comparison(operator: &str, span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Unexpected constant relational comparison")
        .with_help(format!("Both sides of the {operator} are literal values"))
        .with_label(span)
}

impl Rule for NoConstantBinaryExpression {
    fn from_configuration(value: serde_json::Value) -> Result<Self, serde_json::error::Error> {

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Test the property you actually care about: `x.length === 0`, `Object.keys(x).length === 0`, or `Number.isNaN(x)`.
  2. For identity checks, compare against a stored reference (a previously created object), not a freshly constructed one.
  3. For deep equality use a comparison helper (`isEqual` from lodash, `assert.deepStrictEqual`).
  4. If the always-false comparison is a deliberate sentinel, replace it with the literal `false` so readers are not misled.

Example fix

// before
const isEmpty = x === [];

// after
const isEmpty = Array.isArray(x) && x.length === 0;
Defensive patterns

Strategy: type-guard

Validate before calling

// Gate: strict-equality against freshly constructed references
const freshRefCompare = /={2,3}\s*(\[|\{|=>|function|class\b|\/|new\s+(Promise|WeakSet|Boolean|Map|Set)\b)/.test(src);

Type guard

// Name the check you actually mean
const isEmptyArray = (v) => Array.isArray(v) && v.length === 0;
const isEmptyObject = (v) => v !== null && typeof v === 'object' && Object.keys(v).length === 0;
const isSameRef = (a, b) => a === b; // only meaningful for shared references

Prevention

When it happens

Trigger: BinaryExpression with StrictEquality/StrictInequality where `is_always_new(left)` or `is_always_new(right)` — e.g. `x === []`, `x !== () => {}`, `isEmpty = x === []`, `x === new Promise(...)` when the callee is an ECMAScript global.

Common situations: Developers from value-comparison languages (Python, Java equals) writing `x === []` or `obj === {}` emptiness checks; comparing freshly constructed objects for identity; the classic `x.length === 0` vs `x === []` confusion documented in the rule's doc comment.

Related errors


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