Skip to content

[lint request] unnecessary_float_transmute #3993

Closed
@gnzlbg

Description

@gnzlbg
Contributor

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.

Activity

changed the title [-]transmute floats to ints and vice-versa[/-] [+][lint request] unnecessary_float_transmute[/+] on Apr 18, 2019
Centril

Centril commented on Apr 18, 2019

@Centril
Contributor

I think this may also be a candidate for rustc uplifting.

krishna-veerareddy

krishna-veerareddy commented on Dec 6, 2019

@krishna-veerareddy
Contributor

@gnzlbg Hey looks like the lint for int to float conversion already exists. I can tackle the the float to int conversion.

added a commit that references this issue on Dec 8, 2019

Auto merge of #4889 - krishna-veerareddy:issue-3993-float-to-int-tran…

b245fbd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @matthiaskrgr@Centril@gnzlbg@krishna-veerareddy

      Issue actions

        [lint request] unnecessary_float_transmute · Issue #3993 · rust-lang/rust-clippy