oxc-project/oxc · warning

Duplicate case label

Error message

Duplicate case label

What it means

This diagnostic comes from the `no_duplicate_case` rule in oxlint. It reports a `switch` statement with two `case` labels whose test expressions are structurally equal. The second case can never run, because the first one already matches. The rule compares each case test with `ContentEq` and reports the later duplicate with both spans labeled.

Source

Thrown at crates/oxc_linter/src/rules/eslint/no_duplicate_case.rs:9

use oxc_ast::ast::Expression;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{ContentEq, GetSpan, Span};

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

fn no_duplicate_case_diagnostic(first: Span, second: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Duplicate case label")
        .with_help("Remove the duplicated case")
        .with_labels([first.label("This label here"), second.label("is duplicated here")])
}

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

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallow duplicate case labels.
    ///
    /// ### Why is this bad?
    ///
    /// If a switch statement has duplicate test expressions in case clauses,
    /// it is likely that a programmer copied a case clause but forgot to change the test expression.
    ///
    /// ### Examples

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Remove the later duplicate case; its body is unreachable.
  2. Merge both bodies into one case when both actions are needed.
  3. Move the tests to named constants so equal values become visible at a glance.

Example fix

// before
switch (status) {
  case 'open':
    start();
    break;
  case 'open':
    reset();
    break;
}

// after
switch (status) {
  case 'open':
    start();
    reset();
    break;
}
Defensive patterns

Strategy: validation

Validate before calling

// author-time check for generated switches
const tests = cases.map(c => JSON.stringify(c.test));
const dup = tests.filter((t, i) => tests.indexOf(t) !== i);
if (dup.length) throw new Error('duplicate case: ' + dup[0]);

Prevention

When it happens

Trigger: A switch with a repeated test: `switch (x) { case 1: a(); break; case 1: b(); break; }`, or a repeated string case such as `case 'active':` twice.

Common situations: A large status-code switch grows over time, and one code is added twice. Constants are inlined as literals during refactor, and two literals now hold the same value. A case block is copied as a starting point.

Related errors


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