Skip to content

Conversation

GuillaumeGomez
Copy link
Member

@GuillaumeGomez GuillaumeGomez commented Mar 12, 2025

Fixes #14175.

There are two "big" cases to handle: .map(|x| x.to_string()) and .map(String::to_string). If the closure has more than one expression, we should not suggest .cloned().

changelog: Improve string_to_string lint in case it is in a map call

r? @samueltardieu

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Mar 12, 2025
@GuillaumeGomez GuillaumeGomez force-pushed the string_to_string-map branch 3 times, most recently from 1ab177f to b4a6b9c Compare March 12, 2025 14:56
@GuillaumeGomez
Copy link
Member Author

Finally fixed dogfood. :D

Copy link
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

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

Thanks, this is an nice new message.

One thing that bothers me a bit is the HIR node to which the new message applies. Shouldn't it be associated with the .map() call node, since this is what the message suggests to rewrite with .cloned()?

At some point, we'll need the node anyway if we are going to propose an autofix.

@GuillaumeGomez
Copy link
Member Author

One thing that bothers me a bit is the HIR node to which the new message applies. Shouldn't it be associated with the .map() call node, since this is what the message suggests to rewrite with .cloned()?

At some point, we'll need the node anyway if we are going to propose an autofix.

I kept the current formatting: it's always underlining the caller. I can send a follow-up to fix that if you want?

@samueltardieu
Copy link
Member

I kept the current formatting: it's always underlining the caller.

Yes, but it also only proposed to modify the caller itself, never the enclosing map expression.

I can send a follow-up to fix that if you want?

I would say that the message probably belongs to the .map() call. If the .map() is linted for any other reason and we switch to autofixes, we may have problems.

@GuillaumeGomez
Copy link
Member Author

I updated to emit a suggestion on the method call.

@samueltardieu
Copy link
Member

@GuillaumeGomez Did you miss the proposal in #14396 (comment)? Right now, s.map(|x| { x.to_string() }) will suggest x.clone(), while it could suggest s.cloned(), as if the block was not present.

@GuillaumeGomez
Copy link
Member Author

I completely missed it, sorry. Updating.

@GuillaumeGomez
Copy link
Member Author

Added the suggested improvement, thanks!

Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

Some additional comments.

@GuillaumeGomez
Copy link
Member Author

So in the end, I just removed the comment in the ui test. ^^'

@flip1995 flip1995 added this pull request to the merge queue Mar 18, 2025
Merged via the queue into rust-lang:master with commit a07e887 Mar 18, 2025
11 checks passed
@GuillaumeGomez GuillaumeGomez deleted the string_to_string-map branch March 18, 2025 12:15
smoelius added a commit to smoelius/rust-clippy that referenced this pull request Mar 20, 2025
smoelius added a commit to smoelius/rust-clippy that referenced this pull request Mar 28, 2025
smoelius added a commit to smoelius/rust-clippy that referenced this pull request Mar 28, 2025
smoelius added a commit to smoelius/rust-clippy that referenced this pull request Mar 28, 2025
smoelius added a commit to smoelius/rust-clippy that referenced this pull request Mar 28, 2025
smoelius added a commit to smoelius/rust-clippy that referenced this pull request Mar 28, 2025
smoelius added a commit to smoelius/rust-clippy that referenced this pull request Apr 13, 2025
smoelius added a commit to smoelius/rust-clippy that referenced this pull request Apr 13, 2025
smoelius added a commit to smoelius/rust-clippy that referenced this pull request Apr 20, 2025
smoelius added a commit to smoelius/rust-clippy that referenced this pull request Apr 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

string_to_string does not trigger inside a map function
4 participants