{"record":{"id":"d5160071cd9ae081","repo":"oxc-project/oxc","slug":"nan-values-will-never-be-found-by-array-prototype","errorCode":null,"errorMessage":"NaN values will never be found by `Array.prototype.{method_name}`","messagePattern":"NaN values will never be found by `Array\\.prototype\\.(.+?)`","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/eslint/use_isnan.rs","lineNumber":46,"sourceCode":"    OxcDiagnostic::warn(msg)\n        .with_help(\"Use the `isNaN` function to compare with NaN.\")\n        .with_label(span)\n}\n\nfn switch_nan(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Checking `switch` discriminant against NaN will never match\")\n        .with_help(\"Use the `isNaN` function instead of the switch.\")\n        .with_label(span)\n}\n\nfn case_nan(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Checking for NaN in `case` clause will never match\")\n        .with_help(\"Use the `isNaN` function instead of the switch.\")\n        .with_label(span)\n}\n\nfn index_of_nan(method_name: &str, span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\n        \"NaN values will never be found by `Array.prototype.{method_name}`\"\n    ))\n    .with_help(\"Use the `isNaN` function to check for NaN values.\")\n    .with_label(span)\n}\n\n#[derive(Debug, Clone, JsonSchema, Deserialize)]\n#[serde(rename_all = \"camelCase\", default, deny_unknown_fields)]\npub struct UseIsnan {\n    /// Whether to disallow NaN in switch cases and discriminants\n    enforce_for_switch_case: bool,\n    /// Whether to disallow NaN as arguments of `indexOf` and `lastIndexOf`\n    enforce_for_index_of: bool,\n}\n\nimpl Default for UseIsnan {\n    fn default() -> Self {\n        Self { enforce_for_switch_case: true, enforce_for_index_of: false }","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/use_isnan.rs#L28-L64","documentation":"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.","triggerScenarios":"`arr.indexOf(NaN)` or `arr.lastIndexOf(NaN)` while use-isnan is configured with `{ \"enforceForIndexOf\": true }`.","commonSituations":"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.","solutions":["Locate with `arr.findIndex(Number.isNaN)`; test presence with `arr.some(Number.isNaN)`","Filter NaN entries out with `arr.filter((x) => !Number.isNaN(x))` when cleaning arrays","Leave `enforceForIndexOf` off if the -1 behavior is accepted deliberately"],"exampleFix":"// before — indexOf uses strict equality, so i is always -1\nconst i = measurements.indexOf(NaN);\nif (i !== -1) measurements.splice(i, 1);\n\n// after\nconst i = measurements.findIndex(Number.isNaN);\nif (i !== -1) measurements.splice(i, 1);","handlingStrategy":"validation","validationCode":"// .oxlintrc.json — opt in to indexOf/lastIndexOf checking\n{\n  \"rules\": { \"use-isnan\": [\"error\", { \"enforceForIndexOf\": true }] }\n}\n// CI gate: npx oxlint --deny-warnings src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remember indexOf/lastIndexOf use strict equality and can never find NaN","Prefer findIndex(Number.isNaN) / some(Number.isNaN) in data-cleaning code","Enable enforceForIndexOf when ingesting external numeric data (CSV, sensors, APIs)","Add tests with NaN in arrays for any search helper"],"tags":["lint","oxlint","eslint","nan","correctness","array","search"],"backgroundTag":"nan-comparison","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}