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

expected last expression to be `Some(match (..) { .. })`

Error message

expected last expression to be `Some(match (..) { .. })`

What it means

A syn compile error from the map_codes proc macro: the function's final expression is not a Some(match (..) { .. }) call as expected. The macro extracts rule-code pairs from that exact shape, so any other tail expression is rejected at compile time.

Source

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

    /// 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())
    else {
        return Err(Error::new(
            last_stmt.span(),
            "expected last expression to be `Some(match (..) { .. })`",
        ));
    };

    let mut rules = Vec::new();
    for arm in arms {
        if matches!(arm.pat, Pat::Wild(..)) {
            break;
        }

View on GitHub (pinned to 26f38c119c)

Solutions

  1. Ensure the function ends with Some(match (...) { ... }) over rule pairs
  2. Move auxiliary code before the final Some(match ...) expression
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/ruff_macros/src/map_codes.rs:53 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/e44a79968f1b6a13. Report an issue: GitHub.