{"record":{"id":"2053315399e40aab","repo":"oxc-project/oxc","slug":"unexpected-constant-binary-expression","errorCode":null,"errorMessage":"Unexpected constant binary expression","messagePattern":"Unexpected constant binary expression","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/eslint/no_constant_binary_expression.rs","lineNumber":90,"sourceCode":"    /// ```\n    NoConstantBinaryExpression,\n    eslint,\n    correctness,\n    config = NoConstantBinaryExpressionConfig,\n    version = \"0.0.3\",\n    short_description = \"Disallow expressions where the operation doesn't affect the value.\",\n);\n\nfn constant_short_circuit(lhs_name: &str, expr_name: &str, span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\n        \"Unexpected constant {lhs_name} on the left-hand side of a {expr_name:?} expression\"\n    ))\n    .with_help(\"This expression always evaluates to the constant on the left-hand side\")\n    .with_label(span)\n}\n\nfn constant_binary_operand(left_or_right: &str, operator: &str, span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected constant binary expression\")\n        .with_help(format!(\n            \"This compares constantly with the {left_or_right}-hand side of the {operator}\"\n        ))\n        .with_label(span)\n}\n\nfn constant_always_new(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected comparison to newly constructed object\")\n        .with_help(\"These two values can never be equal\")\n        .with_label(span)\n}\n\nfn constant_both_always_new(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected comparison of two newly constructed objects\")\n        .with_help(\"These two values can never be equal\")\n        .with_label(span)\n}\n","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_constant_binary_expression.rs#L72-L108","documentation":"Diagnostic from `no-constant-binary-expression`, emitted by `constant_binary_operand` (crates/oxc_linter/src/rules/eslint/no_constant_binary_expression.rs:89). It fires on `==`/`!=`/`===`/`!==` comparisons where one operand is `null`/`undefined`/a static boolean and the other operand has a statically knowable comparison result — e.g. `({}) == true`, `typeof n === true`, `x === true` when x's type family can never be boolean. The comparison outcome is constant, so the check conveys no information (help: 'This compares constantly with the left/right-hand side of the ===').","triggerScenarios":"BinaryExpression with Equality/Inequality where `find_binary_expression_constant_operand` matches: comparing an object/array/function/class/regex/template literal or numeric-like binary expression against `true`/`false`/`null`/`undefined` — e.g. `'hello' === true`, `({}) == null`, `void a === undefined`, `1 === true`, `(a + b) === true`.","commonSituations":"Truthiness checks mistakenly written as `=== true`; comparing a `typeof` result (always a string) to a boolean; legacy `x == null` checks on expressions that can never be nullish; porting Python-style truth comparisons to JS.","solutions":["Use truthiness directly instead of comparing: `if (obj)` not `if (obj === true)`.","For null checks keep `x === null` only when x can actually be null/undefined; remove the check when the operand (e.g. an object literal, `typeof` result, arithmetic result) provably cannot be.","For type checks compare `typeof x` with a string: `typeof x === 'boolean'`.","If the constant comparison is intentional documentation, rewrite as the literal it collapses to (`true`/`false`)."],"exampleFix":"// before\nif (typeof n === true) { /* never runs */ }\n\n// after\nif (typeof n === 'boolean') { }","handlingStrategy":"validation","validationCode":"// Gate: comparisons of non-boolean expressions against boolean literals\nconst boolLitCompare = /[^=!<>]={1,2}\\s*(true|false)|\\b(true|false)\\s*={1,2}/.test(src) &&\n  !/typeof\\s+\\w+\\s*===?\\s*['\"]/.test(src);","typeGuard":"// Express the real intent instead of `=== true`\nfunction isTruthy(v) { return Boolean(v); }\nfunction isType(v, name) { return typeof v === name; }","tryCatchPattern":null,"preventionTips":["Use truthiness (`if (x)`) or typeof-string comparisons, never `x === true` on non-booleans.","Remember typeof always yields a string; void always yields undefined.","Enable no-constant-binary-expression so dead comparisons are reported."],"tags":["lint","dead-code","equality","truthiness","oxlint"],"backgroundTag":"constant-binary-expression","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"}