Skip to content

Detect iterators going out of scope and suggest collecting #81584

Closed
@estebank

Description

@estebank

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.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-iteratorsArea: IteratorsA-lifetimesArea: Lifetimes / regionsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.P-lowLow priorityT-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