Skip to content
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

iter_on_empty_collections's suggestion causes a compilation error #12807

Closed
schvv31n opened this issue May 16, 2024 · 1 comment · Fixed by #12823
Closed

iter_on_empty_collections's suggestion causes a compilation error #12807

schvv31n opened this issue May 16, 2024 · 1 comment · Fixed by #12823
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@schvv31n
Copy link
Contributor

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

@schvv31n schvv31n added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels May 16, 2024
@Centri3
Copy link
Member

Centri3 commented May 16, 2024

Can probably use expr_use_ctxt here...? docs But I'm unsure tbh and it really should detect this, maybe be considered a FP/issue in that utils function?

Related PR I think since it fixed some FNs and FPs, closed for inactivity though: #11038

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-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
2 participants