fix: don't suggest .into_iter() for .cloned()/.copied() on non-reference Option#157413
Merged
rust-bors[bot] merged 1 commit intoJun 8, 2026
Merged
Conversation
Collaborator
|
r? @folkertdev rustbot has assigned @folkertdev. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
5160827 to
b394147
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
b394147 to
a49c252
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
a49c252 to
914cc9c
Compare
This comment has been minimized.
This comment has been minimized.
folkertdev
requested changes
Jun 7, 2026
| && 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(_)) |
Contributor
There was a problem hiding this comment.
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}>`")); |
Contributor
There was a problem hiding this comment.
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.
Collaborator
|
Reminder, once the PR becomes ready for a review, use |
…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.
914cc9c to
4049f43
Compare
Contributor
Author
|
@rustbot ready |
Contributor
|
Thanks! @bors r+ rollup |
Contributor
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-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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
calling
.cloned()or.copied()onOption<T>whereTis 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()yieldsTby value not&Tso.cloned()/.copied()failed again for the same reason.Option<T>implementsIntoIteratorwhich is why theimpl_into_iterator_should_be_iteratorbranch fires. added a guard before it: when the method iscloned/copied, the receiver isOption<T>andTis a concrete non-reference type. emit a targeted label pointing at the correct form (Option<&T>) and suggest removing the call instead.closes #151147