{"record":{"id":"8c36f5ae0268cadf","repo":"oxc-project/oxc","slug":"all-if-blocks-contain-the-same-code-at-the-start","errorCode":null,"errorMessage":"All `if` blocks contain the same code at the start","messagePattern":"All `if` blocks contain the same code at the start","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/oxc/branches_sharing_code.rs","lineNumber":20,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{Expression, IdentifierReference, IfStatement, Statement},\n};\nuse oxc_ast_visit::Visit;\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_ecmascript::BoundNames;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_semantic::{ReferenceId, SymbolId};\nuse oxc_span::{ContentEq, GetSpan, Span};\nuse rustc_hash::FxHashSet;\n\nuse 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}","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/oxc-project/oxc/blob/1f902a69625cd14e6d8dc58feca94da099d2d251/crates/oxc_linter/src/rules/oxc/branches_sharing_code.rs#L2-L38","documentation":"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`.","triggerScenarios":"`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).","commonSituations":"Copy-pasting a branch and editing only the middle; adding identical logging/telemetry/lock acquisition to every branch; generated code with a duplicated prologue.","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"],"exampleFix":"// before\nif (cond) {\n  log('enter');\n  return 13;\n} else {\n  log('enter');\n  return 42;\n}\n\n// after\nlog('enter');\nif (cond) {\n  return 13;\n} else {\n  return 42;\n}","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":["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"],"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"}