oxc-project/oxc · error · OxcDiagnostic
Unexpected array literal comparison.
Error message
Unexpected array literal comparison.
What it means
The array-literal variant of `oxc/bad-object-literal-comparison`: an equality comparison (`==`, `===`, `!=`, `!==`) has an empty array literal `[]` as an operand. Array literals are fresh references, so `arr === []` is always false and `arr !== []` always true; the help text reports the constant and suggests `Array.length` if an empty check was intended. Only empty array literals match (`elements.is_empty()`).
Source
Thrown at crates/oxc_linter/src/rules/oxc/bad_object_literal_comparison.rs:18
use oxc_ast::{AstKind, ast::Expression};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use oxc_syntax::operator::BinaryOperator;
use crate::{AstNode, context::LintContext, rule::Rule};
fn object_comparison(span: Span, const_result: bool) -> OxcDiagnostic {
OxcDiagnostic::warn("Unexpected object literal comparison.")
.with_help(format!(
"This comparison will always return {const_result:?} as object literals are never equal to each other. Consider using `Object.entries()` of `Object.keys()` and comparing their lengths."
))
.with_label(span)
}
fn array_comparison(span: Span, const_result: bool) -> OxcDiagnostic {
OxcDiagnostic::warn("Unexpected array literal comparison.")
.with_help(format!("This comparison will always return {const_result:?} as array literals are never equal to each other. Consider using `Array.length` if empty checking was intended."))
.with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct BadObjectLiteralComparison;
declare_oxc_lint!(
/// ### What it does
///
/// Checks for comparisons between object and array literals.
///
/// ### Why is this bad?
///
/// Comparing a variable to an object or array literal will always return false as object and array literals are never equal to each other.
///
/// If you want to check if an object or array is empty, use `Object.entries()` or `Object.keys()` and their lengths.
///View on GitHub (pinned to e1e7af627c)
Solutions
- Use an explicit emptiness check: `Array.isArray(arr) && arr.length === 0`
- If deep content comparison was intended, use `assert.deepStrictEqual` or a deep-equal helper
- If membership was intended, compare lengths plus a Set lookup or `some()` instead of a literal
Example fix
// before
if (arr !== []) { /* always taken */ }
// after
if (Array.isArray(arr) && arr.length > 0) { /* ... */ } Defensive patterns
Strategy: type-guard
Validate before calling
// .oxlintrc.json — catch it before runtime
{
"rules": { "oxc/bad-object-literal-comparison": "error" }
} Type guard
const isEmptyArray = (v) => Array.isArray(v) && v.length === 0;
if (isEmptyArray(arr)) { /* ... */ } Prevention
- Use Array.isArray(...).length checks for empty arrays, never `arr === []`
- Write tests that exercise the empty and non-empty paths of every guard
- Enable the rule in CI
When it happens
Trigger: `if (arr !== []) {}` (always true); `if (data === []) {}` (always false); `if (typeof item == 'object' && item == []) {}`.
Common situations: Empty-array guards written by developers used to value-comparing languages; defensive checks after `filter()` that silently never fire the way they were intended; copy-pasted `if (list !== [])` scaffolding.
Related errors
- Unexpected object literal comparison.
- Invalid character comparison
- Bad comparison sequence
- Math.min and Math.max combination leads to constant result
- Left-hand side of `&&` operator has no effect.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/bb7a4d18565d23d3.
Report an issue: GitHub.