Skip to content

Add extra symbol check for .to_owned()#156074

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
EvoPot:cow-diag-item
May 2, 2026
Merged

Add extra symbol check for .to_owned()#156074
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
EvoPot:cow-diag-item

Conversation

@EvoPot
Copy link
Copy Markdown
Contributor

@EvoPot EvoPot commented May 2, 2026

Follow up of #154646

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.

r? @davidtwco since you added the review to the previous PR

@rustbot rustbot added 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. labels May 2, 2026
@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 2, 2026

The Clippy subtree was changed

cc @rust-lang/clippy

@rustbot rustbot added the T-clippy Relevant to the Clippy team. label 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") {
Copy link
Copy Markdown
Member

@chenyukang chenyukang May 2, 2026

Choose a reason for hiding this comment

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

if we already used is_diagnostic_item to check the method name, there is no need to check snippet?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The previous PR used that to crop the span of the method, so I kept it.

@EvoPot EvoPot requested a review from chenyukang May 2, 2026 06:55
Comment thread compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs Outdated
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`.
@EvoPot EvoPot requested a review from ada4a May 2, 2026 11:48
@chenyukang
Copy link
Copy Markdown
Member

Thanks!
@bors r=chenyukang

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 2, 2026

📌 Commit 8a77369 has been approved by chenyukang

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 May 2, 2026
rust-bors Bot pushed a commit that referenced this pull request May 2, 2026
…uwer

Rollup of 4 pull requests

Successful merges:

 - #156074 (Add extra symbol check for `.to_owned()`)
 - #156045 (Move tests associated consts)
 - #156064 (Do not depend on typeck to borrow-check inline consts.)
 - #156083 (Move tests attributes)
@rust-bors rust-bors Bot merged commit 6fa2265 into rust-lang:main May 2, 2026
11 checks passed
@rustbot rustbot added this to the 1.97.0 milestone May 2, 2026
rust-timer added a commit that referenced this pull request May 2, 2026
Rollup merge of #156074 - EvoPot:cow-diag-item, r=chenyukang

Add extra symbol check for `.to_owned()`

Follow up of #154646

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`.

r? @davidtwco since you added the review to the previous PR
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request May 4, 2026
…uwer

Rollup of 4 pull requests

Successful merges:

 - rust-lang/rust#156074 (Add extra symbol check for `.to_owned()`)
 - rust-lang/rust#156045 (Move tests associated consts)
 - rust-lang/rust#156064 (Do not depend on typeck to borrow-check inline consts.)
 - rust-lang/rust#156083 (Move tests attributes)
pull Bot pushed a commit to Kokoro2336/rust-analyzer that referenced this pull request May 4, 2026
…uwer

Rollup of 4 pull requests

Successful merges:

 - rust-lang/rust#156074 (Add extra symbol check for `.to_owned()`)
 - rust-lang/rust#156045 (Move tests associated consts)
 - rust-lang/rust#156064 (Do not depend on typeck to borrow-check inline consts.)
 - rust-lang/rust#156083 (Move tests attributes)
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-clippy Relevant to the Clippy team. 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.

6 participants