Skip to content

std::iter::empty() eats type annotation #18083

Open

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::iter-on-empty-collections

this code:

fn main() {
    (&mut None::<()>.into_iter());
}

fn rec<T: Iterator>(mut it: T) {
    if true {
        it.next();
    } else {
        rec(&mut it);
    }
}

caused the following diagnostics:

    Checking crate-1794796175612746558_34579609790765377663768504026046482798 v0.1.0 (/tmp/icemaker_global_tempdir.Lceei48FaLov/icemaker_clippyfix_tempdir.FVkMq2awxT0v/crate-1794796175612746558_34579609790765377663768504026046482798)
warning: `into_iter` call on an empty collection
 --> src/main.rs:2:11
  |
2 |     (&mut None::<()>.into_iter());
  |           ^^^^^^^^^^^^^^^^^^^^^^ help: try: `std::iter::empty()`
  |
  = help: for further information visit https://clippy.rust-lang.org/main/index.html#iter_on_empty_collections
  = note: requested on the command line with `--force-warn clippy::iter-on-empty-collections`

warning: `crate-1794796175612746558_34579609790765377663768504026046482798` (bin "crate-1794796175612746558_34579609790765377663768504026046482798") generated 1 warning
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.24s

However after applying these diagnostics, the resulting code:

fn main() {
    (&mut std::iter::empty::</* Type */>());
}

fn rec<T: Iterator>(mut it: T) {
    if true {
        it.next();
    } else {
        rec(&mut it);
    }
}

no longer compiled:

    Checking crate-1794796175612746558_34579609790765377663768504026046482798 v0.1.0 (/tmp/icemaker_global_tempdir.Lceei48FaLov/icemaker_clippyfix_tempdir.FVkMq2awxT0v/crate-1794796175612746558_34579609790765377663768504026046482798)
error: errors present after applying fixes to crate `crate_1794796175612746558_34579609790765377663768504026046482798`
 --> src/main.rs
  = cause: error[E0282]: type annotations needed
            --> src/main.rs:2:11
             |
           2 |     (&mut std::iter::empty::</* Type */>());
             |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `empty`
           
           
           error: aborting due to 1 previous error
           
           
           For more information about this error, try `rustc --explain E0282`.
help: to report this as a bug, open an issue at https://github.com/rust-lang/rust-clippy/issues, quoting the full output of this command
help: to possibly apply more fixes, pass in the `--broken-code` flag
note: original diagnostics will follow:
error[E0282]: type annotations needed
 --> src/main.rs:2:11
  |
2 |     (&mut std::iter::empty::</* Type */>());
  |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `empty`

For more information about this error, try `rustc --explain E0282`.
error: could not compile `crate-1794796175612746558_34579609790765377663768504026046482798` (bin "crate-1794796175612746558_34579609790765377663768504026046482798") due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: errors present after applying fixes to crate `crate_1794796175612746558_34579609790765377663768504026046482798`
 --> src/main.rs
  = cause: error[E0282]: type annotations needed
            --> src/main.rs:2:11
             |
           2 |     (&mut std::iter::empty::</* Type */>());
             |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `T` declared on the function `empty`
           
           
           error: aborting due to 1 previous error
           
           
           For more information about this error, try `rustc --explain E0282`.
help: to report this as a bug, open an issue at https://github.com/rust-lang/rust-clippy/issues, quoting the full output of this command
help: to possibly apply more fixes, pass in the `--broken-code` flag
note: original diagnostics will follow:
error: could not compile `crate-1794796175612746558_34579609790765377663768504026046482798` (bin "crate-1794796175612746558_34579609790765377663768504026046482798" test) due to 1 previous error

Version:

rustc 1.102.0-nightly (c99bff8cd 2026-11-16)
binary: rustc
commit-hash: c99bff8cd92cfc7545121db13506095828082138
commit-date: 2026-11-16
host: x86_64-unknown-linux-gnu
release: 1.102.0-nightly
LLVM version: 23.1.5

Activity

  1. added
    C-bug
    I-suggestion-causes-error
    on Nov 17, 2026
  2. matthiaskrgr commented on Nov 17, 2026

    @matthiaskrgr
    MemberAuthor

    maybe we can figure out the type here and then suggest a std::iter::empty::<T>() instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugI-suggestion-causes-error

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions