{"record":{"id":"2111ae51f9c32e42","repo":"astral-sh/ruff","slug":"indented-block-to-start-with-indentation","errorCode":null,"errorMessage":"indented block to start with indentation","messagePattern":"indented block to start with indentation","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/ruff_linter/src/rules/pylint/rules/collapsible_else_if.rs","lineNumber":145,"sourceCode":"        indexer,\n        stylist,\n    )?;\n\n    // If there's trivia, restore it\n    let trivia = if trivia_range.is_empty() {\n        None\n    } else {\n        let indented_trivia =\n            adjust_indentation(trivia_range, indentation, locator, indexer, stylist)?;\n        Some(Edit::insertion(\n            indented_trivia,\n            locator.line_start(else_clause.start()),\n        ))\n    };\n\n    // Strip the indent from the first line of the `if` statement, and add `el` to the start.\n    let Some(unindented) = indented.strip_prefix(indentation) else {\n        return Err(anyhow::anyhow!(\"indented block to start with indentation\"));\n    };\n    let indented = format!(\"{indentation}el{unindented}\");\n\n    Ok(Fix::safe_edits(\n        Edit::replacement(\n            indented,\n            locator.line_start(else_clause.start()),\n            inner_if_line_end,\n        ),\n        trivia,\n    ))\n}\n","sourceCodeStart":127,"sourceCodeEnd":158,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/rules/pylint/rules/collapsible_else_if.rs#L127-L158","documentation":"Also part of the PLR5501 `elif` rewrite fix. After dedenting the inner `if` block, the fixer expects the dedented text to begin with the outer clause's indentation so it can prepend `el` to form `elif`. If the reconstructed string does not start with the computed indentation prefix (inconsistent indentation between the `else` and the inner `if`), the rewrite is abandoned.","triggerScenarios":"`convert_to_elif` computed an `indentation` string, adjusted the inner block's indentation via `adjust_indentation`, but the result fails `strip_prefix(indentation)` — i.e. the inner `if`'s line does not share the `else` clause's indentation style (e.g. `else` indented with tabs but the inner `if` with spaces).","commonSituations":"Files mixing tabs and spaces across the `else`/nested-`if` lines; code edited by tools with different indent settings; users of `ruff --fix` see the nested-if diagnostic remain unfixed.","solutions":["Re-indent the `else` block and the nested `if` with the same whitespace, then rerun the fix","Manually convert `else: if ...` to `elif ...`","Run a formatter (e.g. `ruff format`) to normalize indentation before autofixing"],"exampleFix":"# before (mixed indentation; fix fails)\nelse:\n    if b:      # indented differently from else\n        g()\n# after (uniform indentation)\nelse:\n  if b:\n    g()\n# becomes elif b:\\n    g()","handlingStrategy":"validation","validationCode":"def consistent_indent(line_else: str, line_if: str) -> bool:\n    ie = len(line_else) - len(line_else.lstrip())\n    ii = len(line_if) - len(line_if.lstrip())\n    return ii > ie  # inner if strictly deeper than else, same style","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Indent the nested `if` with the same character as `else`","Don't mix tabs and spaces in the same block","Format the file before applying autofixes"],"tags":["ruff","autofix","pylint","indentation","elif"],"backgroundTag":"autofix-indentation-detection-failed","analyzedSha":"26f38c119cac42e4d320ba08f09224fdec74af2c","analyzedAt":"2026-09-05T10:32:37.492Z","contentChangedAt":"2026-09-05T10:32:37.492Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}