Skip to content

useless conversion wrongly suggests removing .into_iter() when ExactSizeIterator is required #11300

@asomers

Description

@asomers

Summary

I have a function that takes an argument implementing IntoIterator + ExactSizeIterator. When I call it, I supply an array with .into_iter(). Clippy wrongly suggests removing the .into_iter() on the grounds that it's automatic. However, if I do that then the code fails to compile because ExactSizeIterator is not satisfied.

Lint Name

useless_conversion

Reproducer

I tried this code:

pub fn foo<I>(i: I)
where I: IntoIterator<Item=i32> + ExactSizeIterator
{
    assert_eq!(i.len(), 3);
}

pub fn bar() {
    foo([1, 2, 3].into_iter());
}

I saw this happen:

warning: explicit call to `.into_iter()` in function argument accepting `IntoIterator`
 --> src/lib.rs:8:9
  |
8 |     foo([1, 2, 3].into_iter());
  |         ^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `[1, 2, 3]`
  |
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
 --> src/lib.rs:2:10
  |
2 | where I: IntoIterator<Item=i32> + ExactSizeIterator
  |          ^^^^^^^^^^^^^^^^^^^^^^
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion
  = note: `#[warn(clippy::useless_conversion)]` on by default

I expected to see this happen:
No errors.

If I apply Clippy's suggest and remove the .into_iter(), then this happens:

error[[E0277]](https://doc.rust-lang.org/stable/error_codes/E0277.html): the trait bound `[i32; 3]: ExactSizeIterator` is not satisfied
 --> src/lib.rs:8:9
  |
8 |     foo([1, 2, 3]);
  |     --- ^^^^^^^^^ the trait `ExactSizeIterator` is not implemented for `[i32; 3]`
  |     |
  |     required by a bound introduced by this call
  |
  = help: the following other types implement trait `ExactSizeIterator`:
            &mut I
            Args
            ArgsOs
            ArrayChunksMut<'_, T, N>
            ArrayWindows<'_, T, N>
            Box<I, A>
            Chunks<'_, T>
            ChunksExact<'_, T>
          and 109 others
note: required by a bound in `foo`
 --> src/lib.rs:2:35
  |
1 | pub fn foo<I>(i: I)
  |        --- required by a bound in this function
2 | where I: IntoIterator<Item=i32> + ExactSizeIterator
  |                                   ^^^^^^^^^^^^^^^^^ required by this bound in `foo`

Version

rustc 1.73.0-nightly (399b06823 2023-07-20)
binary: rustc
commit-hash: 399b068235ceea440540539b3bfd1aeb82214a28
commit-date: 2023-07-20
host: x86_64-unknown-freebsd
release: 1.73.0-nightly
LLVM version: 16.0.5

Additional Labels

@rustbot label +I-suggestion-causes-error

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 haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions