astral-sh/ruff · error · syn::Error

expected body to end in an expression

Error message

expected body to end in an expression

What it means

A syn compile error from the map_codes proc macro: the annotated function's body does not end in an expression statement. The macro parses the final Some(match (..) { .. }) expression to build the rule-code table, so a body ending otherwise cannot be processed.

Source

Thrown at crates/ruff_macros/src/map_codes.rs:41

    code: Option<LinterCode>,
    /// The path to the struct implementing the rule, e.g.
    /// `rules::pycodestyle::rules::logical_lines::NoIndentedBlock`
    path: Path,
    /// The rule attributes, e.g. for feature gates
    attrs: Vec<Attribute>,
}

#[derive(Clone)]
struct LinterCode {
    /// The linter associated with the rule, e.g., `Pycodestyle`.
    linter: Ident,
    /// The code associated with the rule, e.g., `"E112"`.
    code: LitStr,
}

pub(crate) fn map_codes(func: &ItemFn) -> syn::Result<TokenStream> {
    let Some(last_stmt) = func.block.stmts.last() else {
        return Err(Error::new(
            func.block.span(),
            "expected body to end in an expression",
        ));
    };
    let Stmt::Expr(
        Expr::Call(ExprCall {
            args: some_args, ..
        }),
        _,
    ) = last_stmt
    else {
        return Err(Error::new(
            last_stmt.span(),
            "expected last expression to be `Some(match (..) { .. })`",
        ));
    };
    let mut some_args = some_args.into_iter();
    let (Some(Expr::Match(ExprMatch { arms, .. })), None) = (some_args.next(), some_args.next())

View on GitHub (pinned to 26f38c119c)

Solutions

  1. Make the last statement of the function the Some(match ...) expression
  2. Restructure the mapper function so it returns the match expression directly
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/ruff_macros/src/map_codes.rs:41 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of astral-sh/ruff@26f38c119c (2026-09-05). Data as JSON: /api/errors/fb5c3379db018b1c. Report an issue: GitHub.