Clippy lints / Performance
Clippy lint · Performance · checked with Clippy 0.1.99
clippy::to_string_in_format_args: Checks for ToString::to_string applied to a type that implements Display in a macro that does formatting
What it meansChecks for ToString::to_string applied to a type that implements Display in a macro that does formatting. It is in the performance group and warns by default.
Does it matter?
Since the type implements Display, the use of to_string is unnecessary.
Code that could be written to run faster. Warns by default.
How to fix it
Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.
Flagged
println!("error: something failed at {}", Location::caller().to_string());
Write this instead
println!("error: something failed at {}", Location::caller());
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::to_string_in_format_args)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
to_string_in_format_args = "allow"One run
cargo clippy -- -A clippy::to-string-in-format-argsThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Checks for ToString::to_string
applied to a type that implements Display
in a macro that does formatting.
Why is this bad?
Since the type implements Display, the use of to_string is
unnecessary.
Example
println!("error: something failed at {}", Location::caller().to_string());
Use instead:
println!("error: something failed at {}", Location::caller());
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- large_enum_variantlarge size difference between variants
- result_large_errthe `Err`-variant returned from this function is very large
- sliced_string_as_bytescalling `as_bytes` after slicing a string
- slow_vector_initializationChecks slow zero-filled vector initialization
- unbuffered_bytescalling .bytes() is very inefficient when data is not in memory
- unnecessary_to_ownedunnecessary use of `to_string`
- useless_borrows_in_formattingredundant reference in `println!` argument
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::large_enum_variantlarge size difference between variantsRead it next →Did this answer your question?