oxc-project/oxc · warning

Empty character class will not match anything

Error message

Empty character class will not match anything

What it means

This diagnostic comes from the `no_empty_character_class` rule in oxlint. It reports `[]` inside a regular expression. An empty character class matches no character at all, so the whole regex can never match. The rule visits the parsed regex AST from `oxc_regular_expression` and reports each empty `CharacterClass` span.

Source

Thrown at crates/oxc_linter/src/rules/eslint/no_empty_character_class.rs:15

use memchr::memchr2;
// Ported from https://github.com/eslint/eslint/blob/v9.9.1/lib/rules/no-empty-character-class.js
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_regular_expression::{
    ast::CharacterClass,
    visit::{Visit, walk::walk_character_class},
};
use oxc_span::Span;

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

fn no_empty_character_class_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Empty character class will not match anything")
        .with_help("Remove the empty character class: `[]`")
        .with_label(span)
}

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

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallow empty character classes in regular expressions.
    ///
    /// ### Why is this bad?
    ///
    /// Because empty character classes in regular expressions do not match anything, they might be typing mistakes.
    ///
    /// ### Examples
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Remove the empty class: `/abc/` instead of `/ab[]c/`.
  2. Guard builders so a class is never generated empty.
  3. Use `[\s\S]` when the intent was to match any character.

Example fix

// before
const re = /v[0-9][]/; // never matches

// after
const re = /v[0-9]/;
Defensive patterns

Strategy: validation

Validate before calling

// reject regexes that contain an empty character class
function hasEmptyClass(pattern) {
  return /(^|[^\\])\[\]/.test(pattern);
}
if (hasEmptyClass(userPattern)) throw new Error('empty character class');

Prevention

When it happens

Trigger: A regex literal with an empty class: `/ab[]c/` or `/^[]/`. The pattern compiles without an error, so the bug stays silent until a test notices that nothing matches.

Common situations: A character class is built by string concatenation and ends up empty when the input set is empty: `new RegExp('[' + chars + ']')`. An edit drops the intended range. A placeholder is copied from docs without content.

Related errors


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