Description
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
orto_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
+ anas
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
[-]transmute floats to ints and vice-versa[/-][+][lint request] unnecessary_float_transmute[/+]Centril commentedon Apr 18, 2019
I think this may also be a candidate for rustc uplifting.
krishna-veerareddy commentedon Dec 6, 2019
@gnzlbg Hey looks like the lint for int to float conversion already exists. I can tackle the the float to int conversion.
Auto merge of #4889 - krishna-veerareddy:issue-3993-float-to-int-tran…