actix/actix-web · error · syn::Error

The #[routes] macro requires at least one `#[<method>(..)]`

Error message

The #[routes] macro requires at least one `#[<method>(..)]` attribute.

What it means

The `#[routes]` macro aggregates multiple method attributes on one handler. At actix-web-codegen/src/route.rs:518-540 it scans the function's attributes for recognized method paths (`get`, `post`, ...) and errors if none are found. `#[routes]` with no inner method attribute is meaningless.

Source

Thrown at actix-web-codegen/src/route.rs:536

    for attr in std::mem::take(&mut ast.attrs) {
        match MethodType::from_path(attr.path()) {
            Ok(method) => methods.push((method, attr)),
            Err(_) => ast.attrs.push(attr),
        }
    }

    let methods = match methods
        .into_iter()
        .map(|(method, attr)| {
            attr.parse_args()
                .and_then(|args| Args::new(args, Some(method)))
        })
        .collect::<Result<Vec<_>, _>>()
    {
        Ok(methods) if methods.is_empty() => {
            return input_and_compile_error(
                input,
                syn::Error::new(
                    Span::call_site(),
                    "The #[routes] macro requires at least one `#[<method>(..)]` attribute.",
                ),
            )
        }
        Ok(methods) => methods,
        Err(err) => return input_and_compile_error(input, err),
    };

    match Route::multiple(methods, ast) {
        Ok(route) => route.into_token_stream().into(),
        // on macro related error, make IDEs happy; see fn docs
        Err(err) => input_and_compile_error(input, err),
    }
}

View on GitHub (pinned to 937960ca67)

Solutions

  1. Add at least one method attribute under `#[routes]`, e.g. `#[get("/x")]`.
  2. If only one method/path is needed, drop `#[routes]` and use the single method macro directly.

Example fix

// before
#[routes]
async fn handler() -> HttpResponse { ... }

// after
#[routes]
#[get("/items")]
#[post("/items")]
async fn handler() -> HttpResponse { ... }
Defensive patterns

Strategy: validation

Validate before calling

// Compile-time only. When using #[routes], ensure at least one inner
// method attribute (#[get("...")], #[post("...")], ...) is present.

Prevention

When it happens

Trigger: `#[routes]` decorating a function that has no `#[get(...)]`/`#[post(...)]`/etc. attributes underneath it.

Common situations: Adding `#[routes]` first intending to fill in methods later, or accidentally deleting the inner method attributes.

Related errors


AI-assisted analysis of actix/actix-web@937960ca67 (2026-08-06). Data as JSON: /data/errors/ac03d9ff8296fe2f.json. Report an issue: GitHub.