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

useless_transmute being raised when it's doing multiple casts #1545

Closed
wfraser opened this issue Feb 15, 2017 · 3 comments
Closed

useless_transmute being raised when it's doing multiple casts #1545

wfraser opened this issue Feb 15, 2017 · 3 comments
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy L-unnecessary Lint: Warn about unnecessary code T-middle Type: Probably requires verifiying types

Comments

@wfraser
Copy link

wfraser commented Feb 15, 2017

Here's a code sample:

warning: transmute from a reference to a pointer
  --> src/main.rs:35:29
   |
35 |         let fd = libc::open(mem::transmute(b"/dev/null\0"), libc::O_RDONLY);
   |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: #[warn(useless_transmute)] on by default
help: try
   |         let fd = libc::open(b"/dev/null\0" as *const [u8; 10] as *const i8, libc::O_RDONLY);
   = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#useless_transmute

That seems kind of silly. It's not useless; it's doing two casts at once (reference to pointer, and then pointer to different pointer) and saving quite a bit of typing.

@oli-obk
Copy link
Contributor

oli-obk commented Feb 16, 2017

wfraser: it is useless, because there's a perfectly clean way (the two casts) to do what you want. Or even better you should be doing b"/dev/null\0".as_ptr(). The suggestion could be improved in the array to element ptr case.

@oli-obk oli-obk added L-unnecessary Lint: Warn about unnecessary code good-first-issue These issues are a good way to get started with Clippy C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages T-middle Type: Probably requires verifiying types labels Feb 16, 2017
@wfraser
Copy link
Author

wfraser commented Feb 16, 2017

Well, by that logic, all uses of transmute are useless, because they can all be done using pointer casts. However, it takes multiple casts. The lint should be for cases where it could be a single cast, or a type coercion, or the types are the same. Indeed, this is much closer to what its help text says.

(BTW, I'm not using b"/dev/null\0".as_ptr() because it produces a *const u8 where I need a *const libc::c_char (which on some systems is a i8). I would have to do b"/dev/null\0".as_ptr() as *const libc::c_char which is still much more simply written using the transmute.)

@Manishearth
Copy link
Member

because they can all be done using pointer casts.

No, there's a difference.

If you have a pointer, and are transmuting to another pointer, a pointer cast is the best way to go. It's less likely for inference to mess up with a pointer cast.

If you have something else, and want to transmute to something also-else, transmute is great. Sure, it can be done with pointer casts, but only by taking a reference (*(&x as *const T as *const U)). This is strictly worse than a transmute since a transmute does a size check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy L-unnecessary Lint: Warn about unnecessary code T-middle Type: Probably requires verifiying types
Projects
None yet
Development

No branches or pull requests

3 participants