Skip to content
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

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

Closed
Patryk27 opened this issue Nov 8, 2023 · 2 comments
Closed

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

Patryk27 opened this issue Nov 8, 2023 · 2 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Patryk27
Copy link
Contributor

Patryk27 commented Nov 8, 2023

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

@Patryk27 Patryk27 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 8, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 8, 2023
@saethlin saethlin added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 9, 2023
@d-sonuga
Copy link
Contributor

d-sonuga commented Jan 3, 2024

@rustbot claim

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Feb 16, 2024
…r-errors

suggest `into_iter()` when `Iterator` method called on `impl IntoIterator`

Fix for issue rust-lang#117711.
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 16, 2024
Rollup merge of rust-lang#119928 - d-sonuga:into-iter-sugg, r=compiler-errors

suggest `into_iter()` when `Iterator` method called on `impl IntoIterator`

Fix for issue rust-lang#117711.
@deltragon
Copy link
Contributor

I believe this can be closed as fixed by #119928

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants