Skip to content

fix: don't suggest .into_iter() for .cloned()/.copied() on non-reference Option#157413

Open
Albab-Hasan wants to merge 1 commit into
rust-lang:mainfrom
Albab-Hasan:option-cloned-non-ref-diagnostic
Open

fix: don't suggest .into_iter() for .cloned()/.copied() on non-reference Option#157413
Albab-Hasan wants to merge 1 commit into
rust-lang:mainfrom
Albab-Hasan:option-cloned-non-ref-diagnostic

Conversation

@Albab-Hasan
Copy link
Copy Markdown
Contributor

calling .cloned() or .copied() on Option<T> where T is an owned type emitted "Option<T> is not an iterator" with a suggestion to prepend .into_iter(). the suggested code still did not compile: Option<T>::into_iter() yields T by value not &T so .cloned()/.copied() failed again for the same reason.

Option<T> implements IntoIterator which is why the impl_into_iterator_should_be_iterator branch fires. added a guard before it: when the method is cloned/copied, the receiver is Option<T> and T is a concrete non-reference type. emit a targeted label pointing at the correct form (Option<&T>) and suggest removing the call instead.

closes #151147

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 4, 2026
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 4, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 4, 2026

r? @folkertdev

rustbot has assigned @folkertdev.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 18 candidates

@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Albab-Hasan Albab-Hasan force-pushed the option-cloned-non-ref-diagnostic branch from 5160827 to b394147 Compare June 4, 2026 06:02
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@Albab-Hasan Albab-Hasan force-pushed the option-cloned-non-ref-diagnostic branch from b394147 to a49c252 Compare June 4, 2026 06:38
@rustbot

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

…nce Option

calling `.cloned()` or `.copied()` on `Option<T>` where `T` is an owned type
emitted "`Option<T>` is not an iterator" with a suggestion to prepend
`.into_iter()`. the suggested code still did not compile: `Option<T>::into_iter()`
yields `T` by value not `&T`, so `.cloned()`/`.copied()` failed again for the
same reason.

`Option<T>` implements `IntoIterator`, which is why the
`impl_into_iterator_should_be_iterator` branch fires. added a guard before it:
when the method is `cloned`/`copied`, the receiver is `Option<T>`, and `T` is a
concrete non-reference type, emit a targeted label pointing at the correct form
(`Option<&T>`) and suggest removing the call instead.

closes rust-lang#151147
@Albab-Hasan Albab-Hasan force-pushed the option-cloned-non-ref-diagnostic branch from a49c252 to 914cc9c Compare June 4, 2026 09:00
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 4, 2026

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calling .cloned() on an Option<X> gives a misleading error message about iterators.

4 participants