oxc-project/oxc · warning · OxcDiagnostic

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

Error message

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

What it means

The trailing-statements variant of `oxc/branches-sharing-code`: every branch body of an if/else-if/else chain (with terminal else) ends with identical statements, so the shared tail can move below the `if`. The autofix for a single shared trailing statement is additionally suppressed when that tail references variables declared inside a branch (`duplicated_end_references_branch_locals`), since hoisting would break scoping.

Solutions

  1. Move the shared trailing statements after the `if` (autofix available for a single statement)
  2. If hoisting would break scoping, extract the shared tail into a helper called from each branch
  3. Use try/finally when the tail is cleanup that must run regardless of which branch executed

Example fix

// before
if (cond) {
  doSomething();
  cleanup();
} else {
  doSomethingElse();
  cleanup();
}

// after
if (cond) {
  doSomething();
} else {
  doSomethingElse();
}
cleanup();
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) { doSomething(); cleanup(); } else { doSomethingElse(); cleanup(); }` — both bodies close with the same `cleanup()` call; shared commit/close/unlock/flush tails.

Common situations: Manual try/finally-style cleanup duplicated per branch; resource release calls copy-pasted at each branch end; generated state-machine code with shared epilogue.

Related errors


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

Appendix: source

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

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)),
        )
}

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

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Checks if the `if` and `else` blocks contain shared code that can be moved out of the blocks.
    ///
    /// ### Why is this bad?
    ///
    /// Duplicate code is less maintainable. Extracting common code from branches makes the code more DRY (Don't Repeat Yourself)

View on GitHub (pinned to 1f902a6962)