Email me

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-specs

The [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.

Complexitygroup
warndefault level
nofixed by cargo clippy --fix
1.66.0added in Rust

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

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?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ