Skip to content

casting integer literal to float is unnecessary #3877

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

Merged
merged 1 commit into from
Mar 14, 2019
Merged

casting integer literal to float is unnecessary #3877

merged 1 commit into from
Mar 14, 2019

Conversation

rink1969
Copy link

fix issue #3842

@flip1995 flip1995 added the S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) label Mar 13, 2019

error: casting usize to f64 causes a loss of precision on targets with 64-bit wide pointers (usize is 64 bits wide, but f64's mantissa is only 52 bits wide)
error: casting integer literal to f64 is unnecessary
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This warning shouldn't change, as for example 9999999999999999_usize != 9999999999999999_f64

Playground

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is.
But 9999999999999999_usize as f64 == 9999999999999999_f64, so the suggestion equal to orgin code.

Maybe we should check mantissa bits wide for integer literal case.
Then add the test case mentioned above.
Is it ok?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should rewrite some test case.
The error type doesn't match name of test case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed some test case to keep original error type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9999999999999999_usize as f64 == 9999999999999999_f64

That is correct, but the previous warning explicitly warns you about this kind of cast, since you lose precision with it. The new suggestion would indicate to the programmer, that this cast is always ok and he can change the code.

@flip1995
Copy link
Member

For the purpose of easier reviewing: Could you add new commits for a change and avoid force pushing? You can then squash the commits later

@flip1995
Copy link
Member

I think this looks good now, but I have to take a closer look at it this evening.

@flip1995
Copy link
Member

Thanks!

@bors r+

@bors
Copy link
Contributor

bors commented Mar 14, 2019

📌 Commit d9dd008 has been approved by flip1995

bors added a commit that referenced this pull request Mar 14, 2019
casting integer literal to float is unnecessary

fix issue #3842
@bors
Copy link
Contributor

bors commented Mar 14, 2019

⌛ Testing commit d9dd008 with merge 9df3811...

@bors
Copy link
Contributor

bors commented Mar 14, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: flip1995
Pushing 9df3811 to master...

@bors bors merged commit d9dd008 into rust-lang:master Mar 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants