{"record":{"id":"9d74b99e6b2b1434","repo":"oxc-project/oxc","slug":"checking-inequality-with-nan-will-always-return-tr","errorCode":null,"errorMessage":"Checking inequality with NaN will always return true","messagePattern":"Checking inequality with NaN will always return true","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/eslint/use_isnan.rs","lineNumber":28,"sourceCode":"use serde::Deserialize;\n\nuse crate::{\n    AstNode,\n    context::LintContext,\n    rule::{DefaultRuleConfig, Rule},\n};\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!(","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/use_isnan.rs#L10-L46","documentation":"oxlint's `use-isnan` rule (eslint plugin, correctness category) reports binary expressions where either operand is the bare identifier `NaN`. For the `!=`/`!==` arm of `comparison_with_nan` it emits this message because IEEE-754 NaN compares unequal to every value, including itself, so an inequality against NaN is constantly true and the guarded branch never behaves as written.","triggerScenarios":"Any `!=` or `!==` expression with a literal NaN operand on either side, e.g. `x !== NaN` or `NaN != y`. The AstKind::BinaryExpression arm of Rule::run calls is_nan_identifier on both operands and reports whichever matches.","commonSituations":"Developers porting habits from Java/Python where `x != Double.NaN` looks plausible; copy-pasted input-validation code; refactors that introduce a NaN sentinel value; TypeScript code where the comparison still type-checks.","solutions":["Rewrite the check as `!Number.isNaN(x)` (preferred) or `!isNaN(x)` when string coercion is intended","Run `oxlint --fix`: the rule's conditional fixer rewrites equality-family comparisons with NaN into an isNaN call","Search the codebase for NaN literals near comparison operators to fix every occurrence in one pass"],"exampleFix":"// before — always true, so the branch always runs\nif (value !== NaN) {\n  doWork();\n}\n\n// after\nif (!Number.isNaN(value)) {\n  doWork();\n}","handlingStrategy":"validation","validationCode":"// .oxlintrc.json\n{ \"rules\": { \"use-isnan\": \"error\" } }\n\n// run as a pre-commit gate\n// npx oxlint --deny-warnings src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Review for the Number.isNaN idiom; raw NaN comparisons should never merge","Keep the eslint correctness category enabled in oxlint so use-isnan is always on","Add a grep guard in CI: rg \"(===?|!==?)\\s*NaN|NaN\\s*(===?|!==?)\" src/","Unit-test NaN paths so rewrites stay behaviorally correct"],"tags":["lint","oxlint","eslint","nan","correctness","floating-point","operators"],"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"}