{"record":{"id":"6ba82a6a2d610ec1","repo":"oxc-project/oxc","slug":"unexpected-object-literal-comparison","errorCode":null,"errorMessage":"Unexpected object literal comparison.","messagePattern":"Unexpected object literal comparison\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/oxc/bad_object_literal_comparison.rs","lineNumber":10,"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    ///","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/oxc/bad_object_literal_comparison.rs#L1-L28","documentation":"Oxlint rule `oxc/bad-object-literal-comparison` fires on an equality comparison (`==`, `===`, `!=`, `!==`) in which one operand is an empty object literal `{}`. Every literal creates a fresh reference, so `x === {}` is always false and `x !== {}` always true; the help text reports that constant result and suggests `Object.entries()`/`Object.keys()` with a length check. The rule only matches empty object literals (the source checks `properties.is_empty()`).","triggerScenarios":"`if (x === {}) {}` (always false); `if (user !== {}) {}` (always true); `if (typeof person != 'object' || person != {}) {}`; any `==`/`===`/`!=`/`!==` comparison with a bare `{}` operand.","commonSituations":"Developers from Python/C# expecting value equality; attempting to test 'is the object empty'; hastily written default-options guards; comparisons auto-generated from schemas.","solutions":["Replace with an emptiness check: `typeof x === 'object' && x !== null && Object.keys(x).length === 0`","If deep content comparison was intended, use `assert.deepStrictEqual`, `lodash.isEqual`, or a custom deep-equal helper","If identity comparison was intended, compare stable identifiers (e.g. `x.id === y.id`) instead of literals"],"exampleFix":"// before\nif (x === {}) { /* never runs */ }\n\n// after\nif (x !== null && typeof x === 'object' && Object.keys(x).length === 0) { /* runs when empty */ }","handlingStrategy":"type-guard","validationCode":"// .oxlintrc.json — catch it before runtime\n{\n  \"rules\": { \"oxc/bad-object-literal-comparison\": \"error\" }\n}","typeGuard":"function isEmptyObject(v) {\n  return typeof v === 'object' && v !== null && !Array.isArray(v) && Object.keys(v).length === 0;\n}\nif (isEmptyObject(x)) { /* ... */ }","tryCatchPattern":null,"preventionTips":["Treat any `{}` operand inside ==/===/!=/!== as an immediate smell","Use Object.keys(...).length === 0 for emptiness and a deep-equal helper for content","Enable the rule in CI so the always-false branch cannot ship"],"tags":["oxlint","oxc","equality","javascript","logic-bug","static-analysis"],"backgroundTag":"object-literal-reference-equality","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}