oxc-project/oxc · warning · OxcDiagnostic

Unnecessary use of boolean literals in conditional expressio

Error message

Unnecessary use of boolean literals in conditional expression

What it means

`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.

Source

Thrown at crates/oxc_linter/src/rules/eslint/no_unneeded_ternary.rs:17

use crate::{
    AstNode,
    context::LintContext,
    rule::{DefaultRuleConfig, Rule},
};
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.

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Use the condition directly (`const ok = flag;`) or coerce explicitly (`Boolean(x)` / `!!x`).
  2. For the inverted form use negation (`!flag`).
  3. Apply the editor autofix or `oxlint --fix` where the dangerous fix is acceptable.

Example fix

// before
const isEnabled = settings?.darkMode ? true : false;

// after
const isEnabled = Boolean(settings?.darkMode);
Defensive patterns

Strategy: validation

Validate before calling

# CI gate; autofix (--fix) covers most boolean-literal ternaries
npx oxlint src/ --fix

Prevention

When it happens

Trigger: `const ok = flag ? true : false;`, `return hasItems ? false : true;` — any ConditionalExpression whose consequent and alternate are both boolean literals.

Common situations: 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.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/dc0ea83264b28797. Report an issue: GitHub.