Skip to content

[lint request] unnecessary_float_transmute #3993

@gnzlbg

Description

@gnzlbg

The f32 and f64 types have pub fn from_bits(u32) -> f32 and pub fn to_bits(f32) -> u32 methods, yet many users don't know about them, and use transmute instead.

We should add a lint unnecessary_float_transmute that:

  • detects a transmute between a floating-point type and the unsigned integer type of the same bit width, and recommends using either from_bits or to_bits instead.

  • detects a transmute between a floating-point type and the signed integer type of the same bit-diwth, and recommends using from_bits/to_bits + an as cast (from unsigned to signed or vice-versa) instead.

When one of the arguments to the transmute is a macro variable, this lint probably can't work correctly (e.g. the macro might be expanded with types that aren't floats), so the lint should bail.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions