Skip to content

Is transmute_int_to_float's justification reasonable? #3550

@Enet4

Description

@Enet4

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-documentationArea: Adding or improving documentationgood first issueThese issues are a good way to get started with Clippy

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions