oxc-project/oxc · warning

Do not use the {operator} operator to compare against -0.

Error message

Do not use the {operator} operator to compare against -0.

What it means

Diagnostic from the oxlint rule `no-compare-neg-zero` (crates/oxc_linter/src/rules/eslint/no_compare_neg_zero.rs). It fires when a relational or equality operator (`>`, `<`, `>=`, `<=`, `==`, `===`) has `-0` (a UnaryExpression negating the literal 0) as an operand. Signed zero compares equal to `+0` under these operators, so the comparison silently cannot distinguish -0; only `Object.is(x, -0)` can.

Source

Thrown at crates/oxc_linter/src/rules/eslint/no_compare_neg_zero.rs:10

use oxc_ast::{AstKind, ast::Expression};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::{BinaryOperator, UnaryOperator};

use crate::{AstNode, context::LintContext, rule::Rule};

fn no_compare_neg_zero_diagnostic(operator: &str, span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("Do not use the {operator} operator to compare against -0."))
        .with_help("Use Object.is(x, -0) to test equality with -0 and use 0 for other cases")
        .with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct NoCompareNegZero;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallow comparing against `-0`
    ///
    /// ### Why is this bad?
    ///
    /// The rule should warn against code that tries to compare against `-0`,
    /// since that will not work as intended. That is, code like `x === -0` will
    /// pass for both `+0` and `-0`. The author probably intended `Object.is(x, -0)`.
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Use `Object.is(x, -0)` when you specifically need to detect negative zero.
  2. Use plain `0` when sign-of-zero does not matter (the comparison result is identical anyway).
  3. If distinguishing sign matters more broadly, track the sign separately (e.g. `Math.sign`) instead of comparing against -0.
  4. Suppress inline with `// oxlint-disable-next-line no-compare-neg-zero` for deliberate -0 tests (e.g. in spec-compliance test suites).

Example fix

// before
if (x === -0) { /* ... */ }

// after
if (Object.is(x, -0)) { /* ... */ }
Defensive patterns

Strategy: type-guard

Validate before calling

// Reject -0 comparisons at author time with a lint gate
const negZeroCompare = /[<>=!]==?\s*-\s*0\b|(-\s*0)\s*[<>=]=?/.test(src);

Type guard

// The only correct discriminator for negative zero
function isNegativeZero(v) {
  return Object.is(v, -0);
}

Prevention

When it happens

Trigger: BinaryExpression with a comparison operator where one side is `-0` — e.g. `if (x === -0)`, `x >= -0`. The message interpolates the concrete operator, e.g. 'Do not use the === operator to compare against -0.'

Common situations: Math-heavy code (Math.round, Math.sin results that can be -0); chart/graphics code checking for negative zero after divisions like `-1/Infinity`; developers porting `Object.is` semantics from tests into comparisons.

Related errors


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