{"record":{"id":"657cca5387a2ec86","repo":"diesel-rs/diesel","slug":"last-statement-should-be-an-expression-for-auto-ty","errorCode":null,"errorMessage":"last statement should be an expression for auto_type","messagePattern":"last statement should be an expression for auto_type","errorType":"validation","errorClass":"syn::Error","httpStatus":null,"severity":"error","filePath":"dsl_auto_type/src/auto_type/mod.rs","lineNumber":162,"sourceCode":"                        Ok(()) => {}\n                        Err(e) => errors.push(Rc::new(e)),\n                    }\n                };\n            }\n\n            // Add local variables from the function body, and finally infer the type\n            local_variables_map.infer_block_expression_type(\n                return_expression,\n                Some(&return_type),\n                &input_function.block,\n                &mut errors,\n            )\n        }\n        _ => {\n            // This error message is not strictly correct: we also support\n            // partially-specified return types that involve `_`, but for simplicity we just\n            // put the overwhelmingly most common case in this error message\n            return Err(syn::Error::new(\n                input_function.sig.output.span(),\n                \"Function return type should be explicitly specified as `-> _` for auto_type\",\n            )\n            .into());\n        }\n    };\n\n    let type_alias = match type_alias {\n        Some(type_alias) => {\n            // We're generating a type alias so we need to extract the necessary lifetimes and\n            // generic type parameters for that type alias\n            let type_alias_generics = referenced_generics::extract_referenced_generics(\n                &return_type,\n                &input_function.sig.generics,\n                &mut errors,\n            );\n\n            let vis = &input_function.vis;","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/diesel-rs/diesel/blob/6fa6ed01b24b24248ab2a611698d0a7c6a2e9120/dsl_auto_type/src/auto_type/mod.rs#L144-L180","documentation":"After locating the last statement, auto_type requires it to be an expression (or a bare `return expr;`) because the macro infers the function's `-> _` return type from that final expression. Any other trailing statement (a let binding, an item, an empty `return;`, a statement with a semicolon producing ()) triggers this compile error.","triggerScenarios":"Function body ending in e.g. `let x = 1;` as the last statement, or `return;` with no value, under #[auto_type].","commonSituations":"Writing a function in statement style then annotating it with auto_type; forgetting that a trailing semicolon turns the expression into a statement; early-refactor leftovers.","solutions":["Make the last statement a plain expression without a trailing semicolon: end with `value` instead of `value;`.","Convert a final `return expr;` into a trailing expression, or keep `return expr;` (ExprReturn with expr) which is supported.","Move any trailing let/item statements before the final expression."],"exampleFix":"// before\n#[auto_type]\nfn f() -> _ {\n    let x = 1;\n}\n// after\n#[auto_type]\nfn f() -> _ {\n    let x = 1;\n    x\n}","handlingStrategy":"validation","validationCode":"// Ensure the body ends with an expression (no trailing semicolon on last stmt):\nfn ends_with_expr(stmts: &[syn::Stmt]) -> bool {\n    matches!(stmts.last(), Some(syn::Stmt::Expr(_, None)) | Some(syn::Stmt::Expr(syn::Expr::Return(_), _)))\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["End auto_type functions with a bare expression.","Avoid trailing semicolons on the final value."],"tags":["rust","proc-macro","statements","compile-time"],"backgroundTag":"unsupported-operation","analyzedSha":"6fa6ed01b24b24248ab2a611698d0a7c6a2e9120","analyzedAt":"2026-09-07T01:50:13.074Z","contentChangedAt":"2026-09-07T01:50:13.074Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}