oxc-project/oxc · warning · OxcDiagnostic
Require `default` cases in `switch` statements.
Error message
Require `default` cases in `switch` statements.
What it means
Oxlint's port of the ESLint rule default-case: every `switch` statement must contain a `default` clause. The diagnostic is default_case_diagnostic (crates/oxc_linter/src/rules/eslint/default_case.rs:16) with help 'Add a `default` case.' DefaultCaseConfig carries `commentPattern`, a regex; when a comment inside the switch matches it, the missing default is treated as intentional and not reported.
Source
Thrown at crates/oxc_linter/src/rules/eslint/default_case.rs:17
use lazy_regex::{Regex, RegexBuilder};
use schemars::JsonSchema;
use serde::Deserialize;
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use crate::{
AstNode,
context::LintContext,
rule::{DefaultRuleConfig, Rule},
};
fn default_case_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Require `default` cases in `switch` statements.")
.with_help("Add a `default` case.")
.with_label(span)
}
#[derive(Debug, Default, Clone, Deserialize)]
pub struct DefaultCase(Box<DefaultCaseConfig>);
#[derive(Debug, Default, Clone, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct DefaultCaseConfig {
/// A regex pattern used to detect comments that mark the absence
/// of a `default` case as intentional.
///
/// Default value: `no default`.
///
/// Examples of **incorrect** code for this rule with the `{ "commentPattern": "^skip\\sdefault" }` option:
/// ```js
/// switch (a) {View on GitHub (pinned to e1e7af627c)
Solutions
- Add an explicit default clause to the switch, even a throwing or no-op one.
- If omitting default is intentional, add a comment that matches the configured commentPattern, e.g. `// no default`.
- Configure commentPattern in .oxlintrc.json to your team's marker comment.
- Disable the rule when exhaustiveness is already enforced by TypeScript's never check.
Example fix
// before
switch (kind) {
case 'a': return 1;
case 'b': return 2;
}
// after
switch (kind) {
case 'a': return 1;
case 'b': return 2;
default:
throw new Error(kind);
} Defensive patterns
Strategy: validation
Validate before calling
// .oxlintrc.json — configure the intentional-omission marker comment
{
"rules": {
"default-case": ["warn", { "commentPattern": "^no default$" }]
}
} Prevention
- Make every switch exhaustive with an explicit default, especially over unions.
- Configure commentPattern once so `// no default` comments are honored.
- For TypeScript, combine with a `assertNever`-style default so new union members fail the build.
When it happens
Trigger: Linting any switch that has case clauses but no default clause and no comment matching the configured commentPattern (unset by default, so any comment opt-out requires configuring the pattern).
Common situations: Switches over enums believed to be exhaustive; migrating an ESLint config whose commentPattern (e.g. "^no default$") was not carried over; teammates unaware of the comment-based opt-out.
Related errors
- Enforce default clauses in switch statements to be last
- Expected a `break` statement before `case`.
- Expected a `break` statement before `default`.
- Found a comment that would permit fallthrough, but case cann
- Checking `switch` discriminant against NaN will never match
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/b4dc563d24704969.
Report an issue: GitHub.