-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggest {to,from}_ne_bytes for transmutations between arrays and integers, etc #136083
base: master
Are you sure you want to change the base?
The head ref may contain hidden characters: "\u20E4\u20E4"
Conversation
r? @wesleywiser rustbot has assigned @wesleywiser. Use |
This comment has been minimized.
This comment has been minimized.
how do i fix stdarch? |
This comment has been minimized.
This comment has been minimized.
The job Click to see the possible cause of the failure (guessed by this bot)
|
Bool | Uint(..) | Int(..) => { | ||
matches!(fn_sig.output().kind(), Int(..) | Uint(..)).then(|| { | ||
errors::RedundantTransmute { | ||
sugg: format!("({arg}) as {}", fn_sig.output()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't suggest an as conversion here as as is so overloaded that I at least always forget what each conversion does. I remember there was an attempt in the past to create explicit methods for each as behaviour - if they've been merged, perhaps we could recommend them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what methods you're referring to... I don't think i should suggest i32::from_ne_bytes(x.to_ne_bytes())
? And try_from
has different behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are the unstable cast_unsigned
and cast_signed
methods, and there's an ACP for additional methods rust-lang/libs-team#453.
Perhaps for now the code just gets a FIXME so that the recommendation is changed once #125882 is stabilized (feature integer_sign_cast
)?
Is there a reason you made it a MIR lint instead of a HIR lint? HIR lints will make it easier to do the spans. |
@Noratrieb i dont know the difference; i looked for |
Also, how should i fix |
I would first recommend checking out if the lang team wants this lint. |
implements #136067
Rust has helper methods for many kinds of safe transmutes, for example integer<->bytes. This is a lint against using transmute for these cases.
It would be handy to suggest
u32::from_ne_bytes(x)
.This is implemented for
[u8; _]
->{float int}
This also implements the cases:
char
->u32
viaas
u32
->char
viafrom_u32_unchecked
(note: notesfrom_u32().unwrap()
)uXX
->iXX
viaas
(note: not sure if this could be improved)