oxc-project/oxc · warning

Duplicate conditions in if-else-if chain

Error message

Duplicate conditions in if-else-if chain

What it means

This diagnostic comes from the `no_dupe_else_if` rule in oxlint. It reports an `if / else if` chain where one test is structurally equal to an earlier test in the same chain. The later branch can never run, because the earlier branch already captured that condition. The rule compares test expressions with `ContentEq`, including the operands of `&&` and `||` chains.

Source

Thrown at crates/oxc_linter/src/rules/eslint/no_dupe_else_if.rs:13

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

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

fn no_dupe_else_if_diagnostic(first_test: Span, second_test: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Duplicate conditions in if-else-if chain")
        .with_help(
            "Remove or modify the duplicate condition, as its branch will never be executed.",
        )
        .with_labels([
            first_test.label("condition first checked here"),
            second_test.label("this branch will never be executed"),
        ])
}

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

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallow duplicate conditions in if-else-if chains.
    ///
    /// ### Why is this bad?

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Change or remove the duplicate test in the later branch.
  2. Merge both cases into one branch with `||` when they need the same body.
  3. Replace the chain with a lookup map or a `switch` when each test maps one value to one branch.

Example fix

// before
if (role === 'admin') {
  showPanel();
} else if (role === 'admin') {
  showSecretPanel();
}

// after
if (role === 'admin') {
  showPanel();
  showSecretPanel();
}
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: An `else if` whose test equals an earlier test: `if (x === 1) { a(); } else if (x === 1) { b(); }`. Overlaps inside logical operators also count, for example `if (a || b) {} else if (b || a) {}`.

Common situations: A long condition chain grows by copy-paste. A condition moves during refactor and the old copy stays. Flag names change mid-chain, and a duplicate slips in after a partial rename.

Related errors


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