Skip to content

iter_on_empty_collections's suggestion causes a compilation error #12807

Closed
@its-the-shrimp

Description

@its-the-shrimp

Summary

The lint clippy::iter_on_empty_collections suggests to change [].iter() to std::iter::empty() even when the [].iter() is used as a fallback value for an iterator over a slice.

Lint Name

iter_on_empty_collections

Reproducer

I tried this code:

#![warn(clippy::iter_on_empty_collections)]

fn main() {
    let smth = Some(vec![1, 2, 3]);

    for i in smth.as_ref().map_or([].iter(), |s| s.iter()) {
        println!("{i}");
    }
}

I saw this happen:

warning: `iter` call on an empty collection
 --> src/main.rs:6:35
  |
6 |     for i in smth.as_ref().map_or([].iter(), |s| s.iter()) {
  |                                   ^^^^^^^^^ help: try: `std::iter::empty()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#iter_on_empty_collections
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![warn(clippy::iter_on_empty_collections)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: `test-smth` (bin "test-smth") generated 1 warning

I expected to see this happen: nothing

Version

rustc 1.79.0-nightly (244da22fa 2024-04-23)
binary: rustc
commit-hash: 244da22fabd9fa677bbd0ac601a88e5ca6917526
commit-date: 2024-04-23
host: aarch64-apple-darwin
release: 1.79.0-nightly
LLVM version: 18.1.4

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions