Skip to content

iter_cloned_collect false positive with custom From/IntoIterator impl #9119

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

Closed
Dentosal opened this issue Jul 4, 2022 · 1 comment · Fixed by #14473
Closed

iter_cloned_collect false positive with custom From/IntoIterator impl #9119

Dentosal opened this issue Jul 4, 2022 · 1 comment · Fixed by #14473
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

@Dentosal
Copy link

Dentosal commented Jul 4, 2022

Summary

When FromIterator is implemented for a type inside an another iterator, .collect() is able to do run the inner iterator as well. Clippy doesn't recognize this case, and instead assumes that .iter().cloned().collect() to a Vec<_> always has the same resulting type than to_vec(). This also occurs with .iter().copied().collect().

Lint Name

iter_cloned_collect and iter_cloned_collect

Reproducer

I tried this code:

use std::iter;

#[derive(Clone)]
struct Example(u16);

impl iter::FromIterator<Example> for Vec<u8> {
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = Example>,
    {
        iter.into_iter().flat_map(|e| e.0.to_le_bytes()).collect()
    }
}

fn main() {
    let examples = vec![Example(1), Example(0x1234)];
    let encoded: Vec<u8> = examples.iter().cloned().collect();
    assert_eq!(encoded, vec![0x01, 0x00, 0x34, 0x12]);
}

I saw this happen:

warning: called `iter().copied().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and more readable
  --> src/main.rs:17:36
   |
17 |     let encoded: Vec<u8> = examples.iter().copied().collect();
   |                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.to_vec()`
   |
   = note: `#[warn(clippy::iter_cloned_collect)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#iter_cloned_collect

I expected to see this happen:

No warning, as the to_vec cannot do the required conversion.

Version

rustc 1.62.0 (a8314ef7d 2022-06-27)
binary: rustc
commit-hash: a8314ef7d0ec7b75c336af2c9857bfaf43002bfc
commit-date: 2022-06-27
host: x86_64-unknown-linux-gnu
release: 1.62.0
LLVM version: 14.0.5

Additional Labels

@rustbot label +l-suggestion-causes-error

@Dentosal Dentosal 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 Jul 4, 2022
@rustbot
Copy link
Collaborator

rustbot commented Jul 4, 2022

Error: Label l-suggestion-causes-error can only be set by Rust team members

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

github-merge-queue bot pushed a commit that referenced this issue Apr 8, 2025
…14473)

Closes #9119

changelog: [`iter_cloned_collect`]: fix FP with custom
`From`/`IntoIterator` impl
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
Development

Successfully merging a pull request may close this issue.

2 participants