-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unused format!()
s are not optimised away
#75742
Comments
struct Foo;
impl fmt::Display for Foo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
panic!();
}
}
pub fn foo() {
format!("{}", Foo);
} There is just no guarantee that In other word: |
Is there any other samples other than |
You can replace the |
Did you mean write trait implementation that does nothing? The formatting in format macro is eager and so it does have side-effects, but At the same time there is an alternative that avoids cost of formatting when |
I was slightly surprised to see that code like this:
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.The text was updated successfully, but these errors were encountered: