Skip to content

vec_init_then_push inside macro causes syntax error #16131

@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::vec_init_then_push

this code:

//@ run-pass
#![allow(unused_variables)]


macro_rules! vec [
    ($($e:expr),*) => ({
        let mut _temp = ::std::vec::Vec::new();
        $(_temp.push($e);)*
        _temp
    })
];

pub fn main() {
    let my_vec = vec![1, 2, 3, 4, 5];
}

caused the following diagnostics:

    Checking _vec-macro-with-brackets v0.1.0 (/tmp/icemaker_global_tempdir.gOhJUmIOFvoe/icemaker_clippyfix_tempdir.qAMRf9whdBu6/_vec-macro-with-brackets)
warning: calls to `push` immediately after creation
  --> src/main.rs:7:9
   |
 7 | /         let mut _temp = ::std::vec::Vec::new();
 8 | |         $(_temp.push($e);)*
   | |_________________________^ help: consider using the `vec![]` macro: `let _temp = vec![..];`
...
14 |       let my_vec = vec![1, 2, 3, 4, 5];
   |                    ------------------- in this macro invocation
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#vec_init_then_push
   = note: requested on the command line with `--force-warn clippy::vec-init-then-push`
   = note: this warning originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: `_vec-macro-with-brackets` (bin "_vec-macro-with-brackets") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.25s

However after applying these diagnostics, the resulting code:

//@ run-pass
#![allow(unused_variables)]


macro_rules! vec [
    ($($e:expr),*) => ({
        let _temp = vec![..];)*
        _temp
    })
];

pub fn main() {
    let my_vec = vec![1, 2, 3, 4, 5];
}

no longer compiled:

    Checking _vec-macro-with-brackets v0.1.0 (/tmp/icemaker_global_tempdir.gOhJUmIOFvoe/icemaker_clippyfix_tempdir.qAMRf9whdBu6/_vec-macro-with-brackets)
error: mismatched closing delimiter: `}`
 --> src/main.rs:5:18
  |
5 | macro_rules! vec [
  |                  ^ unclosed delimiter
...
9 |     })
  |     ^ mismatched closing delimiter

error: unexpected closing delimiter: `)`
 --> src/main.rs:9:6
  |
6 |     ($($e:expr),*) => ({
  |                        - the nearest open delimiter
7 |         let _temp = vec![..];)*
  |                              - missing open `(` for this delimiter
8 |         _temp
9 |     })
  |      ^ unexpected closing delimiter

error: could not compile `_vec-macro-with-brackets` (bin "_vec-macro-with-brackets" test) due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
error: could not compile `_vec-macro-with-brackets` (bin "_vec-macro-with-brackets") due to 2 previous errors

Version:

rustc 1.93.0-nightly (4b1b6dde0 2025-11-22)
binary: rustc
commit-hash: 4b1b6dde0c15243857165672a1c616a4523ef32a
commit-date: 2025-11-22
host: x86_64-unknown-linux-gnu
release: 1.93.0-nightly
LLVM version: 21.1.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedT-macrosType: Issues with macros and macro expansion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions