Skip to content

Lint against transmutes which could be as casts. #34249

Open
@eddyb

Description

@eddyb
Member

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).

Activity

added
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
on Jun 13, 2016
solson

solson commented on Jun 13, 2016

@solson
Member

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

sanxiyn commented on Jun 13, 2016

@sanxiyn
Member

AFAIK Clippy doesn't have this yet, although useless_transmute warns for same-type case.

mcarton

mcarton commented on Jun 13, 2016

@mcarton
Member

Clippy also has crosspointer_transmute (transmute between T and *T, “between pointers and integers above EDIT: it's more limited actually) and transmute_ptr_to_ref (transmute from *T to &T) in addition to useless_transmute (transmute from T to T itself, “same-type transmutes” above).

eddyb

eddyb commented on Jun 13, 2016

@eddyb
MemberAuthor

@mcarton To clarify, with "between pointers and integers" I meant casts that can be done with as between pointers and usize, nothing to do with *usize in particular.

steveklabnik

steveklabnik commented on Oct 31, 2018

@steveklabnik
Member

Triage: no changes

tamird

tamird commented on Mar 26, 2025

@tamird
Contributor

Related to this: #34609 added documentation to transmute that suggests replacing reference-to-reference transmutes with pointer as casts, but the documentation doesn't explain why that's better.

  • Is that advice still valid?
  • If yes, can someone tell me why? should be in scope of this issue?
  • If not, should we remove it?

cc @y86-dev

steveklabnik

steveklabnik commented on Mar 27, 2025

@steveklabnik
Member

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.

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-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-feature-requestCategory: A feature request, i.e: not implemented / a PR.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @solson@steveklabnik@sanxiyn@eddyb@tamird

        Issue actions

          Lint against transmutes which could be `as` casts. · Issue #34249 · rust-lang/rust