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.
///
/// ### ExamplesView on GitHub (pinned to e1e7af627c)
Solutions
- Remove the later duplicate case; its body is unreachable.
- Merge both bodies into one case when both actions are needed.
- 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
- Prefer a map from value to handler; duplicate keys then fail fast in tests.
- Group related cases with fall-through instead of copying a test.
- Give case values named constants so equal values are visible.
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
- Duplicate class member: {member_name:?}
- Duplicate conditions in if-else-if chain
- Duplicate key '{key}'
- `debugger` statement is not allowed
- Variables should not be deleted
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/a32c6a38c256b731.
Report an issue: GitHub.