-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
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 requireunsafe
and is less error-prone thantransmute
.