oxc-project/oxc · error · OxcDiagnostic

Checking equality with NaN will always return false

Error message

Checking equality with NaN will always return false

What it means

The `==`/`===` arm of oxlint's `use-isnan` comparison diagnostic: comparing anything for equality with NaN always yields false (NaN !== NaN by specification), so `x === NaN` guards a branch that can never execute. This arm carries an autofix via `make_equality_fix`.

Source

Thrown at crates/oxc_linter/src/rules/eslint/use_isnan.rs:28

use serde::Deserialize;

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

fn comparison_with_nan(span: Span, operator: BinaryOperator) -> OxcDiagnostic {
    let msg = match operator {
        BinaryOperator::Inequality | BinaryOperator::StrictInequality => {
            "Checking inequality with NaN will always return true"
        }
        BinaryOperator::Equality | BinaryOperator::StrictEquality => {
            "Checking equality with NaN will always return false"
        }
        _ => "Comparison with NaN will always return false",
    };
    OxcDiagnostic::warn(msg)
        .with_help("Use the `isNaN` function to compare with NaN.")
        .with_label(span)
}

fn switch_nan(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Checking `switch` discriminant against NaN will never match")
        .with_help("Use the `isNaN` function instead of the switch.")
        .with_label(span)
}

fn case_nan(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Checking for NaN in `case` clause will never match")
        .with_help("Use the `isNaN` function instead of the switch.")
        .with_label(span)
}

fn index_of_nan(method_name: &str, span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!(

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Use `Number.isNaN(value)` instead of comparing to NaN
  2. Run `oxlint --fix` to auto-rewrite the comparison into an isNaN call
  3. Add a unit test asserting the NaN path is actually reachable after the rewrite

Example fix

// before — always false, error handling never runs
if (input === NaN) {
  showError();
}

// after
if (Number.isNaN(input)) {
  showError();
}
Defensive patterns

Strategy: validation

Validate before calling

// .oxlintrc.json
{ "rules": { "use-isnan": "error" } }
// gate: npx oxlint --deny-warnings src/
// grep guard: rg "(===?|!==?)\s*NaN|NaN\s*(===?|!==?)" src/

Prevention

When it happens

Trigger: Equality comparisons with a literal NaN operand on either side: `value === NaN`, `NaN == result`. Reported from the BinaryExpression visit whenever `is_nan_identifier` matches an operand of an equality operator.

Common situations: Sanity checks after parseFloat/Number conversions (`if (Number(x) === NaN)` is a classic beginner bug); parsing JSON with missing numeric fields; porting formulas from spreadsheets or other languages into JavaScript.

Related errors


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