{"record":{"id":"bdb7bb3d828c6649","repo":"oxc-project/oxc","slug":"checking-switch-discriminant-against-nan-will-ne","errorCode":null,"errorMessage":"Checking `switch` discriminant against NaN will never match","messagePattern":"Checking `switch` discriminant against NaN will never match","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/eslint/use_isnan.rs","lineNumber":34,"sourceCode":"};\n\nfn comparison_with_nan(span: Span, operator: BinaryOperator) -> OxcDiagnostic {\n    let msg = match operator {\n        BinaryOperator::Inequality | BinaryOperator::StrictInequality => {\n            \"Checking inequality with NaN will always return true\"\n        }\n        BinaryOperator::Equality | BinaryOperator::StrictEquality => {\n            \"Checking equality with NaN will always return false\"\n        }\n        _ => \"Comparison with NaN will always return false\",\n    };\n    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","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/use_isnan.rs#L16-L52","documentation":"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.","triggerScenarios":"`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).","commonSituations":"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.","solutions":["Replace the switch with direct code, or with an `if (Number.isNaN(value))` check when NaN-ness is the real question","Restore the intended discriminant variable if the NaN literal is a typo","As a last resort, configure use-isnan with `{ \"enforceForSwitchCase\": false }`"],"exampleFix":"// before — only the default case can ever run\nswitch (NaN) {\n  case 1:\n    logOne();\n    break;\n  default:\n    logOther();\n}\n\n// after\nlogOther();\n// or, when testing a runtime value for NaN:\nif (Number.isNaN(value)) logOther();","handlingStrategy":"validation","validationCode":"// .oxlintrc.json — enforceForSwitchCase is on by default\n{ \"rules\": { \"use-isnan\": \"error\" } }\n// CI gate: npx oxlint --deny-warnings src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never use NaN as a switch discriminant — hoist Number.isNaN checks before the switch","Code-review generated switch statements, which produce this pattern most often","Keep the eslint correctness category enabled in oxlint","Unit-test switch routing with NaN inputs"],"tags":["lint","oxlint","eslint","nan","correctness","switch","control-flow"],"backgroundTag":"nan-comparison","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}