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

Detect iterators going out of scope and suggest collecting #81584

Closed
estebank opened this issue Jan 31, 2021 · 4 comments · Fixed by #81939
Closed

Detect iterators going out of scope and suggest collecting #81584

estebank opened this issue Jan 31, 2021 · 4 comments · Fixed by #81939
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-iterators Area: Iterators A-lifetimes Area: lifetime related A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Jan 31, 2021

Given a situation where an iterator is constructed inside of a scope where the data is being returned, we currently emit:

error[E0515]: cannot return value referencing function parameter `y`
 --> src/main.rs:4:18
  |
4 |         .map(|y| y.iter().map(|x| x + 1))
  |                  -^^^^^^^^^^^^^^^^^^^^^^
  |                  |
  |                  returns a value referencing data owned by the current function
  |                  `y` is borrowed here

We should suggest using .collect() in these cases.

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: lifetime related P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Jan 31, 2021
@leonardo-m
Copy link

Or flatten/flat_map?

@estebank
Copy link
Contributor Author

I want for us to detect cases of trying to operate on iterators of iterators because the only option then is to allocate or completely rework the logic. The former will help newcomers, experts will know enough to try the later.

@camelid camelid added A-iterators Area: Iterators C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Feb 1, 2021
@kper
Copy link
Contributor

kper commented Feb 9, 2021

@rustbot claim

@the8472
Copy link
Member

the8472 commented Feb 9, 2021

Couldn't we still recommend both? Recommending bad practices seems suboptimal.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Mar 5, 2021
… r=davidtwco

Add suggestion `.collect()` for iterators in iterators

Closes rust-lang#81584

```
error[E0515]: cannot return value referencing function parameter `y`
 --> main3.rs:4:38
  |
4 | ...                   .map(|y| y.iter().map(|x| x + 1))
  |                                -^^^^^^^^^^^^^^^^^^^^^^
  |                                |
  |                                returns a value referencing data owned by the current function
  |                                `y` is borrowed here
  |                                help: Maybe use `.collect()` to allocate the iterator
```

Added the suggestion: `help: Maybe use `.collect()` to allocate the iterator`
m-ou-se added a commit to m-ou-se/rust that referenced this issue Mar 5, 2021
… r=davidtwco

Add suggestion `.collect()` for iterators in iterators

Closes rust-lang#81584

```
error[E0515]: cannot return value referencing function parameter `y`
 --> main3.rs:4:38
  |
4 | ...                   .map(|y| y.iter().map(|x| x + 1))
  |                                -^^^^^^^^^^^^^^^^^^^^^^
  |                                |
  |                                returns a value referencing data owned by the current function
  |                                `y` is borrowed here
  |                                help: Maybe use `.collect()` to allocate the iterator
```

Added the suggestion: `help: Maybe use `.collect()` to allocate the iterator`
@bors bors closed this as completed in 20887b7 Mar 5, 2021
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-iterators Area: Iterators A-lifetimes Area: lifetime related A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants