oxc-project/oxc · info · OxcDiagnostic
Unnecessary use of conditional expression for default assign
Error message
Unnecessary use of conditional expression for default assignment
What it means
The second `no-unneeded-ternary` diagnostic, from `no_unneeded_ternary_conditional_expression_diagnostic`: it reports the default-assignment pattern `x ? x : y` when the rule's `defaultAssignment` option is set to `false` (default `true` allows it). The help text recommends removing the ternary and using the variable directly — i.e. the logical OR `x || y` (or `?? y` for nullish-only defaults).
Source
Thrown at crates/oxc_linter/src/rules/eslint/no_unneeded_ternary.rs:23
};
use oxc_ast::{
AstKind,
ast::{BinaryOperator, Expression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use schemars::JsonSchema;
use serde::Deserialize;
fn no_unneeded_ternary_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Unnecessary use of boolean literals in conditional expression")
.with_help("Remove this ternary operator")
.with_label(span)
}
fn no_unneeded_ternary_conditional_expression_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Unnecessary use of conditional expression for default assignment")
.with_help("Remove this ternary operator and use the variable directly")
.with_label(span)
}
#[derive(Debug, Clone, JsonSchema, Deserialize)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct NoUnneededTernary {
/// Whether to allow the default assignment pattern `x ? x : y`.
///
/// When set to `false`, the rule also flags cases like `x ? x : y` and suggests using
/// the logical OR form `x || y` instead. When `true` (default), such default assignments
/// are allowed and not reported.
default_assignment: bool,
}
impl Default for NoUnneededTernary {
fn default() -> Self {
Self { default_assignment: true }View on GitHub (pinned to e1e7af627c)
Solutions
- Replace with logical OR: `foo(bar || 1)`.
- If only null/undefined should trigger the default, use nullish coalescing: `input ?? 'default'` (safer for `0`/`''`).
- If the ternary form is your team's accepted style, set `defaultAssignment: true` in the rule config.
Example fix
// before const port = config.port ? config.port : 3000; // after const port = config.port ?? 3000;
Defensive patterns
Strategy: validation
Validate before calling
# find default-assignment ternaries before turning on defaultAssignment: false rg -n '\?\s*([A-Za-z_$][\w$]*)\s*:\s' src/ | rg '\1'
Prevention
- Default to `??` for nullish defaults and `||` for falsy defaults.
- Set defaultAssignment: false only after migrating existing x ? x : y sites.
- Remember `||` swallows 0/'' — prefer `??` when only null/undefined should trigger.
When it happens
Trigger: Enabling `"no-unneeded-ternary": ["error", { "defaultAssignment": false }]` and writing `foo(bar ? bar : 1)` or `const v = input ? input : 'default'`.
Common situations: Teams standardizing on `||`/`??` for defaults turning on this option; codebases migrating from older ternary-default idioms to nullish coalescing; config option discovered after upgrading oxlint versions where the option was added.
Related errors
- Unnecessary use of boolean literals in conditional expressio
- Empty array binding pattern
- Empty object binding pattern
- Expected method{method_name_str} to have this.
- {name} has a complexity of {complexity}. Maximum allowed is
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/71833c27a399b624.
Report an issue: GitHub.