oxc-project/oxc · error · OxcDiagnostic

Checking `switch` discriminant against NaN will never match

Error message

Checking `switch` discriminant against NaN will never match

What it means

With `enforceForSwitch_case: true` (the rule default per `impl Default for UseIsnan`), oxlint's `use-isnan` flags `switch` statements whose discriminant is the literal NaN. Switch matching uses strict equality, and NaN strictly equals nothing, so no `case` can ever match — only `default` runs.

Source

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

};

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!(
        "NaN values will never be found by `Array.prototype.{method_name}`"
    ))
    .with_help("Use the `isNaN` function to check for NaN values.")
    .with_label(span)
}

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace the switch with direct code, or with an `if (Number.isNaN(value))` check when NaN-ness is the real question
  2. Restore the intended discriminant variable if the NaN literal is a typo
  3. As a last resort, configure use-isnan with `{ "enforceForSwitchCase": false }`

Example fix

// before — only the default case can ever run
switch (NaN) {
  case 1:
    logOne();
    break;
  default:
    logOther();
}

// after
logOther();
// or, when testing a runtime value for NaN:
if (Number.isNaN(value)) logOther();
Defensive patterns

Strategy: validation

Validate before calling

// .oxlintrc.json — enforceForSwitchCase is on by default
{ "rules": { "use-isnan": "error" } }
// CI gate: npx oxlint --deny-warnings src/

Prevention

When it happens

Trigger: `switch (NaN) { case 1: ... }` — the SwitchStatement discriminant is the identifier NaN; reported by the switch arm of Rule::run when `enforce_for_switch_case` is enabled (it is on by default).

Common situations: Generated or template-produced switch statements; refactor typos where a variable name was replaced by NaN; leftover debug discriminants that were meant to be a runtime value.

Related errors


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