{"record":{"id":"66613038c0572ef8","repo":"astral-sh/ruff","slug":"expected-one-inner-with-statement","errorCode":null,"errorMessage":"Expected one inner with statement","messagePattern":"Expected one inner with statement","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ruff_linter/src/rules/flake8_simplify/rules/fix_with.rs","lineNumber":66,"sourceCode":"\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 {\n        module_text\n            .strip_prefix(&format!(\"def f():{}\", stylist.line_ending().as_str()))\n            .unwrap()\n            .to_string()","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/rules/flake8_simplify/rules/fix_with.rs#L48-L84","documentation":"This completes the SIM117 fix: the outer `with` must contain exactly one inner `with` statement so the two context lists can be merged. If the outer body holds zero or multiple statements, or the first statement isn't a `with`, the merge cannot proceed and the fixer bails.","triggerScenarios":"The outer `with`'s indented body contains anything other than exactly one compound `With` statement — e.g. multiple statements, a non-with statement, or a trailing comment parsed as a body element — while running the SIM117 autofix.","commonSituations":"Nested `with` blocks that contain additional statements inside the outer context, code with interleaved comments or `pass` lines between the two `with` statements, or partially refactored code.","solutions":["Manually merge the nested `with` statements into one: `with a as x, b as y:`","Ensure the inner block contains only the single inner `with` if you want the autofix to apply","Ignore SIM117 via noqa or configuration if the structure is intentional"],"exampleFix":"// before (fixer cannot merge: extra statement inside outer with)\nwith open('a') as a:\n    log(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":"// Autofix only applies when the outer `with` body is exactly one inner `with`:\n# ensure: with X:\n#             with Y:\n#                 body","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Merge nested with statements manually when extra statements sit between them","Avoid interleaving comments/pass between outer and inner with","Add `# noqa: SIM117` when nesting is 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"}