{"record":{"id":"1aeb23de0846035a","repo":"astral-sh/ruff","slug":"compound-statement-cannot-be-inlined-1aeb23","errorCode":null,"errorMessage":"Compound statement cannot be inlined","messagePattern":"Compound statement cannot be inlined","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/ruff_linter/src/rules/pylint/rules/useless_else_on_loop.rs","lineNumber":157,"sourceCode":") -> Result<Fix> {\n    let Some(start) = orelse.first() else {\n        return Err(anyhow::anyhow!(\"Empty `else` clause\"));\n    };\n    let Some(end) = orelse.last() else {\n        return Err(anyhow::anyhow!(\"Empty `else` clause\"));\n    };\n\n    let start_indentation = indentation(locator.contents(), start);\n    if start_indentation.is_none() {\n        // Inline `else` block (e.g., `else: x = 1`).\n        Ok(Fix::safe_edit(Edit::deletion(\n            else_range.start(),\n            start.start(),\n        )))\n    } else {\n        // Identify the indentation of the loop itself (e.g., the `while` or `for`).\n        let Some(desired_indentation) = indentation(locator.contents(), stmt) else {\n            return Err(anyhow::anyhow!(\"Compound statement cannot be inlined\"));\n        };\n\n        // Dedent the content from the end of the `else` to the end of the loop.\n        let indented = adjust_indentation(\n            TextRange::new(\n                locator.full_line_end(else_range.start()),\n                locator.full_line_end(end.end()),\n            ),\n            desired_indentation,\n            locator,\n            indexer,\n            stylist,\n        )?;\n\n        // Replace the content from the start of the `else` to the end of the loop.\n        Ok(Fix::safe_edit(Edit::replacement(\n            indented,\n            locator.line_start(else_range.start()),","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/rules/pylint/rules/useless_else_on_loop.rs#L139-L175","documentation":"Fix-generation failure in the useless-else-on-loop (PLW0120) fix for multi-statement `else` bodies. When the `else` body contains a compound/indented block, the fixer must dedent the body to the loop's own indentation level; it obtains that level from the enclosing `for`/`while` statement. If `indentation()` returns None for the loop statement (irregular layout around the loop keyword), the dedent cannot be computed and the fix aborts.","triggerScenarios":"`remove_else` handles an `else` body that is not inline (start_indentation is None path), then fails to detect the indentation of the enclosing `for`/`while` statement — e.g. the loop keyword is not at the start of an indentable line, tabs/spaces are mixed, or the statement layout is non-standard.","commonSituations":"Autofixing loops with an `else` block containing multiple statements in files with unusual or mixed indentation, or generated code; users see PLW0120 reported but not fixed by `ruff --fix`.","solutions":["Normalize indentation of the loop and its `else` block (same style, loop keyword on its own line), then rerun the fix","Remove the loop `else` manually and move its statements after the loop (or inline the single-statement case)","Run `ruff format` on the file before applying fixes"],"exampleFix":"# before (fix fails: loop indentation undetectable)\nfor x in xs:  # irregular leading whitespace\n    f()\nelse:\n    g()\n    h()\n# after (dedented after removal)\nfor x in xs:\n    f()\ng()\nh()","handlingStrategy":"try-catch","validationCode":"import re\ndef loop_indent_detectable(source: str) -> bool:\n    # loop keyword at line start with plain leading whitespace\n    return re.search(r\"^[ \\t]*(for|while)\\b\", source, re.M) is not None","typeGuard":null,"tryCatchPattern":"match ruff.fix(path) {\n    Ok(fixes) => apply(fixes),\n    Err(e) if e.to_string().contains(\"Compound statement cannot be inlined\") => {\n        // report PLW0120 without autofix; suggest manual cleanup\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Keep `for`/`while` keywords at consistent indentation","Avoid multi-statement else bodies; move code after the loop instead","Run the formatter before autofixing irregular files"],"tags":["ruff","autofix","pylint","indentation","loop"],"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-14T00:17:10.932Z"}