-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Warn on push_str(&format!()) #6261
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
Comments
I had actually thought this would be optimized away but seems like the slowdown is quite significant in release mode see example here https://play.rust-lang.org/?version=nightly&mode=release&edition=2018&gist=daed734101a8d8bc0c6a7bd811154504 There are some additional drawbacks in addition to the ones you mentioned however. use std::fmt::Write;
let mut string = String::new();
for foo in foo_list {
write!(&mut string, "{:?}", foo).expect("This can never fail");
} Which might cause other lints to give you trouble specifically lints such as You also need to import either/both use std::fmt::Write as FmtWrite;
use std::io::Write as IoWrite; I also feel the readability of All that said I still feel like the performance probably makes up for the drawbacks. I'll try to implement a lint for this over weekend |
If anyone asks about using |
@rustbot claim |
New lint `format_add_strings` Closes #6261 changelog: Added [`format_add_string`]: recommend using `write!` instead of appending the result of `format!`
This commit fixes an error that Clippy flags in Rust 1.62.1 in which appending the output of a `format!` macro to a String is an error because "one extra heap allocation can be avoided". (See rust-lang/rust-clippy#6261 for details.) Signed-off-by: Geoffrey M. Oxberry <[email protected]>
This commit fixes an error that Clippy flags in Rust 1.62.1 in which appending the output of a `format!` macro to a String is an error because "one extra heap allocation can be avoided". (See rust-lang/rust-clippy#6261 for details.) Signed-off-by: Geoffrey M. Oxberry <[email protected]>
* rust-toolchain: bump rust to 1.62.1 (like vector) This commit bumps the Rust version Lading uses to version 1.62.1 for parity with the Vector project (https://github.com/vectordotdev/vector). Signed-off-by: Geoffrey M. Oxberry <[email protected]> * blackhole/sqs.rs: fix clippy format macro error This commit fixes an error that Clippy flags in Rust 1.62.1 in which appending the output of a `format!` macro to a String is an error because "one extra heap allocation can be avoided". (See rust-lang/rust-clippy#6261 for details.) Signed-off-by: Geoffrey M. Oxberry <[email protected]>
Uh oh!
There was an error while loading. Please reload this page.
What it does
Emits a warning on code like ```rust
some_string.push_str(&format!(...));
// or
some_string += &format!(...);
Categories
Perf
What is the advantage of the recommended code over the original code
It avoids one extra heap allocation by writing directly to the existing String instead of creating a new temporary String.
Drawbacks
The user has to
use std::io::Write
for thewrite!
macro to work (which I don't get tbh, why can'twrite!
just import the trait on its own...)Example
Could be written as:
The text was updated successfully, but these errors were encountered: