Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Casting hexadecimal int literals to f32 is necessary #9603

Closed
porky11 opened this issue Oct 7, 2022 · 6 comments · Fixed by #9609
Closed

Casting hexadecimal int literals to f32 is necessary #9603

porky11 opened this issue Oct 7, 2022 · 6 comments · Fixed by #9609
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@porky11
Copy link

porky11 commented Oct 7, 2022

Summary

Rust sadly doesn't support non-decimal floating point numbers.

Lint Name

clippy::unnecessary_cast

Reproducer

I tried this code:

-0x400 as f32

I saw this happen:

warning: casting integer literal to `f32` is unnecessary
  --> src/world/layer.rs:26:29
   |
26 |                 Vector::new(-0x400 as f32, 0x40 as f32),
   |                             ^^^^^^^^^^^^^ help: try: `-0x400_f32`

I expected to see this happen:

I expected it not to work anymore after following the hint, and that's what happened.

Version

rustc 1.64.0 (a55dd71d5 2022-09-19)
binary: rustc
commit-hash: a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52
commit-date: 2022-09-19
host: x86_64-unknown-linux-gnu
release: 1.64.0
LLVM version: 14.0.6

Additional Labels

No response

@porky11 porky11 added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Oct 7, 2022
@kraktus
Copy link
Contributor

kraktus commented Oct 8, 2022

@rustbot claim

@kraktus
Copy link
Contributor

kraktus commented Oct 8, 2022

I'm unable to reproduce, it runs fine on playground (repro). Can you share a minimum reproducible example @porky11? Thanks

@porky11
Copy link
Author

porky11 commented Oct 8, 2022

@kraktus

fn main() {
    println!("{}", -0x10 as f32);
}

It turns out the warning only appears when the hexadecimal number is negative.

@porky11
Copy link
Author

porky11 commented Oct 8, 2022

@kraktus What even is your example?
Something seems wrong here. -0x400_f32 is of an opaque integer type, not of type f32.

Try this:

fn main() {
    let x: f32 = -0x400_f32;
    println!("{x}");
}

Is this a rust bug?

@porky11
Copy link
Author

porky11 commented Oct 8, 2022

Now I get it: In hexadecimal, f32 is a digit. _ is allowed inside of numbers between digits as well.
So -0x400_f32 is the same as 0x400F32. Just a hexadecimal integer.

@kraktus
Copy link
Contributor

kraktus commented Oct 8, 2022

Nice catch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants