Skip to content

Remove redundant .to_string().as_str() calls - #148155

Closed
osamakader wants to merge 1 commit into
rust-lang:masterfrom
osamakader:remove-redundant-to-string-as-str
Closed

Remove redundant .to_string().as_str() calls#148155
osamakader wants to merge 1 commit into
rust-lang:masterfrom
osamakader:remove-redundant-to-string-as-str

Conversation

@osamakader

Copy link
Copy Markdown
Contributor

The pattern .to_string().as_str() creates a temporary String that is immediately borrowed and then dropped, which is inefficient. Replace with &.to_string() for cleaner, more idiomatic code.

Changes:

  • compiler/rustc_builtin_macros/src/autodiff.rs: Simplify identifier creation
  • compiler/rustc_codegen_llvm/src/context.rs: Simplify error message formatting

The pattern .to_string().as_str() creates a temporary String that is
immediately borrowed and then dropped, which is inefficient. Replace
with &.to_string() for cleaner, more idiomatic code.
Changes:
- compiler/rustc_builtin_macros/src/autodiff.rs: Simplify identifier creation
- compiler/rustc_codegen_llvm/src/context.rs: Simplify error message formatting
Signed-off-by: Osama Abdelkader <osama.abdelkader@gmail.com>
@rustbotrustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 26, 2025
@osamakader
osamakader marked this pull request as ready for review October 26, 2025 22:01
@rustbotrustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 26, 2025
@rustbot

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_builtin_macros/src/autodiff.rs

cc @ZuseZ4

@rustbotrustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Oct 26, 2025
@rustbot

Copy link
Copy Markdown
Collaborator

r? @lcnr

rustbot has assigned @lcnr.
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

@Kobzol

Copy link
Copy Markdown
Member

.as_str() vs & has no performance effect here, it does the same thing.

@lcnr

lcnr commented Oct 27, 2025

Copy link
Copy Markdown
Contributor

Why is &X.to_string() more idiomatic?

I don't think this change is worth the (admittedly very small) churn

@rustbotrustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 27, 2025
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.T-compilerRelevant 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.

4 participants

@osamakader@rustbot@Kobzol@lcnr