Skip to content

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

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
Albab-Hasan:option-cloned-non-ref-diagnostic
Jun 8, 2026
Merged

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

Conversation

@Albab-Hasan

@Albab-Hasan Albab-Hasan commented Jun 4, 2026

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

rustbot commented Jun 4, 2026

Copy link
Copy Markdown
Collaborator

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.

@Albab-Hasan Albab-Hasan force-pushed the option-cloned-non-ref-diagnostic branch from a49c252 to 914cc9c Compare June 4, 2026 09:00
@rustbot

This comment has been minimized.

@folkertdev folkertdev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove that link from the commit message (or just write it out fully as a https:// kind of link)

View changes since this review

&& let ty::Adt(adt_def, args) = rcvr_ty.kind()
&& tcx.is_diagnostic_item(sym::Option, adt_def.did())
&& let inner_ty = args.type_at(0)
&& !matches!(inner_ty.kind(), ty::Ref(..) | ty::Param(_) | ty::Infer(_))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you motivate this (and add a test that the correct error is emitted when the inner type is one of these)?

// The default branch below would suggest `.into_iter()`, but that still
// fails: `Option<T>` yields `T` by value, not `&T`, so `.cloned()`/`.copied()`
// can't resolve. Give a targeted diagnostic instead.
err.span_label(span, format!("this method is only available on `Option<&{inner_ty}>`"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
err.span_label(span, format!("this method is only available on `Option<&{inner_ty}>`"));
err.span_label(span, format!("this method is only available on `Option<&_>`"));

Maybe this should be Option<&_> instead, that is what the impl is actually for.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 7, 2026
@rustbot

rustbot commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

…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.
@Albab-Hasan Albab-Hasan force-pushed the option-cloned-non-ref-diagnostic branch from 914cc9c to 4049f43 Compare June 8, 2026 08:41
@Albab-Hasan

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 8, 2026
@Albab-Hasan Albab-Hasan requested a review from folkertdev June 8, 2026 08:44
@folkertdev

Copy link
Copy Markdown
Contributor

Thanks!

@bors r+ rollup

@rust-bors

rust-bors Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 4049f43 has been approved by folkertdev

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 8, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jun 8, 2026
…diagnostic, r=folkertdev

fix: don't suggest .into_iter() for .cloned()/.copied() on non-reference 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
rust-bors Bot pushed a commit that referenced this pull request Jun 8, 2026
…uwer

Rollup of 13 pull requests

Successful merges:

 - #147302 (asm! support for the Xtensa architecture)
 - #148820 (Add very basic "comptime" fn implementation)
 - #157299 (Fix unstable diagnostics in tests)
 - #143511 (Improve TLS codegen by marking the panic/init path as cold)
 - #154608 (Add `_value` API for number literals in proc-macro)
 - #156762 (xfs support in `test_rename_directory_to_non_empty_directory`)
 - #157300 (Relax test requirements for consistency)
 - #157383 (tests: codegen-llvm: Ignore BPF targets in c-variadic-opt)
 - #157413 (fix: don't suggest .into_iter() for .cloned()/.copied() on non-reference Option)
 - #157578 (Fix diagnostics for non-exhaustive destructuring assignments (#157553))
 - #157587 (explain that the size_of constant also serves to avoid optimizing away 'unused' size_of calls)
 - #157596 (test: remove ineffective link-extern-crate-with-drop-type test)
 - #157602 (rustdoc: Remove unnecessary fast path)
@rust-bors rust-bors Bot merged commit 4c561f2 into rust-lang:main Jun 8, 2026
12 checks passed
@rustbot rustbot added this to the 1.98.0 milestone Jun 8, 2026
rust-timer added a commit that referenced this pull request Jun 8, 2026
Rollup merge of #157413 - Albab-Hasan:option-cloned-non-ref-diagnostic, r=folkertdev

fix: don't suggest .into_iter() for .cloned()/.copied() on non-reference 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 #151147
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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