oxc-project/oxc · error · OxcDiagnostic

Prefer using a logical operator over a ternary.

Error message

Prefer using a logical operator over a ternary.

What it means

Diagnostic from the unicorn/prefer-logical-operator-over-ternary lint rule. It fires when a conditional expression returns one of its branches directly, e.g. `a ? a : b` or `a ? b : a`; the logical operators `||` or `??` express the same fallback semantics more concisely. The flagged input is the ternary expression whose branches duplicate a condition operand. It is a lint suggestion, not a runtime error.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/prefer_logical_operator_over_ternary.rs:10

use oxc_ast::{AstKind, ast::Expression};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::UnaryOperator;

use crate::{AstNode, context::LintContext, rule::Rule, utils::is_same_expression};

fn prefer_logical_operator_over_ternary_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Prefer using a logical operator over a ternary.")
        .with_help("Switch to \"||\" or \"??\" operator")
        .with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct PreferLogicalOperatorOverTernary;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// This rule finds ternary expressions that can be simplified to a logical operator.
    ///
    /// ### Why is this bad?
    ///
    /// Using a logical operator is shorter and simpler than a ternary expression.
    ///
    /// ### Examples
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Rewrite `a ? a : b` as `a || b` when truthiness is intended
  2. Use `a ?? b` when only `null`/`undefined` should fall through
  3. Apply the rule's auto-fix
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_logical_operator_over_ternary.rs:10 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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