oxc-project/oxc · warning · OxcDiagnostic

All `if` blocks contain the same code at the start

Error message

All `if` blocks contain the same code at the start

What it means

Oxlint rule `oxc/branches-sharing-code` (pedantic category) reports an `if`/`else if` chain that ends in a final `else` where every branch body starts with identical statements. The primary label marks the `if` keyword span and secondary labels mark each duplicated opening statement. When the shared prefix is exactly one statement, oxlint offers a multi-fix that moves it above the `if`; the rule requires at least two bodies plus a terminal else and analyzes the chain from its root rather than per `else if`.

Source

Thrown at crates/oxc_linter/src/rules/oxc/branches_sharing_code.rs:20

use oxc_ast::{
    AstKind,
    ast::{Expression, IdentifierReference, IfStatement, Statement},
};
use oxc_ast_visit::Visit;
use oxc_diagnostics::OxcDiagnostic;
use oxc_ecmascript::BoundNames;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::{ReferenceId, SymbolId};
use oxc_span::{ContentEq, GetSpan, Span};
use rustc_hash::FxHashSet;

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

fn branches_sharing_code_at_start_diagnostic(
    span: Span,
    duplicated_code_spans: impl Iterator<Item = Span>,
) -> OxcDiagnostic {
    OxcDiagnostic::warn("All `if` blocks contain the same code at the start")
        .with_help("Move the shared code outside the `if` statement to reduce code duplication")
        .with_labels(
            std::iter::once(span.primary_label("`if` statement declared here"))
                .chain(duplicated_code_spans.map(Into::into)),
        )
}

fn branches_sharing_code_at_end_diagnostic(
    span: Span,
    duplicated_code_spans: impl Iterator<Item = Span>,
) -> OxcDiagnostic {
    OxcDiagnostic::warn("All `if` blocks contain the same code at the end")
        .with_help("Move the shared code outside the `if` statement to reduce code duplication")
        .with_labels(
            std::iter::once(span.primary_label("`if` statement declared here"))
                .chain(duplicated_code_spans.map(Into::into)),
        )
}

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Hoist the duplicated leading statements above the `if` (oxlint's autofix does this when the prefix is a single statement)
  2. If the 'duplicates' were supposed to differ, fix the copy-paste slip in the branch that is wrong
  3. Enable the rule explicitly in your oxlint config — pedantic rules are not in the default set

Example fix

// before
if (cond) {
  log('enter');
  return 13;
} else {
  log('enter');
  return 42;
}

// after
log('enter');
if (cond) {
  return 13;
} else {
  return 42;
}
Defensive patterns

Strategy: validation

Validate before calling

// .oxlintrc.json — pedantic rule, must be enabled explicitly
{
  "rules": { "oxc/branches-sharing-code": "warn" }
}
// CLI: npx oxlint src/

Prevention

When it happens

Trigger: `if (c) { console.log('Hello'); return 13; } else { console.log('Hello'); return 42; }` — both branches open with the same logging call; any if/else-if/else chain whose bodies all begin with the same statement(s).

Common situations: Copy-pasting a branch and editing only the middle; adding identical logging/telemetry/lock acquisition to every branch; generated code with a duplicated prologue.

Related errors


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