Clippy lints / Complexity
Clippy lint · Complexity · checked with Clippy 0.1.99
clippy::unused_format_specs: format specifiers have no effect on `format_args!()`
What it meansDetects formatting parameters that have no effect on the output of format!(), println!() or similar macros. This includes: Format specifiers on format_args!() (width, precision have no effect) Format width too small for the format trait (e.g. {:#02x} outputs “0x1” so width 2 has no effect; minimum is 4 for alternate hex/octal/binary) It is in the complexity group and warns by default.
What you see
warning: format specifiers have no effect on `format_args!()` --> main.rs:5:23 | 5 | println!("not padded: {:5}", format_args!("...")); | ^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#unused_format_specs = note: `#[warn(clippy::unused_format_specs)]` on by default help: for the width to apply consider using `format!()` | 5 - println!("not padded: {:5}", format_args!("...")); 5 + println!("not padded: {:5}", format!("...")); | help: if the current behavior is intentional, remove the format specifiers | 5 - println!("not padded: {:5}", format_args!("...")); 5 + println!("not padded: {}", format_args!("...")); |
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
Shorter format specifiers are easier to read, it may also indicate that an expected formatting operation such as adding padding isn’t happening.
Code that does something simple in a complicated way. Warns by default.
How to fix it
Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:
Flagged
println!("{:.}", 1.0);
println!("not padded: {:5}", format_args!("..."));
// width 2 has no effect for alternate hex (outputs "0x1")
format!("{:#02x}", 1_u8);
Write this instead
println!("{}", 1.0);
println!("not padded: {}", format_args!("..."));
// OR
println!("padded: {:5}", format!("..."));
format!("{:#04x}", 1_u8); // width 4 for two-digit zero-padded hex
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::unused_format_specs)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
unused_format_specs = "allow"One run
cargo clippy -- -A clippy::unused-format-specsThe [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
Detects formatting parameters that have no effect on the output of
format!(), println!() or similar macros.
This includes:
- Format specifiers on
format_args!()(width, precision have no effect) - Format width too small for the format trait (e.g.
{:#02x}outputs “0x1” so width 2 has no effect; minimum is 4 for alternate hex/octal/binary)
Why is this bad?
Shorter format specifiers are easier to read, it may also indicate that an expected formatting operation such as adding padding isn’t happening.
Example
println!("{:.}", 1.0);
println!("not padded: {:5}", format_args!("..."));
// width 2 has no effect for alternate hex (outputs "0x1")
format!("{:#02x}", 1_u8);
Use instead:
println!("{}", 1.0);
println!("not padded: {}", format_args!("..."));
// OR
println!("padded: {:5}", format!("..."));
format!("{:#04x}", 1_u8); // width 4 for two-digit zero-padded hex
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- too_many_argumentsChecks for functions with too many parameters
- clone_on_copyusing `clone` on type `u64` which implements the `Copy` trait
- type_complexityChecks for types used in structs, parameters and let declarations above a certain complexity threshold
- unnecessary_unwrapChecks for calls of unwrap[_err]() that cannot fail
- unnecessary_unwrap_uncheckedusage of `unwrap_unchecked` when an `_unchecked` variant of the function exists
- unneeded_wildcard_patternthis pattern is unneeded as the `..` pattern can match that element
- useless_asrefChecks for usage of .as_ref() or .as_mut() where the types before and after the call are the same
- useless_concatunneeded use of `concat!` macro
- useless_conversionuseless conversion to the same type: `std::string::String`
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::too_many_argumentsChecks for functions with too many parametersRead it next →Did this answer your question?