{"record":{"id":"bb7a4d18565d23d3","repo":"oxc-project/oxc","slug":"unexpected-array-literal-comparison","errorCode":null,"errorMessage":"Unexpected array literal comparison.","messagePattern":"Unexpected array literal comparison\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/oxc/bad_object_literal_comparison.rs","lineNumber":18,"sourceCode":"use oxc_ast::{AstKind, ast::Expression};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\nuse oxc_syntax::operator::BinaryOperator;\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn object_comparison(span: Span, const_result: bool) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected object literal comparison.\")\n        .with_help(format!(\n            \"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.\"\n        ))\n        .with_label(span)\n}\n\nfn array_comparison(span: Span, const_result: bool) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected array literal comparison.\")\n        .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.\"))\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct BadObjectLiteralComparison;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Checks for comparisons between object and array literals.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// Comparing a variable to an object or array literal will always return false as object and array literals are never equal to each other.\n    ///\n    /// If you want to check if an object or array is empty, use `Object.entries()` or `Object.keys()` and their lengths.\n    ///","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/oxc/bad_object_literal_comparison.rs#L1-L36","documentation":"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()`).","triggerScenarios":"`if (arr !== []) {}` (always true); `if (data === []) {}` (always false); `if (typeof item == 'object' && item == []) {}`.","commonSituations":"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.","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"],"exampleFix":"// before\nif (arr !== []) { /* always taken */ }\n\n// after\nif (Array.isArray(arr) && arr.length > 0) { /* ... */ }","handlingStrategy":"type-guard","validationCode":"// .oxlintrc.json — catch it before runtime\n{\n  \"rules\": { \"oxc/bad-object-literal-comparison\": \"error\" }\n}","typeGuard":"const isEmptyArray = (v) => Array.isArray(v) && v.length === 0;\nif (isEmptyArray(arr)) { /* ... */ }","tryCatchPattern":null,"preventionTips":["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"],"tags":["oxlint","oxc","equality","array","javascript","logic-bug"],"backgroundTag":"array-literal-reference-equality","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"}