Skip to content

iter-on-single-items breaks code in macro context that takes IntoIterator #14981

Open
@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::iter-on-single-items

this code:

use std::option::IntoIter;
fn takes_into_iter(_: impl IntoIterator<Item = i32>) {}

macro_rules! x {
    ($e:expr) => {
        takes_into_iter($e); // lint here because `.into_iter()` is "redundant"
        let _: IntoIter<i32> = $e; // removing `.into_iter()` leads to a type error here
    };
}

fn main() {
    x!(Some(5).into_iter());
}

caused the following diagnostics:

    Checking _clippy11065-3 v0.1.0 (/tmp/icemaker_global_tempdir.t8dYaH8PqaqW/icemaker_clippyfix_tempdir.S1c809nLp6vL/_clippy11065-3)
warning: `into_iter` call on a collection with only one item
  --> src/main.rs:12:8
   |
12 |     x!(Some(5).into_iter());
   |        ^^^^^^^^^^^^^^^^^^^ help: try: `std::iter::once(5)`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#iter_on_single_items
   = note: requested on the command line with `--force-warn clippy::iter-on-single-items`

warning: `_clippy11065-3` (bin "_clippy11065-3") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.11s

However after applying these diagnostics, the resulting code:

use std::option::IntoIter;
fn takes_into_iter(_: impl IntoIterator<Item = i32>) {}

macro_rules! x {
    ($e:expr) => {
        takes_into_iter($e); // lint here because `.into_iter()` is "redundant"
        let _: IntoIter<i32> = $e; // removing `.into_iter()` leads to a type error here
    };
}

fn main() {
    x!(std::iter::once(5));
}

no longer compiled:

    Checking _clippy11065-3 v0.1.0 (/tmp/icemaker_global_tempdir.t8dYaH8PqaqW/icemaker_clippyfix_tempdir.S1c809nLp6vL/_clippy11065-3)
error[E0308]: mismatched types
  --> src/main.rs:12:8
   |
7  |         let _: IntoIter<i32> = $e; // removing `.into_iter()` leads to a type error here
   |                ------------- expected due to this
...
12 |     x!(std::iter::once(5));
   |        ^^^^^^^^^^^^^^^^^^ expected `IntoIter<i32>`, found `Once<{integer}>`
   |
   = note: expected struct `std::option::IntoIter<i32>`
              found struct `std::iter::Once<{integer}>`

For more information about this error, try `rustc --explain E0308`.
error: could not compile `_clippy11065-3` (bin "_clippy11065-3") due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_clippy11065-3` (bin "_clippy11065-3" test) due to 1 previous error

Version:

rustc 1.89.0-nightly (076ec59ff 2025-06-05)
binary: rustc
commit-hash: 076ec59ff1dcf538b9d3a0b8e0d7f4edd0559959
commit-date: 2025-06-05
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

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 applied

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions