oxc-project/oxc · warning · OxcDiagnostic

Enforce default clauses in switch statements to be last

Error message

Enforce default clauses in switch statements to be last

What it means

Oxlint's port of the ESLint rule default-case-last: when a switch has a `default` clause, it must be the final clause. The diagnostic highlights exactly the keyword via Span::sized(span.start, 7) (crates/oxc_linter/src/rules/eslint/default_case_last.rs:12) and labels it 'Default clause should be the last clause.'

Source

Thrown at crates/oxc_linter/src/rules/eslint/default_case_last.rs:11

use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;

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

fn default_case_last_diagnostic(span: Span) -> OxcDiagnostic {
    let default_span = Span::sized(span.start, 7);

    OxcDiagnostic::warn("Enforce default clauses in switch statements to be last")
        .with_label(default_span.label("Default clause should be the last clause."))
}

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

declare_oxc_lint!(
    /// ### What it does
    /// Requires the `default` clause in `switch` statements to be the last one.
    ///
    /// ### Why is this bad?
    /// By convention and for readability, the `default` clause should be the last one in a `switch`.
    /// While it is legal to place it before or between `case` clauses, doing so is confusing and may
    /// lead to unexpected "fall-through" behavior.
    ///
    /// ### Examples
    ///
    /// Examples of **incorrect** code for this rule:

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Move the default clause to the end of the switch body.
  2. Note that default-on-top still executes correctly at runtime (cases after it still match), so reordering is always behavior-safe.

Example fix

// before
switch (x) {
  default: return 0;
  case 1: return 1;
}

// after
switch (x) {
  case 1: return 1;
  default: return 0;
}
Defensive patterns

Strategy: validation

Validate before calling

// .oxlintrc.json
{
  "rules": {
    "default-case-last": "warn"
  }
}

Prevention

When it happens

Trigger: Any switch where default appears before one or more case clauses, e.g. `switch (x) { default: r(); case 1: f(); }`.

Common situations: Reordering cases for perceived performance and leaving default on top; copy-pasted switch templates; hand-merging similar switches.

Related errors


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