{"record":{"id":"0298094e258dc998","repo":"astral-sh/ruff","slug":"compound-statement-cannot-be-inlined","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/flake8_return/rules/function.rs","lineNumber":841,"sourceCode":"        )))\n    } else {\n        // the start of the line where the `else`` is\n        let else_line_start = locator.line_start(elif_else.start());\n\n        // making a tokenizer to find the Colon for the `else`, not always on the same line!\n        let mut else_line_tokenizer =\n            SimpleTokenizer::starts_at(elif_else.start(), locator.contents());\n\n        // find the Colon for the `else`\n        let Some(else_colon) =\n            else_line_tokenizer.find(|token| token.kind == SimpleTokenKind::Colon)\n        else {\n            return Err(anyhow::anyhow!(\"Cannot find `:` in `else` statement\"));\n        };\n\n        // get the indentation of the `else`, since that is the indent level we want to end with\n        let Some(desired_indentation) = indentation(locator.contents(), elif_else) else {\n            return Err(anyhow::anyhow!(\"Compound statement cannot be inlined\"));\n        };\n\n        // If the statement is on the same line as the `else`, just remove the `else: `.\n        // Ex) `else: return True` -> `return True`\n        if let Some(first) = elif_else.body.first() {\n            if indexer.preceded_by_multi_statement_line(first, locator.contents()) {\n                return Ok(Fix::safe_edit(Edit::deletion(\n                    elif_else.start(),\n                    first.start(),\n                )));\n            }\n        }\n\n        // we're deleting the `else`, and it's Colon, and the rest of the line(s) they're on,\n        // so here we get the last position of the line the Colon is on\n        let else_colon_end = locator.full_line_end(else_colon.end());\n\n        // if there is a comment on the same line as the Colon, let's keep it","sourceCodeStart":823,"sourceCodeEnd":859,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/rules/flake8_return/rules/function.rs#L823-L859","documentation":"This is an internal fix-generation failure in Ruff's flake8-return superfluous-else fix. When removing `else: return ...` after a `return` in every branch, the fixer must compute the indentation of the `else` body's first statement to dedent it to the loop/function level. Ruff's `indentation()` helper returns None when the first body statement is not on a line of its own or the indent cannot be detected, so the fix is abandoned with this message.","triggerScenarios":"Running the SIM125/RET505-style fix (`superfluous_else_node` -> `remove_else`) on an `if`/`else` where the `else:` body starts with a compound statement or the else-body's first statement's indentation cannot be detected (e.g. multi-statement or otherwise non-standard line layout around `else:`).","commonSituations":"Autofixing Python files with unusual formatting (multiple statements on the `else` line, tabs mixed with spaces, generated/minified code); users running `ruff --fix` see 'fix failed' rather than a normal diagnostic.","solutions":["Reformat the `else` block so the first statement starts on its own line with normal indentation, then rerun the fix","Rewrite the `else: return ...` branch manually into the flat early-return form","Report the snippet upstream as a fixable-lint false negative if the formatting looks standard"],"exampleFix":"// before (fix fails)\nif cond:\n    return a\nelse: return b\n// after (manual early-return form)\nif cond:\n    return a\nreturn b","handlingStrategy":"try-catch","validationCode":"import re\ndef fixable_else(source: str) -> bool:\n    # else body must start on its own line with plain indentation\n    return re.search(r\"^\\s*else:\\s*$\", 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        // fall back to reporting the diagnostic without an autofix\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Keep `else:` and its first statement on separate, consistently indented lines","Avoid multiple statements on the `else:` line","Normalize tabs/spaces before running `ruff --fix`"],"tags":["ruff","autofix","indentation","flake8-return"],"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"}