oxc-project/oxc · error · OxcDiagnostic
Checking equality with NaN will always return false
Error message
Checking equality with NaN will always return false
What it means
The `==`/`===` arm of oxlint's `use-isnan` comparison diagnostic: comparing anything for equality with NaN always yields false (NaN !== NaN by specification), so `x === NaN` guards a branch that can never execute. This arm carries an autofix via `make_equality_fix`.
Source
Thrown at crates/oxc_linter/src/rules/eslint/use_isnan.rs:28
use serde::Deserialize;
use crate::{
AstNode,
context::LintContext,
rule::{DefaultRuleConfig, Rule},
};
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!(View on GitHub (pinned to e1e7af627c)
Solutions
- Use `Number.isNaN(value)` instead of comparing to NaN
- Run `oxlint --fix` to auto-rewrite the comparison into an isNaN call
- Add a unit test asserting the NaN path is actually reachable after the rewrite
Example fix
// before — always false, error handling never runs
if (input === NaN) {
showError();
}
// after
if (Number.isNaN(input)) {
showError();
} Defensive patterns
Strategy: validation
Validate before calling
// .oxlintrc.json
{ "rules": { "use-isnan": "error" } }
// gate: npx oxlint --deny-warnings src/
// grep guard: rg "(===?|!==?)\s*NaN|NaN\s*(===?|!==?)" src/ Prevention
- Treat `=== NaN` as a review-blocking bug; teach Number.isNaN in onboarding
- Keep the rule in the recommended/correctness set for all packages
- Cover numeric parsing helpers with NaN-input tests
- When porting validation code from other languages, re-check every NaN comparison
When it happens
Trigger: Equality comparisons with a literal NaN operand on either side: `value === NaN`, `NaN == result`. Reported from the BinaryExpression visit whenever `is_nan_identifier` matches an operand of an equality operator.
Common situations: Sanity checks after parseFloat/Number conversions (`if (Number(x) === NaN)` is a classic beginner bug); parsing JSON with missing numeric fields; porting formulas from spreadsheets or other languages into JavaScript.
Related errors
- Checking inequality with NaN will always return true
- Comparison with NaN will always return false
- Checking `switch` discriminant against NaN will never match
- Checking for NaN in `case` clause will never match
- NaN values will never be found by `Array.prototype.{method_n
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/b595fc46be221677.
Report an issue: GitHub.