oxc-project/oxc · warning
Do not use `null` comparisons without type-checking operator
Error message
Do not use `null` comparisons without type-checking operators.
What it means
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.
Source
Thrown at crates/oxc_linter/src/rules/eslint/no_eq_null.rs:12
use std::fmt::Debug;
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::BinaryOperator;
use crate::{AstNode, context::LintContext, rule::Rule};
fn no_eq_null_diagnostic(span: Span, suggested_operator: &str) -> OxcDiagnostic {
OxcDiagnostic::warn("Do not use `null` comparisons without type-checking operators.")
.with_help(format!("Use '{suggested_operator}' to compare with null"))
.with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct NoEqNull;
declare_oxc_lint!(
/// ### What it does
///
/// Disallow `null` comparisons without type-checking operators.
///
/// ### Why is this bad?
///
/// Comparing to `null` without a type-checking operator (`==` or `!=`), can
/// have unintended results as the comparison will evaluate to `true` when
/// comparing to not just a `null`, but also an `undefined` value.
///View on GitHub (pinned to e1e7af627c)
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`.
Example fix
// before
if (input == null) {
throw new Error('input required');
}
// after
if (input === null || input === undefined) {
throw new Error('input required');
} Defensive patterns
Strategy: validation
Validate before calling
// find loose null comparisons before lint
const loose = /[=!]==?\s*null|null\s*[=!]==?/.exec(src);
if (loose && loose[0].startsWith('==') === false) { /* strict */ }
if (/[=!]=\s*null|null\s*[=!]=/.test(src)) throw new Error('loose null comparison found'); Type guard
function isAbsent(v: unknown): v is null | undefined {
return v === null || v === undefined;
} Prevention
- 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.
When it happens
Trigger: A binary expression with `BinaryOperator::Eq` or `NotEq` and a `null` literal on either side: `if (user == null) return;` or `while (item != null) { ... }`.
Common situations: 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.
Related errors
- `debugger` statement is not allowed
- Variables should not be deleted
- A regular expression literal can be confused with '/='.
- Duplicate class member: {member_name:?}
- Duplicate conditions in if-else-if chain
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/0559b0b2dbf440cb.
Report an issue: GitHub.