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
- Hoist the duplicated leading statements above the `if` (oxlint's autofix does this when the prefix is a single statement)
- If the 'duplicates' were supposed to differ, fix the copy-paste slip in the branch that is wrong
- 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
- Hoist shared prologue code above the `if` while writing branches, not after review
- After copy-pasting a branch, diff the duplicates — the rule only fires when ALL branches (including the final else) share the code
- Enable this pedantic rule explicitly; it is off by default
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
- All `if` blocks contain the same code at the end
- Global flag (g) is missing in the regular expression supplie
- Unexpected object literal comparison.
- Global flag (g) is missing in the regular expression supplie
- Left-hand side of `&&` operator has no effect.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/8c36f5ae0268cadf.
Report an issue: GitHub.