{"record":{"id":"0559b0b2dbf440cb","repo":"oxc-project/oxc","slug":"do-not-use-null-comparisons-without-type-checkin","errorCode":null,"errorMessage":"Do not use `null` comparisons without type-checking operators.","messagePattern":"Do not use `null` comparisons without type-checking operators\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/no_eq_null.rs","lineNumber":12,"sourceCode":"use std::fmt::Debug;\n\nuse oxc_ast::AstKind;\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\nuse oxc_syntax::operator::BinaryOperator;\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn no_eq_null_diagnostic(span: Span, suggested_operator: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Do not use `null` comparisons without type-checking operators.\")\n        .with_help(format!(\"Use '{suggested_operator}' to compare with null\"))\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoEqNull;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Disallow `null` comparisons without type-checking operators.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// Comparing to `null` without a type-checking operator (`==` or `!=`), can\n    /// have unintended results as the comparison will evaluate to `true` when\n    /// comparing to not just a `null`, but also an `undefined` value.\n    ///","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_eq_null.rs#L1-L30","documentation":"This diagnostic comes from the `no_eq_null` rule in oxlint. It reports comparisons of a value with `null` that use loose equality, `==` or `!=`. Loose equality with `null` also matches `undefined`, so the test hides which case the code handles. The report suggests the strict operator for the span.","triggerScenarios":"A binary expression with `BinaryOperator::Eq` or `NotEq` and a `null` literal on either side: `if (user == null) return;` or `while (item != null) { ... }`.","commonSituations":"Many teams use `== null` on purpose as the short 'null or undefined' check. A project migrates to oxlint with this rule on, and dozens of lines light up at once. A naive replace with `=== null` changes behavior, because `undefined` stops matching.","solutions":["Write `x === null` / `x !== null` when the code means null only.","Write `x === null || x === undefined` when both must match; this states the intent.","When the idiom is team policy, disable the rule in .oxlintrc.json, or per line with `// oxlint-disable-next-line no-eq-null`."],"exampleFix":"// before\nif (input == null) {\n  throw new Error('input required');\n}\n\n// after\nif (input === null || input === undefined) {\n  throw new Error('input required');\n}","handlingStrategy":"validation","validationCode":"// find loose null comparisons before lint\nconst loose = /[=!]==?\\s*null|null\\s*[=!]==?/.exec(src);\nif (loose && loose[0].startsWith('==') === false) { /* strict */ }\nif (/[=!]=\\s*null|null\\s*[=!]=/.test(src)) throw new Error('loose null comparison found');","typeGuard":"function isAbsent(v: unknown): v is null | undefined {\n  return v === null || v === undefined;\n}","tryCatchPattern":null,"preventionTips":["Pick one null-check idiom in the style guide and write it down.","Enable TypeScript strictNullChecks so the type system drives the check.","Prefer `??` and `?.` over explicit null tests where possible."],"tags":["javascript","eslint","lint","equality","null-handling"],"backgroundTag":"lint-loose-null-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"}