Skip to content

print_literal causes semantic change when inlined literal is succeeded by a numbered arg #15576

@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

--force-warn clippy::print_literal

this code:

pub fn main() {
    println!("Hello {} is {2:.*}", "x", 5, 0.01);
}

caused the following diagnostics:

    Checking _a v0.1.0 (/tmp/icemaker_global_tempdir.K9QWpxAXuoH0/icemaker_clippyfix_tempdir.VR9VFWCCYMUj/_a)
warning: literal with an empty format string
 --> src/main.rs:2:36
  |
2 |     println!("Hello {} is {2:.*}", "x", 5, 0.01);
  |                                    ^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#print_literal
  = note: requested on the command line with `--force-warn clippy::print-literal`
help: try
  |
2 -     println!("Hello {} is {2:.*}", "x", 5, 0.01);
2 +     println!("Hello x is {2:.*}", 5, 0.01);
  |

warning: `_a` (bin "_a") generated 1 warning (run `cargo clippy --fix --bin "_a"` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.13s

However after applying these diagnostics, the resulting code:

pub fn main() {
    println!("Hello x is {2:.*}", 5, 0.01);
}

no longer compiled:

    Checking _a v0.1.0 (/tmp/icemaker_global_tempdir.K9QWpxAXuoH0/icemaker_clippyfix_tempdir.VR9VFWCCYMUj/_a)
error: invalid reference to positional argument 2 (there are 2 arguments)
 --> src/main.rs:2:27
  |
2 |     println!("Hello x is {2:.*}", 5, 0.01);
  |                           ^
  |
  = note: positional arguments are zero-based
  = note: for information about formatting flags, visit https://doc.rust-lang.org/std/fmt/index.html

error: argument never used
 --> src/main.rs:2:38
  |
2 |     println!("Hello x is {2:.*}", 5, 0.01);
  |              -------------------     ^^^^ argument never used
  |              |
  |              formatting specifier missing

error: could not compile `_a` (bin "_a") due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
error: could not compile `_a` (bin "_a" test) due to 2 previous errors

Version:

rustc 1.91.0-nightly (b2dd217dd 2025-08-27)
binary: rustc
commit-hash: b2dd217dd0a099fb87601657ec480bf3e92b30a6
commit-date: 2025-08-27
host: x86_64-unknown-linux-gnu
release: 1.91.0-nightly
LLVM version: 21.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions