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

Is transmute_int_to_float's justification reasonable? #3550

Closed
Enet4 opened this issue Dec 14, 2018 · 0 comments
Closed

Is transmute_int_to_float's justification reasonable? #3550

Enet4 opened this issue Dec 14, 2018 · 0 comments
Labels
A-documentation Area: Adding or improving documentation good-first-issue These issues are a good way to get started with Clippy

Comments

@Enet4
Copy link
Contributor

Enet4 commented Dec 14, 2018

The lint transmute_int_to_float claims that transmuting directly from a float to an integer can lead to an invalid in-memory representation.

Why is this bad

This might result in an invalid in-memory representation of a float.

This reason seems inaccurate at this point. What can result from transmutation of an integer to a float of the same size is a signalling NaN, which is currently not treated as an invalid representation, nor is it considered undefined behaviour. Moreover, the latest implementations of from_bits from the core library are equivalent to transmute<u32, f32> and transmute<u64, f64> anyway (see for example f64::from_bits), so in practice the lint does not address the claimed issue.

This does not invalidate the lint, since it provides a reasonable alternative to the use of an unsafe function that can do something really dangerous when used improperly (basically for the reasons described in #1675). What do you think of changing the "Why this is bad" explanation to the following?

Why is this bad

from_bits does not require unsafe and is less error-prone than transmute.

@flip1995 flip1995 added good-first-issue These issues are a good way to get started with Clippy A-documentation Area: Adding or improving documentation labels Dec 20, 2018
bors added a commit that referenced this issue Feb 18, 2019
Update why transmute_int_to_float is bad

As suggested in #3550, this PR changes the reason why using `transmute` from an integer to a float is not recommended. Effectively, `from_bits` uses `transmute` underneath, but the former is preferred.
bors added a commit that referenced this issue Feb 18, 2019
Update why transmute_int_to_float is bad

As suggested in #3550, this PR changes the reason why using `transmute` from an integer to a float is not recommended. Effectively, `from_bits` uses `transmute` underneath, but the former is preferred.
@bors bors closed this as completed in 74ac35b Feb 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-documentation Area: Adding or improving documentation good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

No branches or pull requests

2 participants