Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
matthiaskrgr opened this issue Jun 5, 2025 · 2 comments · May be fixed by #15013
Open

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

matthiaskrgr opened this issue Jun 5, 2025 · 2 comments · May be fixed by #15013
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Jun 5, 2025

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

@matthiaskrgr
Copy link
Member Author

may be the same problem as here:

use std::option::IntoIter;

fn main() {
    const C: fn(IntoIter<i32>) -> IntoIter<i32> = <IntoIter<i32> as IntoIterator>::into_iter;
    C(Some(5).into_iter()); // ICE here
}

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Jun 6, 2025
@profetia
Copy link
Contributor

profetia commented Jun 6, 2025

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants