Skip to content

#[derive(OpenApi)] generates code that triggers clippy::needless_for_each warning #1420

@dnehring7

Description

@dnehring7

Description

The #[derive(OpenApi)] macro generates code that triggers a clippy warning for needless_for_each, which cannot be suppressed at the derive site and requires a module-level #![allow(clippy::needless_for_each)] to silence.

Steps to Reproduce:

  1. Create a struct with #[derive(OpenApi)]
  2. Run cargo clippy with pedantic lints enabled

Example Code:

use utoipa::OpenApi;

#[derive(OpenApi)]
#[openapi(
    paths(),
    components(),
    tags((name = "api", description = "API")),
    info(title = "Test API", version = "1.0.0")
)]
struct ApiDoc;

Expected Behavior:
No clippy warnings should be generated from derive macro code.

Actual Behavior:

warning: needless use of `for_each`
  --> src/main.rs:XX:XX
   |
XX | #[derive(OpenApi)]
   |          ^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_for_each
   = note: this warning originates in the derive macro `OpenApi`

Environment:

utoipa version: 5.4.0
Rust version: nightly (rustc 1.90.0-nightly (28f1c8079 2025-06-24))
Clippy: nightly (rustc 1.90.0-nightly (28f1c8079 2025-06-24))

Workaround:
Currently requires adding #![allow(clippy::needless_for_each)] at the module level.

Suggested Fix:
Use a for loop instead of for_each where appropriate

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions