Skip to content

from_iter_instead_of_collect feels like a blanket ban of the FromIterator trait #7213

Description

@Nemo157

I tried this code:

Vec::from_iter(2..5)

I expected to see this happen: no warning

Instead, this happened:

warning: usage of `FromIterator::from_iter`
   --> src/lib.rs:219:1
    |
219 | Vec::from_iter(2..5);
    | ^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `(2..5).collect::<Vec<_>>()`
    |
    = note: `#[warn(clippy::from_iter_instead_of_collect)]` on by default
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#from_iter_instead_of_collect

The Vec::from_iter call is much more succinct and readable than .collect::<Vec<_>>(), and rust-lang/rfcs#3114 is currently in FCP-disposition-merge to add std::iter::FromIterator to the Rust 2021 prelude. Having Clippy recommend against using it seems to go against the decision there that it is a trait to be used directly.

I'm really not sure if there's some subset of this lint that would feel ok, there's maybe some sort of "collect here would be more readable", but defining heuristics to detect when to enable that seems complicated.

Meta

  • cargo clippy -V: clippy 0.1.53 (42816d6 2021-04-24)

Activity

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-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions