-
-
Notifications
You must be signed in to change notification settings - Fork 15.1k
fix: don't suggest .into_iter() for .cloned()/.copied() on non-reference Option #157413
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
Merged
rust-bors
merged 1 commit into
rust-lang:main
from
Albab-Hasan:option-cloned-non-ref-diagnostic
Jun 8, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Tests that the guard for `.cloned()`/`.copied()` on `Option<T>` correctly | ||
| // excludes inner types where the targeted diagnostic would be wrong: | ||
| // | ||
| // - `Option<&T>`: `Option<&T>` has inherent `cloned()`/`copied()` methods, | ||
| // so these compile successfully and the guard must not fire. | ||
| // - `Option<T>` (generic param): falls through to the standard "not an | ||
| // iterator / call .into_iter() first" diagnostic. | ||
| // | ||
| // See https://github.com/rust-lang/rust/issues/151147 | ||
|
|
||
| // Reference inner type: these should compile without error. | ||
| pub fn cloned_on_ref(x: Option<&i32>) -> Option<i32> { | ||
| x.cloned() | ||
| } | ||
|
|
||
| pub fn copied_on_ref(x: Option<&i32>) -> Option<i32> { | ||
| x.copied() | ||
| } | ||
|
|
||
| // Generic param inner type: falls through to the standard diagnostic. | ||
| pub fn cloned_on_param<T: Clone>(x: Option<T>) { | ||
| x.cloned(); | ||
| //~^ ERROR no method named `cloned` found for enum `Option<T>` in the current scope | ||
| //~| HELP call `.into_iter()` first | ||
| } | ||
|
|
||
| fn main() {} |
14 changes: 14 additions & 0 deletions
14
tests/ui/methods/option-cloned-copied-non-ref-excluded.stderr
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| error[E0599]: no method named `cloned` found for enum `Option<T>` in the current scope | ||
| --> $DIR/option-cloned-copied-non-ref-excluded.rs:22:7 | ||
| | | ||
| LL | x.cloned(); | ||
| | ^^^^^^ `Option<T>` is not an iterator | ||
| | | ||
| help: call `.into_iter()` first | ||
| | | ||
| LL | x.into_iter().cloned(); | ||
| | ++++++++++++ | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0599`. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Tests that calling `.cloned()` or `.copied()` on `Option<T>` where `T` is an | ||
| // owned (non-reference) type gives a targeted diagnostic instead of the | ||
| // misleading "not an iterator / call .into_iter() first" suggestion. | ||
| // See https://github.com/rust-lang/rust/issues/151147 | ||
|
|
||
| //@ run-rustfix | ||
|
|
||
| pub fn cloned_on_owned(x: Option<i32>) -> i32 { | ||
| x.unwrap() | ||
| //~^ ERROR no method named `cloned` found for enum `Option<i32>` in the current scope | ||
| //~| HELP consider removing the `.cloned()` call | ||
| } | ||
|
|
||
| pub fn copied_on_owned(x: Option<i32>) -> i32 { | ||
| x.unwrap() | ||
| //~^ ERROR no method named `copied` found for enum `Option<i32>` in the current scope | ||
| //~| HELP consider removing the `.copied()` call | ||
| } | ||
|
|
||
| pub fn cloned_on_string(x: Option<String>) -> String { | ||
| x.unwrap() | ||
| //~^ ERROR no method named `cloned` found for enum `Option<String>` in the current scope | ||
| //~| HELP consider removing the `.cloned()` call | ||
| } | ||
|
|
||
| fn main() {} |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Tests that calling `.cloned()` or `.copied()` on `Option<T>` where `T` is an | ||
| // owned (non-reference) type gives a targeted diagnostic instead of the | ||
| // misleading "not an iterator / call .into_iter() first" suggestion. | ||
| // See https://github.com/rust-lang/rust/issues/151147 | ||
|
|
||
| //@ run-rustfix | ||
|
|
||
| pub fn cloned_on_owned(x: Option<i32>) -> i32 { | ||
| x.cloned().unwrap() | ||
| //~^ ERROR no method named `cloned` found for enum `Option<i32>` in the current scope | ||
| //~| HELP consider removing the `.cloned()` call | ||
| } | ||
|
|
||
| pub fn copied_on_owned(x: Option<i32>) -> i32 { | ||
| x.copied().unwrap() | ||
| //~^ ERROR no method named `copied` found for enum `Option<i32>` in the current scope | ||
| //~| HELP consider removing the `.copied()` call | ||
| } | ||
|
|
||
| pub fn cloned_on_string(x: Option<String>) -> String { | ||
| x.cloned().unwrap() | ||
| //~^ ERROR no method named `cloned` found for enum `Option<String>` in the current scope | ||
| //~| HELP consider removing the `.cloned()` call | ||
| } | ||
|
|
||
| fn main() {} |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| error[E0599]: no method named `cloned` found for enum `Option<i32>` in the current scope | ||
| --> $DIR/option-cloned-copied-non-ref.rs:9:7 | ||
| | | ||
| LL | x.cloned().unwrap() | ||
| | -^^^^^^-- | ||
| | || | ||
| | |this method is only available on `Option<&_>` | ||
| | help: consider removing the `.cloned()` call | ||
|
|
||
| error[E0599]: no method named `copied` found for enum `Option<i32>` in the current scope | ||
| --> $DIR/option-cloned-copied-non-ref.rs:15:7 | ||
| | | ||
| LL | x.copied().unwrap() | ||
| | -^^^^^^-- | ||
| | || | ||
| | |this method is only available on `Option<&_>` | ||
| | help: consider removing the `.copied()` call | ||
|
|
||
| error[E0599]: no method named `cloned` found for enum `Option<String>` in the current scope | ||
| --> $DIR/option-cloned-copied-non-ref.rs:21:7 | ||
| | | ||
| LL | x.cloned().unwrap() | ||
| | -^^^^^^-- | ||
| | || | ||
| | |this method is only available on `Option<&_>` | ||
| | help: consider removing the `.cloned()` call | ||
|
|
||
| error: aborting due to 3 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0599`. |
Oops, something went wrong.
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.
There was a problem hiding this comment.
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)?