Skip to content

FP print_literal #14930

Closed
Closed
@matthiaskrgr

Description

@matthiaskrgr

Summary

.

Lint Name

print_literal

Reproducer

I tried this code:

fn main() {
    println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
}

I saw this happen:

warning: literal with an empty format string
 --> src/main.rs:2:41
  |
2 |     println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
  |                                         ^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#print_literal
  = note: `#[warn(clippy::print_literal)]` on by default
help: try
  |
2 -     println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
2 +     println!("Hello x is {1}", 5, 0.01);
  |

The suggested code

fn main() {
    println!("Hello x is {1}", 5, 0.01);
}

does not compile

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

error: aborting due to 1 previous error

Original diagnostics will follow.

warning: literal with an empty format string
 --> src/main.rs:2:41
  |
2 |     println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
  |                                         ^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#print_literal
  = note: `#[warn(clippy::print_literal)]` on by default
help: try
  |
2 -     println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
2 +     println!("Hello x is {1}", 5, 0.01);
  |
``

### Version

```text

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions