Skip to content

Calling Iterator methods on IntoIterator should suggest .into_iter() #117711

Closed
@Patryk27

Description

@Patryk27

Code

fn process(items: impl IntoIterator<Item = String>) -> Vec<String> {
    items.collect()
}

Current output

error[E0599]: `impl IntoIterator<Item = String>` is not an iterator
 --> src/lib.rs:2:11
  |
1 | fn process(items: impl IntoIterator<Item = String>) -> Vec<String> {
  |                   -------------------------------- method `collect` not found for this type parameter
2 |     items.collect()
  |           ^^^^^^^ `impl IntoIterator<Item = String>` is not an iterator
  |
  = note: the following trait bounds were not satisfied:
          `impl IntoIterator<Item = String>: Iterator`
          which is required by `&mut impl IntoIterator<Item = String>: Iterator`
help: consider restricting the type parameter to satisfy the trait bound
  |
1 | fn process(items: impl IntoIterator<Item = String>) -> Vec<String> where impl IntoIterator<Item = String>: Iterator {
  |                                                                    ++++++++++++++++++++++++++++++++++++++++++++++++

Desired output

error[E0599]: `impl IntoIterator<Item = String>` is not an iterator
 --> src/lib.rs:2:11
  |
1 | fn process(items: impl IntoIterator<Item = String>) -> Vec<String> {
  |                   -------------------------------- method `collect` not found for this type parameter
2 |     items.collect()
  |           ^^^^^^^ `impl IntoIterator<Item = String>` is not an iterator
  |

help: call `.into_iter()` first
  |
2 |     items.into_iter().collect()
  |          ++++++++++++
  |

Rationale and extra context

It's an easy mistake that takes a moment to realize, so imo it would be cool to handle it better.

Other cases

No response

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions