emit! macro triggers clippy::vec_init_then_push warning #105

Closed
opened 2025-07-21 21:49:39 -07:00 by jwilger · 0 comments
jwilger commented 2025-07-21 21:49:39 -07:00 (Migrated from github.com)

Description

The emit! macro triggers a false positive clippy warning when used with an empty vector that was just created. This forces users to add #[allow(clippy::vec_init_then_push)] annotations to their code.

Steps to Reproduce

use eventcore::{emit, require, CommandLogic, CommandResult, ReadStreams, StreamWrite};

// In a CommandLogic::handle implementation:
async fn handle(
    &self,
    _read_streams: ReadStreams<Self::StreamSet>,
    state: Self::State,
    _stream_resolver: &mut StreamResolver,
) -> CommandResult<Vec<StreamWrite<Self::StreamSet, Self::Event>>> {
    let mut events = vec![];
    
    emit!(
        events,
        &_read_streams,
        self.stream_id.clone(),
        DomainEvent::SomethingHappened { data: "test".into() }
    );
    
    Ok(events)
}

Expected Behavior

The emit! macro should not trigger clippy warnings about vector initialization patterns.

Actual Behavior

Clippy produces the following warning:

error: calls to `push` immediately after creation
   --> src/domain/commands/version_commands.rs:260:9
    |
260 | /         emit!(
261 | |             events,
262 | |             &_read_streams,
263 | |             self.version_stream.clone(),
...   |
269 | |         );
    | |_________^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#vec_init_then_push
    = note: `-D clippy::vec-init-then-push` implied by `-D warnings`
    = note: this error originates in the macro `emit` (in Nightly builds, run with -Z macro-backtrace for more info)

Analysis

The macro appears to expand to code that immediately pushes to a vector after creation, which triggers the vec_init_then_push lint. This is a false positive because:

  1. The user doesn't have control over how the macro expands
  2. The pattern makes sense in the context of building up a list of events conditionally
  3. It forces users to add #[allow(clippy::vec_init_then_push)] annotations

Suggested Fix

The macro could be updated to:

  1. Add #[allow(clippy::vec_init_then_push)] internally within the macro expansion
  2. Or restructure the expansion to avoid the pattern that triggers the lint

Workaround

Users must add #[allow(clippy::vec_init_then_push)] to methods that use the emit! macro:

#[allow(clippy::vec_init_then_push)]
async fn handle(...) -> CommandResult<Vec<StreamWrite<Self::StreamSet, Self::Event>>> {
    // ... use emit! macro
}

Environment

  • eventcore version: 0.1.7
  • clippy version: (current stable)

This issue was discovered while implementing the macros in union_square after the fix for #97: https://github.com/jwilger/union_square/pull/107

## Description The `emit!` macro triggers a false positive clippy warning when used with an empty vector that was just created. This forces users to add `#[allow(clippy::vec_init_then_push)]` annotations to their code. ## Steps to Reproduce ```rust use eventcore::{emit, require, CommandLogic, CommandResult, ReadStreams, StreamWrite}; // In a CommandLogic::handle implementation: async fn handle( &self, _read_streams: ReadStreams<Self::StreamSet>, state: Self::State, _stream_resolver: &mut StreamResolver, ) -> CommandResult<Vec<StreamWrite<Self::StreamSet, Self::Event>>> { let mut events = vec![]; emit!( events, &_read_streams, self.stream_id.clone(), DomainEvent::SomethingHappened { data: "test".into() } ); Ok(events) } ``` ## Expected Behavior The `emit!` macro should not trigger clippy warnings about vector initialization patterns. ## Actual Behavior Clippy produces the following warning: ``` error: calls to `push` immediately after creation --> src/domain/commands/version_commands.rs:260:9 | 260 | / emit!( 261 | | events, 262 | | &_read_streams, 263 | | self.version_stream.clone(), ... | 269 | | ); | |_________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#vec_init_then_push = note: `-D clippy::vec-init-then-push` implied by `-D warnings` = note: this error originates in the macro `emit` (in Nightly builds, run with -Z macro-backtrace for more info) ``` ## Analysis The macro appears to expand to code that immediately pushes to a vector after creation, which triggers the `vec_init_then_push` lint. This is a false positive because: 1. The user doesn't have control over how the macro expands 2. The pattern makes sense in the context of building up a list of events conditionally 3. It forces users to add `#[allow(clippy::vec_init_then_push)]` annotations ## Suggested Fix The macro could be updated to: 1. Add `#[allow(clippy::vec_init_then_push)]` internally within the macro expansion 2. Or restructure the expansion to avoid the pattern that triggers the lint ## Workaround Users must add `#[allow(clippy::vec_init_then_push)]` to methods that use the `emit!` macro: ```rust #[allow(clippy::vec_init_then_push)] async fn handle(...) -> CommandResult<Vec<StreamWrite<Self::StreamSet, Self::Event>>> { // ... use emit! macro } ``` ## Environment - eventcore version: 0.1.7 - clippy version: (current stable) ## Related This issue was discovered while implementing the macros in union_square after the fix for #97: https://github.com/jwilger/union_square/pull/107
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
jwilger/eventcore#105
No description provided.