{"record":{"id":"dc0ea83264b28797","repo":"oxc-project/oxc","slug":"unnecessary-use-of-boolean-literals-in-conditional","errorCode":null,"errorMessage":"Unnecessary use of boolean literals in conditional expression","messagePattern":"Unnecessary use of boolean literals in conditional expression","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/no_unneeded_ternary.rs","lineNumber":17,"sourceCode":"use crate::{\n    AstNode,\n    context::LintContext,\n    rule::{DefaultRuleConfig, Rule},\n};\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.","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_unneeded_ternary.rs#L1-L35","documentation":"`no-unneeded-ternary` flags conditional expressions whose branches are boolean literals (`cond ? true : false` / `cond ? false : true`) — the ternary adds nothing over the condition itself or its negation. The message is \"Unnecessary use of boolean literals in conditional expression\" with help \"Remove this ternary operator\"; the rule is registered with `fix_dangerous`, so an editor autofix is available.","triggerScenarios":"`const ok = flag ? true : false;`, `return hasItems ? false : true;` — any ConditionalExpression whose consequent and alternate are both boolean literals.","commonSituations":"Developers forcing boolean coercion of truthy values (`val ? true : false` instead of `!!val` or `Boolean(val)`); explicit-looking code written for readability that reviewers then flag; porting Java/C-style idioms into JS.","solutions":["Use the condition directly (`const ok = flag;`) or coerce explicitly (`Boolean(x)` / `!!x`).","For the inverted form use negation (`!flag`).","Apply the editor autofix or `oxlint --fix` where the dangerous fix is acceptable."],"exampleFix":"// before\nconst isEnabled = settings?.darkMode ? true : false;\n\n// after\nconst isEnabled = Boolean(settings?.darkMode);","handlingStrategy":"validation","validationCode":"# CI gate; autofix (--fix) covers most boolean-literal ternaries\nnpx oxlint src/ --fix","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Coerce with Boolean(x) or !!x instead of x ? true : false.","Negate with !cond instead of cond ? false : true.","Apply the editor quick-fix when the diagnostic appears."],"tags":["eslint","oxlint","no-unneeded-ternary","ternary","boolean","style"],"backgroundTag":"redundant-ternary","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"}