Skip to content
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

Lint against transmutes which could be as casts. #34249

Open
eddyb opened this issue Jun 13, 2016 · 7 comments
Open

Lint against transmutes which could be as casts. #34249

eddyb opened this issue Jun 13, 2016 · 7 comments
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR.

Comments

@eddyb
Copy link
Member

eddyb commented Jun 13, 2016

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

@eddyb eddyb added the A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. label Jun 13, 2016
@solson
Copy link
Member

solson commented 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
Copy link
Member

sanxiyn commented Jun 13, 2016

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

@mcarton
Copy link
Member

mcarton commented Jun 13, 2016

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
Copy link
Member Author

eddyb commented Jun 13, 2016

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

@Mark-Simulacrum Mark-Simulacrum added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jul 25, 2017
@steveklabnik
Copy link
Member

Triage: no changes

@tamird
Copy link
Contributor

tamird commented Mar 26, 2025

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
Copy link
Member

steveklabnik commented 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. C-feature-request Category: A feature request, i.e: not implemented / a PR.
Projects
None yet
Development

No branches or pull requests

7 participants