{"record":{"id":"ff225c20dfd34bab","repo":"oxc-project/oxc","slug":"empty-character-class-will-not-match-anything","errorCode":null,"errorMessage":"Empty character class will not match anything","messagePattern":"Empty character class will not match anything","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/no_empty_character_class.rs","lineNumber":15,"sourceCode":"use memchr::memchr2;\n// Ported from https://github.com/eslint/eslint/blob/v9.9.1/lib/rules/no-empty-character-class.js\nuse oxc_ast::AstKind;\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_regular_expression::{\n    ast::CharacterClass,\n    visit::{Visit, walk::walk_character_class},\n};\nuse oxc_span::Span;\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn no_empty_character_class_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Empty character class will not match anything\")\n        .with_help(\"Remove the empty character class: `[]`\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoEmptyCharacterClass;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Disallow empty character classes in regular expressions.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// Because empty character classes in regular expressions do not match anything, they might be typing mistakes.\n    ///\n    /// ### Examples\n    ///","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_empty_character_class.rs#L1-L33","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Remove the empty class: `/abc/` instead of `/ab[]c/`.","Guard builders so a class is never generated empty.","Use `[\\s\\S]` when the intent was to match any character."],"exampleFix":"// before\nconst re = /v[0-9][]/; // never matches\n\n// after\nconst re = /v[0-9]/;","handlingStrategy":"validation","validationCode":"// reject regexes that contain an empty character class\nfunction hasEmptyClass(pattern) {\n  return /(^|[^\\\\])\\[\\]/.test(pattern);\n}\nif (hasEmptyClass(userPattern)) throw new Error('empty character class');","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Unit-test each critical regex with strings that must match and must not match.","Build classes from arrays and assert a non-empty result before you join.","Keep oxlint in the editor so the literal is flagged as you type it."],"tags":["javascript","eslint","lint","regex"],"backgroundTag":"lint-empty-regex-character-class","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}