{"record":{"id":"6f047dd8afb4e2ec","repo":"astral-sh/ruff","slug":"expected-statement-to-be-embedded-in-a-function-de","errorCode":null,"errorMessage":"Expected statement to be embedded in a function definition","messagePattern":"Expected statement to be embedded in a function definition","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ruff_linter/src/rules/flake8_pytest_style/rules/assertion.rs","lineNumber":759,"sourceCode":"    let module_text = if outer_indent.is_empty() {\n        Cow::Borrowed(contents)\n    } else {\n        Cow::Owned(format!(\n            \"def f():{}{contents}\",\n            stylist.line_ending().as_str()\n        ))\n    };\n\n    // Parse the CST.\n    let mut tree = match_module(&module_text)?;\n\n    // Extract the assert statement.\n    let statements = if outer_indent.is_empty() {\n        &mut tree.body\n    } else {\n        let [Statement::Compound(CompoundStatement::FunctionDef(embedding))] = &mut *tree.body\n        else {\n            bail!(\"Expected statement to be embedded in a function definition\")\n        };\n\n        let indented_block = match_indented_block(&mut embedding.body)?;\n        indented_block.indent = Some(outer_indent);\n\n        &mut indented_block.body\n    };\n\n    let [Statement::Simple(simple_statement_line)] = statements.as_slice() else {\n        bail!(\"Expected one simple statement\")\n    };\n\n    let [SmallStatement::Assert(assert_statement)] = simple_statement_line.body.as_slice() else {\n        bail!(\"Expected simple statement to be an assert\")\n    };\n\n    // Extract the individual conditions.\n    let mut conditions: Vec<Expression> = Vec::with_capacity(2);","sourceCodeStart":741,"sourceCodeEnd":777,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/rules/flake8_pytest_style/rules/assertion.rs#L741-L777","documentation":"An anyhow bail! in the PT018 composite-condition fixer: after re-parsing the reconstructed module, the expected wrapping function definition was not found, so the rewritten assert statement cannot be extracted. The fix is abandoned rather than emitting broken code.","triggerScenarios":"Thrown at crates/ruff_linter/src/rules/flake8_pytest_style/rules/assertion.rs:759 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Verify the module reconstruction logic that embeds the statement in a function","Skip the fix when the re-parsed tree does not have the expected shape"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"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"}