{"record":{"id":"1e78c170995c9b6f","repo":"oxc-project/oxc","slug":"all-if-blocks-contain-the-same-code-at-the-end","errorCode":null,"errorMessage":"All `if` blocks contain the same code at the end","messagePattern":"All `if` blocks contain the same code at the end","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/oxc/branches_sharing_code.rs","lineNumber":32,"sourceCode":"use crate::{AstNode, ast_util::get_preceding_indent_str, context::LintContext, rule::Rule};\n\nfn branches_sharing_code_at_start_diagnostic(\n    span: Span,\n    duplicated_code_spans: impl Iterator<Item = Span>,\n) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"All `if` blocks contain the same code at the start\")\n        .with_help(\"Move the shared code outside the `if` statement to reduce code duplication\")\n        .with_labels(\n            std::iter::once(span.primary_label(\"`if` statement declared here\"))\n                .chain(duplicated_code_spans.map(Into::into)),\n        )\n}\n\nfn branches_sharing_code_at_end_diagnostic(\n    span: Span,\n    duplicated_code_spans: impl Iterator<Item = Span>,\n) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"All `if` blocks contain the same code at the end\")\n        .with_help(\"Move the shared code outside the `if` statement to reduce code duplication\")\n        .with_labels(\n            std::iter::once(span.primary_label(\"`if` statement declared here\"))\n                .chain(duplicated_code_spans.map(Into::into)),\n        )\n}\n\n#[derive(Debug, Default, Clone)]\npub struct BranchesSharingCode;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Checks if the `if` and `else` blocks contain shared code that can be moved out of the blocks.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// Duplicate code is less maintainable. Extracting common code from branches makes the code more DRY (Don't Repeat Yourself)","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/oxc-project/oxc/blob/1f902a69625cd14e6d8dc58feca94da099d2d251/crates/oxc_linter/src/rules/oxc/branches_sharing_code.rs#L14-L50","documentation":"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.","triggerScenarios":"`if (c) { doSomething(); cleanup(); } else { doSomethingElse(); cleanup(); }` — both bodies close with the same `cleanup()` call; shared commit/close/unlock/flush tails.","commonSituations":"Manual try/finally-style cleanup duplicated per branch; resource release calls copy-pasted at each branch end; generated state-machine code with shared epilogue.","solutions":["Move the shared trailing statements after the `if` (autofix available for a single statement)","If hoisting would break scoping, extract the shared tail into a helper called from each branch","Use try/finally when the tail is cleanup that must run regardless of which branch executed"],"exampleFix":"// before\nif (cond) {\n  doSomething();\n  cleanup();\n} else {\n  doSomethingElse();\n  cleanup();\n}\n\n// after\nif (cond) {\n  doSomething();\n} else {\n  doSomethingElse();\n}\ncleanup();","handlingStrategy":"validation","validationCode":"// .oxlintrc.json — pedantic rule, must be enabled explicitly\n{\n  \"rules\": { \"oxc/branches-sharing-code\": \"warn\" }\n}\n// CLI: npx oxlint src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Move shared tail statements after the `if` when both branches end identically","Prefer try/finally for cleanup that must run regardless of branch","If the shared tail references branch-local variables, extract it into a helper instead of hoisting"],"tags":["oxlint","oxc","code-duplication","pedantic","javascript","static-analysis"],"backgroundTag":"duplicated-branch-code","analyzedSha":"1f902a69625cd14e6d8dc58feca94da099d2d251","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}