oxc-project/oxc · error
Unexpected constant {lhs_name} on the left-hand side of a {e
Error message
Unexpected constant {lhs_name} on the left-hand side of a {expr_name:?} expression What it means
Diagnostic from the oxlint rule `no-constant-binary-expression`, emitted by `constant_short_circuit` (crates/oxc_linter/src/rules/eslint/no_constant_binary_expression.rs:81). It fires on `||`/`&&` whose left side has constant truthiness, or on `??` whose left side has constant nullishness — e.g. `true || x`, `[] && foo`, `1 ?? foo`. Because the left operand is statically known, the expression always evaluates to the left constant and the right side is dead. Common trigger: misjudged precedence like `a + b ?? c`, which parses as `(a + b) ?? c` where `a + b` can never be nullish.
Source
Thrown at crates/oxc_linter/src/rules/eslint/no_constant_binary_expression.rs:82
/// // However, this will always result in `isEmpty` being `false`.
/// ```
///
/// Examples of **correct** code for this rule:
/// ```javascript
/// const x = a + (b ?? c);
///
/// const isEmpty = x.length === 0;
/// ```
NoConstantBinaryExpression,
eslint,
correctness,
config = NoConstantBinaryExpressionConfig,
version = "0.0.3",
short_description = "Disallow expressions where the operation doesn't affect the value.",
);
fn constant_short_circuit(lhs_name: &str, expr_name: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!(
"Unexpected constant {lhs_name} on the left-hand side of a {expr_name:?} expression"
))
.with_help("This expression always evaluates to the constant on the left-hand side")
.with_label(span)
}
fn constant_binary_operand(left_or_right: &str, operator: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Unexpected constant binary expression")
.with_help(format!(
"This compares constantly with the {left_or_right}-hand side of the {operator}"
))
.with_label(span)
}
fn constant_always_new(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Unexpected comparison to newly constructed object")
.with_help("These two values can never be equal")
.with_label(span)View on GitHub (pinned to e1e7af627c)
Solutions
- Add explicit parentheses to express the intended grouping: `a + (b ?? c)`.
- Drop the dead right side entirely when the left is intentionally constant.
- For default values use `??` directly on the nullable operand, not on a sub-expression that cannot be nullish.
- Replace object-truthiness checks with the specific test you mean, e.g. `x.length === 0` instead of `x === []` patterns.
Example fix
// before const x = a + b ?? c; // after const x = a + (b ?? c);
Defensive patterns
Strategy: validation
Validate before calling
// Grep gate for precedence-prone `+ ... ??` and constant-left logicals
const precedenceRisk = /[+\-*/%]\s*\w+\s*\?\?/.test(src);
const constantLeft = /(^|[([{;,])\s*(true|false|\[\]|\{\}|\d+)\s*(&&|\|\||\?\?)/.test(src); Prevention
- Parenthesize any expression mixing arithmetic and ?? / || / &&.
- Never put a literal or fresh object/array on the left of &&, ||, ??.
- Keep no-constant-binary-expression enabled in the correctness category.
When it happens
Trigger: AstKind::LogicalExpression where operator is Or/And and `left.is_constant(true, ctx)` (literals, `[]`, `{}`, arrow/function/class expressions, regex literals, boxed `new Boolean(...)`, void/typeof results, template literals without holes), or operator is Coalesce and the left side has constant nullishness (never-nullish objects or always-nullish `undefined`). Message reads e.g. 'Unexpected constant truthiness on the left-hand side of a "||" expression'.
Common situations: Arithmetic-then-default chains (`price + tax ?? 0`) where the author expected `+` to bind tighter than `??`; developers from value-comparison languages writing `[] && foo`; copy-pasted defaults after a truthiness refactor.
Related errors
- Unexpected constant binary expression
- Unexpected comparison to newly constructed object
- Unexpected comparison of two newly constructed objects
- Unexpected constant relational comparison
- Unexpected constant condition
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/a4981f5ef6e7acdf.
Report an issue: GitHub.