Add extra symbol check for .to_owned()#156074
Open
EvoPot wants to merge 1 commit intorust-lang:mainfrom
Open
Conversation
This comment has been minimized.
This comment has been minimized.
Collaborator
|
The Clippy subtree was changed cc @rust-lang/clippy |
chenyukang
reviewed
May 2, 2026
| && tcx.is_diagnostic_item(sym::to_owned_method, def_id) | ||
| && let Ok(snippet) = tcx.sess.source_map().span_to_snippet(return_span) | ||
| { | ||
| if let Some(pos) = snippet.rfind(".to_owned") { |
Member
There was a problem hiding this comment.
if we already used is_diagnostic_item to check the method name, there is no need to check snippet?
Author
There was a problem hiding this comment.
The previous PR used that to crop the span of the method, so I kept it.
ada4a
reviewed
May 2, 2026
Comment on lines
+3546
to
+3550
| to_owned_ident.span.shrink_to_lo(), | ||
| "try using `.into_owned()` if you meant to convert a `Cow<'_, T>` to an owned `T`", | ||
| "in", | ||
| Applicability::MaybeIncorrect, | ||
| ); |
Contributor
There was a problem hiding this comment.
the indentation here seems off?..
Previously when adding a suggestion for using `Cow::into_owned()` instead of `ToOwned::to_owned()`, the compiler would just convert the methods `Span` into a `String` and do checks on that `String`. This PR adds an extra guard to that suggestion by checking if the method is `sym::to_owned_method`.
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.
Follow up of #154646
Previously when adding a suggestion for using
Cow::into_owned()instead ofToOwned::to_owned(), the compiler would just convert the methodsSpaninto aStringand do checks on thatString. This PR adds an extra guard to that suggestion by checking if the method issym::to_owned_method.r? @davidtwco since you added the review to the previous PR