{"record":{"id":"dfce8a44b91f06e8","repo":"astral-sh/ruff","slug":"expected-outer-with-to-have-indented-body","errorCode":null,"errorMessage":"Expected outer with to have indented body","messagePattern":"Expected outer with to have indented body","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ruff_linter/src/rules/flake8_simplify/rules/fix_with.rs","lineNumber":62,"sourceCode":"        let embedding = match_function_def(&mut tree)?;\n\n        let indented_block = match_indented_block(&mut embedding.body)?;\n        indented_block.indent = Some(outer_indent);\n\n        let Some(statement) = indented_block.body.first_mut() else {\n            bail!(\"Expected indented block to have at least one statement\")\n        };\n        statement\n    };\n\n    let outer_with = match_with(statement)?;\n\n    let With {\n        body: Suite::IndentedBlock(outer_body),\n        ..\n    } = outer_with\n    else {\n        bail!(\"Expected outer with to have indented body\")\n    };\n\n    let [Statement::Compound(CompoundStatement::With(inner_with))] = &mut *outer_body.body else {\n        bail!(\"Expected one inner with statement\");\n    };\n\n    outer_with.items.append(&mut inner_with.items);\n    if outer_with.lpar.is_none() {\n        outer_with.lpar.clone_from(&inner_with.lpar);\n        outer_with.rpar.clone_from(&inner_with.rpar);\n    }\n    outer_with.body = inner_with.body.clone();\n\n    // Reconstruct and reformat the code.\n    let module_text = tree.codegen_stylist(stylist);\n    let contents = if outer_indent.is_empty() {\n        module_text\n    } else {","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/rules/flake8_simplify/rules/fix_with.rs#L44-L80","documentation":"Ruff's SIM117 autofix, after locating the outer `with` statement, expects its body to be a conventional indented block. If the outer `with` matches structurally but its body is not a `Suite::IndentedBlock` (e.g. it is represented as parenthesized/hugging statements or an unexpected suite form), the fixer cannot safely rewrite it and bails.","triggerScenarios":"`fix_multiple_with_statements` called `match_with` on the target statement and got a `With` node whose `body` field is not `Suite::IndentedBlock` — an unusual suite representation for the outer `with`.","commonSituations":"Nonstandard statement layouts (comments-only bodies, dedent edge cases, partial parses) when running `ruff --fix` over generated or heavily formatted code.","solutions":["Rewrite the nested `with` manually (combine contexts into one `with a as x, b as y:`)","Ignore SIM117 for the offending file/line if the shape is intentional","File a Ruff issue with the snippet since the fixer didn't expect this body form"],"exampleFix":"// before\nwith open('a') as a:\n    with open('b') as b:\n        use(a, b)\n// after\nwith open('a') as a, open('b') as b:\n    use(a, b)","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep nested `with` bodies in conventional indented form (no comments-only or empty bodies)","Manually merge nested `with` statements before autofix","Suppress SIM117 where unusual structures are intentional"],"tags":["ruff","autofix","flake8-simplify","fixer"],"backgroundTag":"autofix-failed-to-apply","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"}