Skip to content

needless-for-each expands macros in suggestion #15256

Open
@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::needless-for-each

this code:

use std::collections::*;

fn main() {
    let vec: Vec<i32> = Vec::new();
    vec.iter().for_each(|v| println!("{}", v));
}

caused the following diagnostics:

    Checking _a v0.1.0 (/tmp/icemaker_global_tempdir.wwIYilbqcRrS/icemaker_clippyfix_tempdir.9qZ6jSOOassq/_a)
warning: needless use of `for_each`
 --> src/main.rs:5:5
  |
5 |     vec.iter().for_each(|v| println!("{}", v));
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_for_each
  = note: requested on the command line with `--force-warn clippy::needless-for-each`
help: try
  |
5 ~     for v in vec.iter() {
6 +         $crate::io::_print($crate::format_args_nl!($($arg)*));
7 +     }
  |

warning: `_a` (bin "_a") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.35s

However after applying these diagnostics, the resulting code:

use std::collections::*;

fn main() {
    let vec: Vec<i32> = Vec::new();
    for v in vec.iter() {
        $crate::io::_print($crate::format_args_nl!($($arg)*));
    }
}

no longer compiled:

    Checking _a v0.1.0 (/tmp/icemaker_global_tempdir.wwIYilbqcRrS/icemaker_clippyfix_tempdir.9qZ6jSOOassq/_a)
error: expected expression, found `$`
 --> src/main.rs:6:9
  |
6 |         $crate::io::_print($crate::format_args_nl!($($arg)*));
  |         ^ expected expression

error: could not compile `_a` (bin "_a" test) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_a` (bin "_a") due to 1 previous error

Version:

rustc 1.90.0-nightly (9535feebd 2025-07-12)
binary: rustc
commit-hash: 9535feebd5741a55fc24e84060e82d41a75dac6e
commit-date: 2025-07-12
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.7

Metadata

Metadata

Assignees

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