oxc-project/oxc · error · OxcDiagnostic

NaN values will never be found by `Array.prototype.{method_n

Error message

NaN values will never be found by `Array.prototype.{method_name}`

What it means

The `enforceForIndexOf` option (off by default per `impl Default for UseIsnan`) extends use-isnan to array searches: `indexOf`/`lastIndexOf` locate elements via strict equality, so searching for NaN always returns -1. Passing a literal NaN to these methods is reported with the method name interpolated into the message.

Source

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

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

#[derive(Debug, Clone, JsonSchema, Deserialize)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct UseIsnan {
    /// Whether to disallow NaN in switch cases and discriminants
    enforce_for_switch_case: bool,
    /// Whether to disallow NaN as arguments of `indexOf` and `lastIndexOf`
    enforce_for_index_of: bool,
}

impl Default for UseIsnan {
    fn default() -> Self {
        Self { enforce_for_switch_case: true, enforce_for_index_of: false }

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Locate with `arr.findIndex(Number.isNaN)`; test presence with `arr.some(Number.isNaN)`
  2. Filter NaN entries out with `arr.filter((x) => !Number.isNaN(x))` when cleaning arrays
  3. Leave `enforceForIndexOf` off if the -1 behavior is accepted deliberately

Example fix

// before — indexOf uses strict equality, so i is always -1
const i = measurements.indexOf(NaN);
if (i !== -1) measurements.splice(i, 1);

// after
const i = measurements.findIndex(Number.isNaN);
if (i !== -1) measurements.splice(i, 1);
Defensive patterns

Strategy: validation

Validate before calling

// .oxlintrc.json — opt in to indexOf/lastIndexOf checking
{
  "rules": { "use-isnan": ["error", { "enforceForIndexOf": true }] }
}
// CI gate: npx oxlint --deny-warnings src/

Prevention

When it happens

Trigger: `arr.indexOf(NaN)` or `arr.lastIndexOf(NaN)` while use-isnan is configured with `{ "enforceForIndexOf": true }`.

Common situations: Data-cleaning code trying to locate bad sensor/CSV values that parsed to NaN; teams enabling the stricter option when hardening configs; porting tests that assumed indexOf could find NaN.

Related errors


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