Open
Description
The relevant casts are at least those between pointers and integers, or between two pointers.
There's probably also some likelihood of same-type transmutes arising, which keep working after changing the types (of FFI signatures, for example) but could be removed because they're noops.
Clippy might be interested, if they don't already have this (cc @Manishearth).
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
solson commentedon Jun 13, 2016
Unnecessary transmutes are so common that I would encourage making this a warning lint in rustc itself. It could really help to cut down on the number of transmutes in the crates.io ecosystem.
Having transmutes when they aren't actually doing things that require transmute leads to a boy-who-cried-wolf problem, making people easily miss transmutes that are more dangerous.
sanxiyn commentedon Jun 13, 2016
AFAIK Clippy doesn't have this yet, although
useless_transmute
warns for same-type case.mcarton commentedon Jun 13, 2016
Clippy also has
crosspointer_transmute
(transmute
betweenT
and*T
,“between pointers and integers aboveEDIT: it's more limited actually) andtransmute_ptr_to_ref
(transmute
from*T
to&T
) in addition touseless_transmute
(transmute
fromT
toT
itself, “same-type transmutes” above).eddyb commentedon Jun 13, 2016
@mcarton To clarify, with "between pointers and integers" I meant casts that can be done with
as
between pointers andusize
, nothing to do with*usize
in particular.steveklabnik commentedon Oct 31, 2018
Triage: no changes
tamird commentedon Mar 26, 2025
Related to this: #34609 added documentation to
transmute
that suggests replacing reference-to-reference transmutes with pointeras
casts, but the documentation doesn't explain why that's better.cc @y86-dev
steveklabnik commentedon Mar 27, 2025
Copying my comment from Zulip:
The context I remember around the original PR, all those years ago: at the time, there was a lot of anxiety around unnecessary unsafe usage. The short and long of it was roughly "as is safe, transmute is unsafe, we should be encouraging use of as where possible."
I also think that since all those years ago, as's favorability has gone down, and tolerance for unsafe has gone up a little, so I'm not even sure that the original justification would still be a good one today.