Skip to content

Suggestion for fixing from_iter_instead_of_collect results in build error #7259

Description

@cmichi

I tried this code:

use std::iter::FromIterator;

struct Foo(Vec<bool>);

impl FromIterator<bool> for Foo {
    fn from_iter<T: IntoIterator<Item = bool>>(iter: T) -> Self {
        let mut v = Foo(Vec::new());
        v.0.extend(iter);
        v
    }
}

impl<'a> FromIterator<&'a bool> for Foo {
    fn from_iter<T: IntoIterator<Item = &'a bool>>(iter: T) -> Self {
        <Self as FromIterator<bool>>::from_iter(iter.into_iter().copied())
    }
}

fn main() {}
$ cargo clippy
…
warning: usage of `FromIterator::from_iter`
  --> src/main.rs:15:9
   |
15 |         <Self as FromIterator<bool>>::from_iter(iter.into_iter().copied())
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `.collect()` instead of `::from_iter()`: `iter.into_iter().copied().collect::<<Self as FromIterator<bool>>>()`
   |
   = 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

warning: 1 warning emitted
…

I expected to see this happen:
When applying the suggestion the clippy warning should vanish and cargo build still execute without failures.

Instead, this happened:

$ cargo build
…
error: unmatched angle bracket
  --> src/main.rs:16:40                                                                                                                                        
   |                                                                                                                                                           
16 |                 iter.into_iter().copied().collect::<<Self as FromIterator<bool>>>()
   |                                                    ^ help: remove extra angle bracket
                                       
error: expected one of `,` or `>`, found keyword `as`
  --> src/main.rs:16:47                                                                                                                                        
   |                                                                                                                                                           
16 |                 iter.into_iter().copied().collect::<<Self as FromIterator<bool>>>()
   |                                                           ^^ expected one of `,` or `>`
   |
help: expressions must be enclosed in braces to be used as const generic arguments          
   |                                                                                                                                                           
16 |                 iter.into_iter().copied().collect::<<{ Self as FromIterator<bool }>>>()
   |                                                      ^                           ^
                                       
error: unmatched angle brackets
  --> src/main.rs:16:68                                                                                                                                        
   |                                                                                                                                                           
16 |                 iter.into_iter().copied().collect::<<Self as FromIterator<bool>>>()
   |                                                                                ^^ help: remove extra angle brackets
                                       
error[E0747]: constant provided when a type was expected
  --> src/main.rs:16:42                                                                                                                                        
   |                                                                                                                                                           
16 |                 iter.into_iter().copied().collect::<<Self as FromIterator<bool>>>()
   |                                                      ^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 4 previous errors 

Meta

  • cargo clippy -V: clippy 0.1.54 (40d2302 2021-05-20)
  • rustc -Vv:
rustc 1.54.0-nightly (40d230204 2021-05-20)
binary: rustc
commit-hash: 40d23020470db06903589e210c83a4936f22d52a
commit-date: 2021-05-20
host: x86_64-unknown-linux-gnu
release: 1.54.0-nightly
LLVM version: 12.0.1

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