Skip to content

Comments

fix: handle false negative for str_to_string#16512

Merged
samueltardieu merged 1 commit intorust-lang:masterfrom
nyurik:str-to-string
Feb 5, 2026
Merged

fix: handle false negative for str_to_string#16512
samueltardieu merged 1 commit intorust-lang:masterfrom
nyurik:str-to-string

Conversation

@nyurik
Copy link
Contributor

@nyurik nyurik commented Feb 5, 2026

Replace ToString::to_string with ToOwned::to_owned when it is passed as a function parameter.

fn issue16511(x: Option<&str>) -> Option<String> {
    // Replace with ToOwned::to_owned
    x.map(ToString::to_string)
}

fixes #16511


changelog: [str_to_string]: handle a case when ToString::to_string is passed as function parameter

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Feb 5, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 5, 2026

r? @samueltardieu

rustbot has assigned @samueltardieu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@github-actions
Copy link

github-actions bot commented Feb 5, 2026

Lintcheck changes for 691e226

Lint Added Removed Changed
clippy::str_to_string 17 0 1

This comment will be updated if you push new changes

Replace `ToString::to_string` with `ToOwned::to_owned` when the function is passed as is:

```rust
fn issue16511(x: Option<&str>) -> String {
    // Replace with ToOwned::to_owned
    x.map(ToString::to_string)
}
```
@samueltardieu samueltardieu added this pull request to the merge queue Feb 5, 2026
@samueltardieu
Copy link
Member

Thanks

Merged via the queue into rust-lang:master with commit b303ab4 Feb 5, 2026
11 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Feb 5, 2026
@nyurik nyurik deleted the str-to-string branch February 5, 2026 20:44
@nyurik
Copy link
Contributor Author

nyurik commented Feb 5, 2026

@samueltardieu thx for a quick review! I wonder if there should be a separate similar lint or a bunch of lints for cases like:

  • Path: .to_pathbuf() -> to_owned()
  • String: to_owned() or to_string() -> clone()
  • PathBuf: to_owned() or to_pathbuf() -> clone()

and the likes?

github-merge-queue bot pushed a commit that referenced this pull request Feb 19, 2026
Fix #16569 bug introduced in
#16512

changelog: [`str_to_string`]: fix false positive

r? @samueltardieu
@TimDiekmann
Copy link
Member

This "fix" introduced a bunch of new false positives if ToString::to_string is used in a map when ToOwned::to_owned is not available: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=a1f10acd03e29a7704ef14c768ccaa43

@nyurik
Copy link
Contributor Author

nyurik commented Feb 24, 2026

@TimDiekmann did #16571 fix your issue?

@samueltardieu
Copy link
Member

samueltardieu commented Feb 24, 2026

FYI #16571 is not yet propagated to the compiler repository, and thus not available in Clippy nightly. It should be available later this week.

But I can confirm that the playground example now does no longer trigger the lint when using the development version of Clippy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

str_to_string does not trigger inside a map function

4 participants