rust-lang / rust-lang/rust-clippy

false positive of lossy_float_literal for bigger powers of 2

Open
#7,404 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Powers of 2 which exceed the size of the mantissa seem to yield a lossy_float_literal warning even though those are perfectly representable.

I tried this code:

fn main() {
    let x64 = 4_294_967_296_f64;
    let x32 = 4_294_967_296_f32;
    println!("x64 {:016x}", x64.to_bits());
    println!("x32 {:08x}", x32.to_bits());
    println!("x32 as x64 {:016x}", (x32 as f64).to_bits());
}

I expected to see this happen:

No clippy warnings.

Instead, this happened:

4_294_967_296_f32 get reported as not being covered by f32. The output shows that this value could successfully be converted into the expected bit-pattern.

x64 41f0000000000000
x32 4f800000
x32 as x64 41f0000000000000
Meta
  • cargo clippy -V: clippy 0.1.54 (e82b650 2021-06-20)
  • rustc -Vv:
    rustc 1.55.0-nightly (e82b65026 2021-06-20)
    binary: rustc
    commit-hash: e82b65026dcadf0998e3abf9dabc677f63160de7
    commit-date: 2021-06-20
    host: x86_64-unknown-linux-gnu
    release: 1.55.0-nightly
    LLVM version: 12.0.1
    

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported Rust example and locating the lossy_float_literal lint. Check how the lint evaluates the larger powers-of-two literals, then verify that the shown f32 value no longer produces a warning while the existing warning behavior remains covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.