Skip to content

Unused format!()s are not optimised away #75742

Open
@Timmmm

Description

@Timmmm

I was slightly surprised to see that code like this:

pub fn foo() {
    format!("Hello {}", 4);
}

does not get optimised to a nop. It still calls format!(). This is surprising from a user point of view - you're calling a function, but it seems like it has no side effects and I don't use the result, so surely it should be removed?

I presume the reason it doesn't is that it allocates a String and heap allocation is considered to be a side effect? Whatever the reason I think that it would be nice if it was optimised.

If you're wondering about motivation, consider a debug trait with a default implementation that does nothing. You could call debug.write(&format!(...)) and it would be fully optimised away for the default implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-mir-optArea: MIR optimizationsC-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-slowIssue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions