oxc-project/oxc · warning · OxcDiagnostic

Found identifier '{name}' with the same name as a label.

Error message

Found identifier '{name}' with the same name as a label.

What it means

Diagnostic from oxlint's eslint/no-label-var rule (crates/oxc_linter/src/rules/eslint/no_label_var.rs:9). It reports a labeled statement whose label has the same name as a variable in scope (e.g. 'var x = 1; x: while (true) {...}'). The label and the variable live in different namespaces so this is legal JavaScript, but readers commonly mistake break x / continue x for references to the variable.

Source

Thrown at crates/oxc_linter/src/rules/eslint/no_label_var.rs:9

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 no_label_var_diagnostic(name: &str, id_span: Span, label_span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("Found identifier '{name}' with the same name as a label."))
        .with_help("Rename either the variable or the label to avoid confusion.")
        .with_labels([
            id_span.label(format!("Identifier '{name}' found here.")),
            label_span.label("Label with the same name."),
        ])
}

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

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallow labels that share a name with a variable.
    ///
    /// ### Why is this bad?
    ///
    /// This rule aims to create clearer code by disallowing the bad practice of creating a label

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Rename the label to something that cannot be confused with data, e.g. 'outerLoop:' instead of the variable's name
  2. Or rename the variable so it no longer collides with the label
  3. Prefer removing the label entirely by restructuring the loop condition or extracting a function

Example fix

// before
const max = 10;
max: for (let i = 0; i < max; i++) {
  for (let j = 0; j < max; j++) {
    if (j > i) continue max;
  }
}

// after
const max = 10;
outerLoop: for (let i = 0; i < max; i++) {
  for (let j = 0; j < max; j++) {
    if (j > i) continue outerLoop;
  }
}
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: Declaring a variable and later using its name as a statement label: 'const speed = 5; speed: for (...) { break speed; }'. Both identifier references and label identifiers in the same file/scope collide.

Common situations: Code written by developers used to Go-style labels; refactoring where a loop gets a label that matches an existing variable; search-and-replace renames that create accidental name sharing.

Related errors


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