Skip to content

False positive: print_literal with numeric literal arguments #6335

Closed
@197g

Description

@197g

I tried this code:

println!("{}", 0xdead_beefu32)

I expected to see this happen: no warnings, the decimal conversion is printed.

Instead, this happened:

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

The same issue appears with float literals. The problem is that the source code representation need not equate the printed number in any obvious way. As hopefully shown by the example above, there are a number of conversion applied to literals that might make their decimal Display representation rather non-obvious. The motivation for the source code representation is entirely different, showing rather how they might be derive or related to a specification document. Examples where the source representation is different than the Display one include:

  • 1.200 to show the number of significant digits of a constant.
  • 0b11001 for numbers with bit flags.
  • 1_000_001 as recommended for splitting long numbers.
  • 123.0E+77 for engineering numbers.

Meta

  • cargo clippy -V: 0.0.212 (2020-11-13 74f7e32)
  • `rustc -V: 1.4.25-nightly (2020-11-11 0f29ff6)

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't havegood first issueThese issues are a good way to get started with Clippy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions