{"record":{"id":"71833c27a399b624","repo":"oxc-project/oxc","slug":"unnecessary-use-of-conditional-expression-for-defa","errorCode":null,"errorMessage":"Unnecessary use of conditional expression for default assignment","messagePattern":"Unnecessary use of conditional expression for default assignment","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"info","filePath":"crates/oxc_linter/src/rules/eslint/no_unneeded_ternary.rs","lineNumber":23,"sourceCode":"};\nuse oxc_ast::{\n    AstKind,\n    ast::{BinaryOperator, Expression},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\nuse schemars::JsonSchema;\nuse serde::Deserialize;\n\nfn no_unneeded_ternary_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unnecessary use of boolean literals in conditional expression\")\n        .with_help(\"Remove this ternary operator\")\n        .with_label(span)\n}\n\nfn no_unneeded_ternary_conditional_expression_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unnecessary use of conditional expression for default assignment\")\n        .with_help(\"Remove this ternary operator and use the variable directly\")\n        .with_label(span)\n}\n\n#[derive(Debug, Clone, JsonSchema, Deserialize)]\n#[serde(rename_all = \"camelCase\", default, deny_unknown_fields)]\npub struct NoUnneededTernary {\n    /// Whether to allow the default assignment pattern `x ? x : y`.\n    ///\n    /// When set to `false`, the rule also flags cases like `x ? x : y` and suggests using\n    /// the logical OR form `x || y` instead. When `true` (default), such default assignments\n    /// are allowed and not reported.\n    default_assignment: bool,\n}\n\nimpl Default for NoUnneededTernary {\n    fn default() -> Self {\n        Self { default_assignment: true }","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_unneeded_ternary.rs#L5-L41","documentation":"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).","triggerScenarios":"Enabling `\"no-unneeded-ternary\": [\"error\", { \"defaultAssignment\": false }]` and writing `foo(bar ? bar : 1)` or `const v = input ? input : 'default'`.","commonSituations":"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.","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."],"exampleFix":"// before\nconst port = config.port ? config.port : 3000;\n\n// after\nconst port = config.port ?? 3000;","handlingStrategy":"validation","validationCode":"# find default-assignment ternaries before turning on defaultAssignment: false\nrg -n '\\?\\s*([A-Za-z_$][\\w$]*)\\s*:\\s' src/ | rg '\\1'","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["eslint","oxlint","no-unneeded-ternary","ternary","default-assignment","nullish-coalescing"],"backgroundTag":"redundant-ternary","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"}