oxc-project/oxc · warning · OxcDiagnostic

Backreference '{back_reference}' will be ignored. It referen

Error message

Backreference '{back_reference}' will be ignored. It references group '{group}' which is in another alternative.

What it means

no-useless-backreference, Disjunctive case: the backreference sits in one alternative of an alternation while its target group only participates in a different alternative. On this path the group never captured anything, so the backreference can never match non-empty text and is dead weight in the pattern.

Source

Thrown at crates/oxc_linter/src/rules/eslint/no_useless_backreference.rs:23

    ast::LookAroundAssertionKind,
    visit::{RegExpAstKind, Visit},
};
use oxc_span::Span;

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

fn no_useless_backreference_diagnostic(
    span: Span,
    problem: &Problem,
    back_reference: &str,
    group: &str,
) -> OxcDiagnostic {
    let diagnostic = match problem {
        Problem::Nested => OxcDiagnostic::warn(format!(
            "Backreference '{back_reference}' will be ignored. It references group '{group}' from within that group."
        )),
        Problem::Disjunctive => OxcDiagnostic::warn(format!(
            "Backreference '{back_reference}' will be ignored. It references group '{group}' which is in another alternative."
        )),
        Problem::Forward => OxcDiagnostic::warn(format!(
            "Backreference '{back_reference}' will be ignored. It references group '{group}' which appears later in the pattern."
        )),
        Problem::Backward => OxcDiagnostic::warn(format!(
            "Backreference '{back_reference}' will be ignored. It references group '{group}' which appears before in the same lookbehind."
        )),
        Problem::IntoNegativeLookaround => OxcDiagnostic::warn(format!(
            "Backreference '{back_reference}' will be ignored. It references group '{group}' which is in a negative lookaround."
        )),
    };
    diagnostic
        .with_help("Consider revising the pattern to remove or relocate the backreference so it points to a group that can be matched at the time of evaluation.")
        .with_label(span)
}

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

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Duplicate the subpattern explicitly in each alternative instead of backreferencing across branches: /^(a)|(a)$/.
  2. Restructure so the referenced group encloses the whole alternation that uses it.
  3. Remove the dead backreference if the shared-token intent was wrong.

Example fix

// before
const re = /^(a)|(\1)$/;
// after
const re = /^(a)|(a)$/;
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Regex literals like /(a)|(\1)/ or /(?:x(y))|(?:\1z)/ where the referenced group lives in the other branch of the alternation.

Common situations: Combining two regexes with | after refactoring; alternations where each branch was meant to share a common token; hand-grown URL/date matchers.

Related errors


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